温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

设计模式-代理模式

发布时间:2020-07-23 13:53:01 来源:网络 阅读:309 作者:全嗲吉祥 栏目:编程语言
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、高扩展

设计模式-代理模式

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI