桥接模式(Bridge)

个人理解:

桥接模式的精髓在于维护一个抽象对象,并抽取这个对象的抽象部分。


UML类图:

代码实现:

    public interface IComponent
    {
        void Operation();
    }

    public class ComponentA : IComponent
    {
        public void Operation()
        {
            Console.WriteLine("This is A...");
        }
    }

    public class ComponentB : IComponent
    {
        public void Operation()
        {
            Console.WriteLine("This is B...");
        }
    }

    public abstract class Abstraction:IComponent
    {
        protected IComponent component;

        public Abstraction(IComponent component)
        {
            this.component = component;
        }

        public abstract void Operation();
    }

    public class ConcreteAbstraction : Abstraction
    {
        public ConcreteAbstraction(IComponent component)
            : base(component)
        { }

        public override void Operation()
        {
            this.component.Operation();
        }
    }

Python代码实现:

class Chinese(object):
    def Say(self):
        raise NotImplementedError("say abstract")


class NorthChinese(object):
    def Say(self):
        print "your elder-grandpa"

class ShanghaiChinese(object):
    def Say(self):
        print "small second old"

class Manager(object):
    def ManageSay(self):
        raise NotImplementedError("say abstract")

class ChineseManager(Manager):
    __chinese=None
    def __init__(self, chinese):
        self.__chinese=chinese
    def ManageSay(self):
        self.__chinese.Say()

if __name__ == "__main__":
    manager1=ChineseManager(NorthChinese())
    manager2=ChineseManager(ShanghaiChinese())
    manager1.ManageSay()
    manager2.ManageSay()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值