设计模式-桥接模式

//职责
abstract class Command
    {
        public abstract int Run<T>(T t);
    }
    class Add : Command
    {
        public override int Run<T>(T t)
        {
            Console.WriteLine("add{0}",t.ToString());
            return 0;
        }
    }
    class Update : Command
    {
        public override int Run<T>(T t)
        {
            Console.WriteLine("update{0}", t.ToString());
            return 0;
        }
    }
    class Delete : Command
    {
        public override int Run<T>(T t)
        {
            Console.WriteLine("delete{0}", t.ToString());
            return 0;
        }
    }
        //实体类
        bstract class Entity
    {
        protected Command command;
        public void SetCommand(Command _command)
        {
            command = _command;
        }
        public abstract int Run();       
    }
    class User : Entity
    {
        public string name { get; set; }
        public int age { get; set; }

        public override int Run()
        {
            return command.Run(this);
        }        
    }

    class Manager : Entity
    {
        public string name { get; set; }
        public int age { get; set; }

        public override int Run()
        {
            return command.Run(this);
        }
    }
        //前端
        static void Main(string[] args)
        {            
            Command add = new Add();
            Command update = new Update();
            Command delete = new Delete();
            Entity user = new User();
            user.SetCommand(add);
            user.Run();
            user.SetCommand(update);
            user.Run();
            user.SetCommand(delete);
            user.Run();
            Console.ReadLine();
        }

总结:DEMO不是很适合做桥接模式,但是完全实现了桥接模式。
桥接模式就是把抽象类和他的职责分离,重新把职责整个一个新的抽象,然后把职责注入到抽象类。
用到了聚合(合成)复用原则(能用聚合的尽量不要用继承),符合单一,开闭原则。
优点:避免了继承类的无线扩大,并且扩展性增强。
缺点:对业务理解不到位,可能被错误运用,就像DEMO。

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站建设、外贸网站建设、梁子湖网络推广、小程序设计、梁子湖网络营销、梁子湖企业策划、梁子湖品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供梁子湖建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

分享文章:设计模式-桥接模式
转载注明:https://www.cdcxhl.com/article46/ghdeeg.html

成都网站建设公司_创新互联,为您提供移动网站建设企业网站制作网站维护全网营销推广小程序开发电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司