·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 设计模式(8)---桥接模式

设计模式(8)---桥接模式

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

定义:

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

解释:抽象与它的实现分离并不是说让抽象类与其派生类分离,而是抽象类和它的派生类用来实现自己的对象。

 

UML类图和基本代码:

class PRogram
    {
        static void Main(string[] args)
        {
            Abstration ab = new RefinedAbstration();

            ab.SetImplementor (new ConcreteImplementorA ());
            ab.Operation();

            ab.SetImplementor(new ConcreteImplementorB());
            ab.Operation();

            Console.Read();
        }
    }

    abstract class Implementor
    {
        public abstract void Operation();
    }

    class ConcreteImplementorA : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("implement A action");
        }
    }

    class ConcreteImplementorB : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("implement B action");
        }
    }

    class Abstration
    {
        protected Implementor implementor;

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

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

    class RefinedAbstration : Abstration
    {
        public override void Operation()
        {
            implementor.Operation();
        }
    }
View Code

 

在网上看到一个实例,感觉非常贴切,”拿来主义“参考学习。

具体是:现实生活中实现遥控器,遥控器中并不包含开机、关机、换台等功能的实现,遥控器只是包含了电视机上的这些功能的引用,然后红外线去找到电视机上对应功能的实现。

抽象类电视机及生成长虹、三星电视机:

public abstract class TV
    {
        public abstract void On();
        public abstract void Off();
        public abstract void TurnChannel();
    }

    public class ChangHong : TV
    {
        public override void On()
        {
            Console.WriteLine("长虹牌电视机已经打开了");
        }

        public override void Off()
        {
            Console.WriteLine("长虹牌电视机已经关掉了");
        }

        public override void TurnChannel()
        {
            Console.WriteLine("长虹牌电视机换频道");
        }
    }

    public class Samsung : TV
    {
        public override void On()
        {
            Console.WriteLine("三星牌电视机已经打开了");
        }

        public override void Off()
        {
            Console.WriteLine("三星牌电视机已经关掉了");
        }

        public override void TurnChannel()
        {
            Console.WriteLine("三星牌电视机换频道");
        }
    }
View Code

 

 抽象类遥控器及实现:

public class RemoteControl
    {
        private TV implementor;
        public TV Implementor
        {
            get { return implementor; }
            set { implementor = value; }
        }

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

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

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

    public class ConcreteRemote : RemoteControl
    {
        
    }
View Code

 

客户端调用:

RemoteControl remoteControl = new ConcreteRemote();

            remoteControl.Implementor = new ChangHong();
            remoteControl.On();
            remoteControl.Off();
            remoteControl.TurnChannel();

 

优点:

抽象接口与实现解耦。

抽象和实现都可以独立扩展,不会影响到对方。

缺点:

增加了系统的复杂度。

 

适用场景:

一个类存在两个独立变化的维度,且两个维度都需要进行扩展。

需要跨越多个平台的图形和窗口系统。