设计模式(6)-代理模式(Proxy)
来源:互联网 发布:怎样查农村淘宝服务站 编辑:程序博客网 时间:2024/06/07 18:33
在使用一个类之前,要做一些其它操作,这时,我们就可以使用代理模式。
代理模式分好多种,有远程代理,虚拟代理,保护代理等等。
代理和适配器的一个明显区别在于,适配器是在源类接口不合要求时,加一个适配器改变接口。但代理和装饰一样,是不改变接口的。
装饰是在不改变接口的同时,扩充源类的功能,代理一般是对源类作限制。
UserManage 接口
package org.bluebea.gof.proxy;
public interface UserManage {
public void addUser();
}
UserManageImpl
package org.bluebea.gof.proxy;
public class UserManageImpl implements UserManage {
public UserManageImpl() {
}
public void addUser() {
System.out.println("-- " + "UserManageImpl.addUser().");
}
}
UserManageProxy
package org.bluebea.gof.proxy;
public class UserManageProxy implements UserManage {
private UserManage userManage;
public UserManageProxy() {
}
public void addUser() {
//int roleType = 2;
int roleType = 1;
if (null == userManage) {
userManage = new UserManageImpl();
}
if (2 == roleType) {
userManage.addUser();
} else {
System.out.println("-- " + "can't add user.");
}
}
}
Test
package org.bluebea.gof.proxy;
public class Test {
public Test() {
}
public static void main(String[] args) {
UserManage userManage = new UserManageProxy();
userManage.addUser();
}
}
UserManageProxy 还是调用UserManageImpl ,但是在调用时做了限制。
- Proxy代理设计模式
- 设计模式 - 代理(Proxy)
- 代理设计模式--Proxy
- Proxy(代理)设计模式
- 代理 proxy 设计模式
- 设计模式(6)-代理模式(Proxy)
- 设计模式----Proxy(代理)模式
- 设计模式--代理模式(Proxy)
- 设计模式 -- 代理模式(Proxy)
- 设计模式--代理模式(Proxy)
- [设计模式]Proxy代理模式
- 设计模式--Proxy(代理模式)
- [设计模式]代理模式(Proxy)
- 设计模式-Proxy代理模式
- 设计模式--proxy代理模式
- 设计模式 -- 代理模式 Proxy
- 设计模式-代理模式(Proxy)
- [设计模式] 代理模式(Proxy)
- 设计模式(3)-原型模式(Prototype)
- ASP连接数据库的11种方法
- 设计模式(5)-外观模式(Facade)
- DateSet使用技巧
- 异步调用xml web service
- 设计模式(6)-代理模式(Proxy)
- 设计模式(9)-装饰模式(Decorator)
- 设计模式(7)-适配器模式(Adapter)
- 防SQL注入
- 设计模式(8)-组合模式(Composite)
- 设计模式(10)-桥接模式(Bridge)
- (转宝贝记).net 框架设计 签名系统三层架构
- linux配置-磁盘
- 设计模式(12)-模板方法模式(Template Method)