桥接模式(Bridge): 将抽象部分与实现部分分离,使它们都可以独立地变化。
即实现系统可能有多角度分类,每一种分类都可能变化,把多角度分离出来让其独立变化,减少它们之间的耦合。
namespace BridgeMode { //抽象实现类 abstract class Implementor { public abstract void Operation(); } class ConcreteImplementorA : Implementor { public override void Operation() { Console.WriteLine("具体实现A方法执行"); } } class ConcreteImplementorB : Implementor { public override void Operation() { Console.WriteLine("具体实现B方法执行"); } } class Abstraction { protected Implementor implementor; public void SetImplementor(Implementor implementor) { this.implementor = implementor; } public virtual void Operation() { implementor.Operation(); } } class RefinedAbstraction : Abstraction { public override void Operation() { implementor.Operation(); } } class Program { static void Main(string[] args) { Abstraction ab = new RefinedAbstraction(); ab.SetImplementor(new ConcreteImplementorA()); ab.Operation(); ab.SetImplementor(new ConcreteImplementorB()); ab.Operation(); Console.Read(); } } }
设计模式------桥接模式
猜你喜欢
转载自blog.csdn.net/love_phoebe/article/details/104084551
今日推荐
周排行