Static Proxy (静态代理模式)

来源:互联网 发布:歌手阿朵 知乎 编辑:程序博客网 时间:2024/05/29 13:20

1.定义一个接口

ProxyInterface.java

package com.staticproxy ;public interface ProxyInterface  //就假设为 定义一个购房的接口{public void buyTest() ;//定义一个实现购房的方法规范}

 

 

2.定义一个真实角色实现了ProxyInterface接口

RealMaster.java

 

package com.staticproxy ;public class RealMaster implements ProxyInterface //定义一个真实的主人,也就是 房主{public void buyTest(){System.out.println("实现购买房主的手续") ;}}

 

3.定义一个代理角色,实现了ProxyInterface接口,还持有一个 真实主人对象的引用

ProxyMaster.java

 

package com.staticproxy ;public class ProxyMaster implements ProxyInterface //定义一个代理主人,就相当于 中介,{private RealMaster rm;//持有一个 真实主人对象的引用  public void buyTest(){        this.beforeTest() ; //中介 一开始 收取买主 的介绍费        this.rm = new RealMaster() ;rm.buyTest() ; this.afterTest() ; // 购完房 中介要收取的费用}public void beforeTest(){System.out.println("中介 一开始 的介绍费") ;}public void afterTest(){System.out.println("购完房 中介要收取的费用") ;}}

4.客户端
Client.java

 

package com.staticproxy ;public class Client //定义一个客户端{public static void main(String[] args){ProxyMaster pm = new ProxyMaster() ;        pm.buyTest() ;}}


 

原创粉丝点击