Static Proxy (静态代理模式)

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() ;

	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值