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

来源:互联网 发布:百分百网络营销软件 编辑:程序博客网 时间:2024/06/05 15:10

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("客户满意度调查");}}


0 0
原创粉丝点击