设计模式-代理模式

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、高扩展

创新互联专注于将乐网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供将乐营销型网站建设,将乐网站制作、将乐网页设计、将乐网站官网定制、成都微信小程序服务,打造将乐网络公司原创品牌,更为您提供将乐网站排名全网营销落地服务。

设计模式-代理模式


分享题目:设计模式-代理模式
文章链接:http://csdahua.cn/article/jsspse.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流