<pre code_snippet_id="1580901" snippet_file_name="blog_20160217_1_7167142" name="code" class="java">代码注释很清晰就不罗嗦了
public interface iGiveGift { void GiveFlowers(); void GiveShoes(); }
<pre name="code" class="java">/** * 真正的追求者 */ public class Pursuit implements iGiveGift { private SchoolGirl mm; public Pursuit(SchoolGirl mm) { this.mm = mm; } @Overridepublic void GiveFlowers() { System.out.println(mm.getName() + "送花"); } @Overridepublic void GiveShoes() { System.out.println(mm.getName() + "送鞋"); } }
/** * * 代理者 */ public class Proxy implements iGiveGift { Pursuit mPursuit; public Proxy(SchoolGirl schoolGirl) { this.mPursuit = new Pursuit(schoolGirl); } @Override public void GiveFlowers() { mPursuit.GiveFlowers(); } @Override public void GiveShoes() { mPursuit.GiveShoes(); } }
</pre><pre>
/**被追求者*/ public class SchoolGirl { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
/** * 为别人做嫁妆----代理者模式 <br/> * 代理模式,为其他对象提供一种代理以控制对这个对象的访问。 */ public class Test { public static void main(String[] args) { SchoolGirl mSchoolGirl = new SchoolGirl(); mSchoolGirl.setName("狗剩子"); Proxy mProxy = new Proxy(mSchoolGirl); // 看似调用Proxy的GiveFlowers方法 // 其实在Proxy的GiveFlowers的方法里调用的还是Persuit的GiveFlowers方法 // 好处:在Test类里隐藏了Persuit类 mProxy.GiveFlowers(); mProxy.GiveShoes(); } }
时间: 2024-10-03 03:32:13