Java设计模式——静态代理设计模式

1.定义

代理模式:为其他对象提供一个代理以控制对该对象的访问


2.代码示例

public class StaticAgency
{
	public static void main(String[] args)
	{
		Husband husband = new Husband();
		Agency agency = new Agency(husband);
		agency.shopping();
	}
}

interface Shop
{
	public void shopping();
}

//被代理人
class Husband implements Shop
{
	public void shopping()
	{
		System.out.println("购买海外代购化妆品");
	}
}

//代理人
class Agency implements Shop
{
	private Shop target;//被代理人 

	public Agency(Shop target)
	{
		this.target = target;
	}

	public void shopping()
	{
		//代购之前代理人要做的业务
		System.out.println("对代购化妆品的评估");
		
		//这里就是控制的体现
		//我就用true作为条件进行简化
		if(true)
		{
			target.shopping();//被代理人要做的业务
		}
		
		//代购之后代理人要做的业务
		System.out.println("客户满意度调查");
	}
}


你可能感兴趣的:(Java)