代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问。代理者可以作任何东西的接口:网络连接、内存中的大物件、档案或其它昂贵或无法复制的资源。例如我们生活中使用代理上网,客户通过网络代理连接网络,由代理服务器完成用户权限和访问限制等于上网操作相关的操作。代码如下:
//代理设计 interface NetWork{ public void browser(); } class Real implements NetWork{ public void browser(){ System.out.println("browser infomation"); } } class Proxy implements NetWork{ private NetWork network; public Proxy(NetWork network){ this.network=network; } public void check(){ System.out.println("user info right!"); } public void browser(){ this.check(); this.network.browser(); } } public class ProxyDemo{ public static void main(String[] args){ NetWork net=null; net=new Proxy(new Real()); net.browser(); } }