设计模式----Proxy(代理)模式
来源:互联网 发布:vwap算法 编辑:程序博客网 时间:2024/05/01 13:03
设计模式----Proxy(代理)模式
GOF:为其他对象提供一种代理以控制对这个对象的访问。
和Flyweight不一样Proxy本身就是代理的意思。对一个对象进行访问控制的原因是,当我们确实需要这个对象,或者这个对象的某些功能后才去使用它。在一个论坛系统中(比如说非常著名的“天涯社区”)来说,浏览该论坛的有两种基本用户,一种是注册用户:可以发表文章,修改自己发表的文章,删除自己的文章,回复别人的文章等。另一种是游客(非注册用户):只有看文章的权限。在这样的系统中就可以用Proxy模式来代理控制这样两种用户的权限。
根据我在各种参考资料上看到的内容,Proxy模式还有许多变种,他适用于很多地方。在GOF的书中,阐述了如下使用Proxy模式的情景:
1. 远程代理(Remote Proxy):为一个对象在不同地址空间提供局部代表。
2. 虚代理(Virtua Proxy):根据需要创建开销很大的对象。比如说,在网站上看图片,一般都先显示小图片,想看大图片的时候可以点击小图片。
3. 保护代理(Protection Proxy):控制对原始对象的访问。保护代理用于对象应该有不同的访问权限的时候。例如我们上面提到的论坛系统。
4. 智能指引(Smart Reference)取代了简单的指针,它在访问对象时执行一些附加操作。他的典型用途包括:对指向实际对象的引用计数,这样当该对象没有引用时,可以自动释放他(也称为Smart Pointers。这个好像在C++中有专门的讲解);当第一次引用一个持久对象时,将他装入内存;在访问一个实际对象前,检查是否已经锁定了他,以确保其他对象不能改变他。
举一个简单的例子来说明Proxy模式。在娱乐圈里明星要演出,要做广告这些事情都不是明星自己和商家谈,而是有一个自己的经济人,这个经济人就代理这个明星的一切外事活动。比如说和某某电影公司签约阿,或者和某厂家签约广告啊什么的。这个经济人就是个代理的脚色,他和商家谈判的时候觉得可行就让明星去演出或去拍广告,如果谈判不行他就取消该商家和该明星的联系活动,一定程度上来说他控制者明星的活动。
下面看简单的代码:
package Proxy;
public abstract class MusicPerson
...{
public abstract void age();
public abstract void requital();//报酬
public abstract void date();
}//end class MusicPerson
明星出场:
package Proxy;
public class MusicStar extends MusicPerson
...{
public void date()
...{
System.out.println("the music person's date");
}//end f()
public void requital()
...{
System.out.println("the music person's retuital");
}//end g()
public void age()
...{
System.out.println("the musicperson's age");
}//end h()
}//end class MusicStar
代理人来了:
package Proxy;
public class MusicStarProxy extends MusicPerson
...{
private MusicPerson implementation;
public MusicStarProxy()
...{
implementation = new MusicStar();
}
public void date()
...{
implementation.date();
}//end date()
public void requital()
...{
implementation.requital();
}//end requital()
public void age()
...{
// implementation.age();
System.out.println("Sorry I can't tell you the musicstar's age!");
}//end age()
}//end class MusicStarProxy
Porxy模式的运用:
package Proxy;
public class ProxyPattern
...{
private MusicPerson p = new MusicStarProxy();
public void showProxyPattern()
...{
p.date();
p.requital();
p.age();
}//end showProxyPattern()
public static void main(String[] args)
...{
System.out.println("----------------------------");
System.out.println("The Proxy Pattern");
ProxyPattern pp = new ProxyPattern();
pp.showProxyPattern();
System.out.println("----------------------------");
}//end main(...)
}//end class ProxyPattern
package Proxy;
public abstract class MusicPerson
...{
public abstract void age();
public abstract void requital();//报酬
public abstract void date();
}//end class MusicPerson
明星出场:
package Proxy;
public class MusicStar extends MusicPerson
...{
public void date()
...{
System.out.println("the music person's date");
}//end f()
public void requital()
...{
System.out.println("the music person's retuital");
}//end g()
public void age()
...{
System.out.println("the musicperson's age");
}//end h()
}//end class MusicStar
代理人来了:
package Proxy;
public class MusicStarProxy extends MusicPerson
...{
private MusicPerson implementation;
public MusicStarProxy()
...{
implementation = new MusicStar();
}
public void date()
...{
implementation.date();
}//end date()
public void requital()
...{
implementation.requital();
}//end requital()
public void age()
...{
// implementation.age();
System.out.println("Sorry I can't tell you the musicstar's age!");
}//end age()
}//end class MusicStarProxy
Porxy模式的运用:
package Proxy;
public class ProxyPattern
...{
private MusicPerson p = new MusicStarProxy();
public void showProxyPattern()
...{
p.date();
p.requital();
p.age();
}//end showProxyPattern()
public static void main(String[] args)
...{
System.out.println("----------------------------");
System.out.println("The Proxy Pattern");
ProxyPattern pp = new ProxyPattern();
pp.showProxyPattern();
System.out.println("----------------------------");
}//end main(...)
}//end class ProxyPattern
下面是Proxy模式的UML图:
Porxy模式给我的感觉是比较简单,简单的原因可能是我没有理解好,其他相关的变种也没有去看,等待以后有机会,有时间再去研究。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=987253
- Proxy代理设计模式
- 设计模式 - 代理(Proxy)
- 代理设计模式--Proxy
- Proxy(代理)设计模式
- 代理 proxy 设计模式
- 设计模式----Proxy(代理)模式
- 设计模式--代理模式(Proxy)
- 设计模式 -- 代理模式(Proxy)
- 设计模式--代理模式(Proxy)
- [设计模式]Proxy代理模式
- 设计模式--Proxy(代理模式)
- [设计模式]代理模式(Proxy)
- 设计模式-Proxy代理模式
- 设计模式--proxy代理模式
- 设计模式 -- 代理模式 Proxy
- 设计模式-代理模式(Proxy)
- [设计模式] 代理模式(Proxy)
- 设计模式--代理模式(Proxy)
- 设计模式----Prototype(原形)模式
- 程序员考试笔记
- 设计模式----Composite(组合)模式
- 耳朵发热
- 设计模式----Flyweight(享元)模式
- 设计模式----Proxy(代理)模式
- 数据结构笔记
- .net精简框架集(WinCE平台下),XML反序列化成功与否与时间相关
- 设计模式----Chain of Responsibility(职责链)模式
- 考点
- SQL SERVER安装问题,总是出现提示以前的某个程序安装在计算机上创建挂
- 设计模式----Command(命令)模式
- 设计模式----Interpreter(解释器)模式
- 转树查找