设计模式-代理模式
public class house { public string name { get; set; } public house(string _name) { this.name = _name; } } public interface guke { void buyHouse(); } public class xiaofeizhe : guke { house h; public xiaofeizhe(house _h) { h = _h; } public void buyHouse() { Console.WriteLine(string.Format("我要买名字为{0}的房子",h.name)); } } public class proxy : guke { house h; xiaofeizhe x; public proxy(house _h) { this.h = _h; } public void buyHouse() { if(x==null) x = new xiaofeizhe(h); x.buyHouse(); } } 前端: static void Main(string[] args) { house h = new house("盘古大厦"); proxy p = new proxy(h); p.buyHouse(); Console.ReadLine(); }
总结:如果不使用代理类·直接调用对象,那么当需求有变更时,就需要改变该对象,违反了开闭原则,使用代理类的话,就没有这种问题出现。
特点:代理类中引入被代理的对象,和装饰模式有一点类似,都是引入第三方对象(但是装饰模式主要是扩展对象的行为、属性)。
好处:1、结构清晰,2、保护了被代理对象,3、高扩展
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。