定义:为其他对象提供一种代理以控制对这个对象的访问。
我的理解就是此对象就是另外某个对象的代理(镜子,封面……)。是一种“代理者类”和“被代理者类”的意思。也就是控制和被控制关系。 我在很多书和文档上都看到Proxy模式和State模式都被称为Object decoupling(退耦)。实在让吾等非文学家感到郁闷。网上查了下大意是 “指消除或减轻两个以上物体间在某方面相互影响的方法”。这种书写的就是有问题,多写一句会死人啊。简单的东西非要把你写昏了才显示他水平。 关于为什么都称之为退耦,我在State模式的探讨中有描述,看完State模式比较好理解。
|
//: proxy:ProxyDemo.java import junit.framework.*; interface ProxyBase { void f(); void g(); void h(); } class Proxy implements ProxyBase { private ProxyBase implementation; public Proxy() { implementation = new Implementation(); } // Pass method calls to the implementation: public void f() { implementation.f(); } public void g() { implementation.g(); } public void h() { implementation.h(); } } class Implementation implements ProxyBase { public void f() { System.out.println("Implementation.f()"); } public void g() { System.out.println("Implementation.g()"); } public void h() { System.out.println("Implementation.h()"); } } public class ProxyDemo extends TestCase { Proxy p = new Proxy(); public void test() { // This just makes sure it will complete // without throwing an exception. p.f(); p.g(); p.h(); } public static void main(String args[]) { junit.textui.TestRunner.run(ProxyDemo.class); } } ///:~
|