簡介摘要
這裡我們假設有一個用戶管理模塊
public class User {
private String username;
private String password;
public User() {
}
public User(String username
this
this
}
}
為了提供功能模塊
public interface IUserFace {
public void addUser(User user);
public void removeUser(User user);
}
接下來為這個接口編寫一個真正實現具體功能的類出來
public class UserFaceImpl implements IUserFace {
public void addUser(User user) {
//這裡處理相關的添加用戶的代碼任務
//比如說連接數據庫
System
}
public void removeUser(User user) {
//這裡處理相關的刪除用戶的代碼任務
//比如說連接數據庫
System
}
}
好了
public class FaceFactory {
private static FaceFactory instance;
private FaceFactory() {
}
public static FaceFactory getInstance() {
if(instance == null) {
instance = new FaceFactory();
}
return instance;
}
public IUserFace createUserFace() {
return new UserFaceImpl();
}
}
完成了工廠類的代碼
public class App {
public static void main(String args[]) {
User u = new User();
IUserFace uf = FaceFactory
uf
}
}
從上面代碼我們可以看到
public class UserFaceProxy implements IUserFace {
private IUserFace userFace;
public UserFaceProxy(IUserFace userFace) {
this
}
public void addUser(User user) {
//在這裡檢查權限
//如果權限通過則完成下面的工作
userFace
}
public void removeUser(User user) {
//在這裡檢查權限
//如果權限通過則完成下面的工作
userFace
}
}
在代碼中你可以看到
public class FaceFactory {
private static FaceFactory instance;
private FaceFactory() {
}
public static FaceFactory getInstance() {
if(instance == null) {
instance = new FaceFactory();
}
return instance;
}
public IUserFace createUserFace() {
IUserFace userFace = new UserFaceImpl();
IUserFace proxy = new UserFaceProxy(userFace);
return proxy;
}
}
好了
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26887.html