代理模式java源码实现(大话设计模式学习备忘录)
public class SchoolGirl { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
//追求者 public class Pursuit { SchoolGirl mm; public Pursuit(SchoolGirl mm){ this.mm=mm; } public void giveDolls(){ System.out.println(mm.getName()+"送你洋娃娃"); } public void giveFlowers(){ System.out.println(mm.getName()+"送你鲜花"); } public void giveChocolate(){ System.out.println(mm.getName()+"送你巧克力"); } }
/** * <h1>代理模式</h1> * QS:卓贾易找戴励代理和李娇娇谈恋爱 * @author xangqun * */ public class Program { /**PS: * 这里成了卓贾易和李娇娇谈恋爱了,不合题 * @param args */ public static void main(String[] args) { SchoolGirl jiaojaio=new SchoolGirl(); jiaojaio.setName("李娇娇"); Pursuit zhuojiayi=new Pursuit(jiaojaio); zhuojiayi.giveDolls(); zhuojiayi.giveFlowers(); zhuojiayi.giveChocolate(); } }
public interface IGiveGift { void GiveDolls(); void GiveFlowers(); void GiveChocolate(); }
public class PursuitTwo implements IGiveGift { SchoolGirl mm; public PursuitTwo(SchoolGirl mm){ this.mm=mm; } @Override public void GiveChocolate() { System.out.println(mm.getName()+"送你巧克力"); } @Override public void GiveDolls() { System.out.println(mm.getName()+"送你洋娃娃"); } @Override public void GiveFlowers() { System.out.println(mm.getName()+"送你鲜花"); } }
public class Proxy implements IGiveGift { PursuitTwo gg; public Proxy(SchoolGirl mm){ gg=new PursuitTwo(mm); } @Override public void GiveChocolate() { gg.GiveChocolate(); } @Override public void GiveDolls() { gg.GiveDolls(); } @Override public void GiveFlowers() { gg.GiveFlowers(); } }
package com.xangqun.proxy; /** * <h1>代理模式</h1> * QS:卓贾易找戴励代理和李娇娇谈恋爱 * @author xangqun * */ public class ProgramTwo { /**<b>代理模式(proxy)<b> * 为其他对象提供一种代理以控制对这个对象的访问 * <ul> * <li>远程代理,也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间的事实</li> * <li>虚拟代理,是根据需要创建开销很大的对象,通常它在存放实例化需要很长时间的真实对象</li> * <li>安全代理,用来控制真实对象访问时的权限</li> * <li>智能指引,是指当调用真实的对象时,代理处理另外一些事</li> * </ul> * @param args */ public static void main(String[] args) { SchoolGirl jiaojaio=new SchoolGirl(); jiaojaio.setName("李娇娇"); Proxy daili=new Proxy(jiaojaio); daili.GiveDolls(); daili.GiveFlowers(); daili.GiveChocolate(); } }
相关推荐
Balmunc 2020-08-02
hegaoye0 2020-08-01
gaochujia 2020-05-31
masternan 2020-04-10
trandy 2016-12-20
droidpioneer的IT 2013-12-29
Darklovy 2019-11-03
yishujixiaoxiao 2019-10-20
89251348 2019-09-23
jscese 2017-11-15
宁静致远 2010-12-24
masternan 2019-07-01
xusong 2013-06-26
chengrile 2013-06-01
rionchen 2010-04-02
finalcola 2010-04-02
bobpipi 2010-04-02