java-pattern 之 读书笔记7 ---Bridge


bridge模式就是所谓的桥模式,这种模式应用的不是那么频繁,但是却也是比较有用的。桥模式主要是把抽象化和实现化分离,使软件系统的抽象化和实现化之间使用组合/聚合的方式而不是继承的关系。从而使两者可以独自的变化。
桥 顾名思义就是将两者搭建起来,使用的时候要用什么就在什么那里搭建一个桥。

// Bridge pattern -- Structural example

using System;

// "Abstraction"


class
Abstraction

{
  // Fields
  protected Implementor implementor;

 
// Properties
  public Implementor Implementor
  {
    set{ implementor = value; }
  }

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

// "Implementor"


abstract
class Implementor

{
  // Methods
  abstract public void Operation();
}

// "RefinedAbstraction"


class
RefinedAbstraction : Abstraction

{
  // Methods
  override public void Operation()
  {
    implementor.Operation();
  }
}

// "ConcreteImplementorA"


class
ConcreteImplementorA : Implementor

{
  // Methods
  override public void Operation()
  {
    Console.WriteLine("ConcreteImplementorA Operation");
  }
}

// "ConcreteImplementorB"


class
ConcreteImplementorB : Implementor

{
  // Methods
  override public void Operation()
  {
    Console.WriteLine("ConcreteImplementorB Operation");
  }
}

///
<summary>

/// 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
ConcreteImplementorA Operation
ConcreteImplementorB Operation
posted on 2005-08-02 16:35  Michael J  阅读(297)  评论(0编辑  收藏  举报