java-pattern 之 读书笔记7 ---Bridge
bridge模式就是所谓的桥模式,这种模式应用的不是那么频繁,但是却也是比较有用的。桥模式主要是把抽象化和实现化分离,使软件系统的抽象化和实现化之间使用组合/聚合的方式而不是继承的关系。从而使两者可以独自的变化。
桥 顾名思义就是将两者搭建起来,使用的时候要用什么就在什么那里搭建一个桥。
// Bridge pattern -- Structural example | |
using System;
{ // Fields protected Implementor implementor;
// Properties public Implementor Implementor { set{ implementor = value; } }
// Methods virtual public void Operation() { implementor.Operation(); } }
{ // Methods abstract public void Operation(); }
{ // Methods override public void Operation() { implementor.Operation(); } }
{ // Methods override public void Operation() { Console.WriteLine("ConcreteImplementorA Operation"); } }
{ // Methods override public void Operation() { Console.WriteLine("ConcreteImplementorB Operation"); } }
/// Client test /// </summary> public class Client { public static void Main( string[] args ) { Abstraction abstraction = new RefinedAbstraction();
// Set implementation and call abstraction.Implementor = new ConcreteImplementorA(); abstraction.Operation();
// Change implemention and call abstraction.Implementor = new ConcreteImplementorB(); abstraction.Operation(); } } | |
Output
|