码迷,mamicode.com
首页 > 其他好文 > 详细

桥接模式(Bridge)

时间:2017-06-18 13:22:27      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:tor   protected   prot   角度   imp   virt   模式   部分   abstract   

桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立的变化。

实现系统可能有多角度分类,每一个分类都可能变化,那么把这种角度分离出来,让它们独立变化,减少他们之间的耦合。

 

 abstract class Implementor
    {
        public abstract void Operation();
    }
class AImplementor : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("AImplementor的方法执行");
        }
    }
class BImplementor : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("BImplementor的方法执行");
        }
    }
class Abstraction
    {
        protected Implementor implementor;

        public void SetImplementor(Implementor implementor)
        {
            this.implementor = implementor;
        }

        public virtual void Operation()
        {
            implementor.Operation();
        }

    }
class AAbstraction : Abstraction
    {
    }
Abstraction obj = new AAbstraction();
            obj.SetImplementor(new AImplementor());
            obj.Operation();
            obj.SetImplementor(new BImplementor());
            obj.Operation();

 

桥接模式(Bridge)

标签:tor   protected   prot   角度   imp   virt   模式   部分   abstract   

原文地址:http://www.cnblogs.com/suzixuan/p/7043880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!