设计模式之桥模式

Bridge Design Pattern


definition

Decouple an abstraction from its implementation so that the two can vary independently.

Frequency of use:  nbsp;medium

UML class diagram

participants

    The classes and/or objects participating in this pattern are:

  • Abstraction   (BusinessObject)
    • defines the abstraction's interface.
    • maintains a reference to an object of type Implementor.
  • RefinedAbstraction   (CustomersBusinessObject)
    • extends the interface defined by Abstraction.
  • Implementor   (DataObject)
    • defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
  • ConcreteImplementor   (CustomersDataObject)
    • implements the Implementor interface and defines its concrete implementation.

sample code in C#

This structural code demonstrates the Bridge pattern which separates (decouples) the interface from its implementation. The implementation can evolve without changing clients which use the abstraction of the object.

Hide code

// Bridge pattern -- Structural example

using System;

namespace DoFactory.GangOfFour.Bridge.Structural
{

   // MainApp test application

   class MainApp
  {
     static void Main()
    {
      Abstraction ab = new RefinedAbstraction();

       // Set implementation and call
      ab.Implementor = new ConcreteImplementorA();
      ab.Operation();

       // Change implemention and call
      ab.Implementor = new ConcreteImplementorB();
      ab.Operation();

       // Wait for user
      Console.Read();
    }
  }

   // "Abstraction"

   class Abstraction
  {
     protected Implementor implementor;

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

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

   // "Implementor"

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

   // "RefinedAbstraction"

   class RefinedAbstraction : Abstraction
  {
     public override void Operation()
    {
      implementor.Operation();
    }
  }

   // "ConcreteImplementorA"

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

   // "ConcreteImplementorB"

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

Output
ConcreteImplementorA Operation
ConcreteImplementorB Operation




This real-world code demonstrates the Bridge pattern in which a BusinessObject abstraction is decoupled from the implementation in DataObject. The DataObject implementations can evolve dynamically without changing any clients.



// Bridge pattern -- Real World example

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Bridge.RealWorld
{

   // MainApp test application
  
   class MainApp
  {
     static void Main()
    {
       // Create RefinedAbstraction
      Customers customers =
         new Customers("Chicago");

       // Set ConcreteImplementor
      customers.Data = new CustomersData();

       // Exercise the bridge
      customers.Show();
      customers.Next();
      customers.Show();
      customers.Next();
      customers.Show();
      customers.New("Henry Velasquez");

      customers.ShowAll();

       // Wait for user
      Console.Read();
    }
  }

   // "Abstraction"

   class CustomersBase
  {
     private DataObject dataObject;
     protected string group;

     public CustomersBase( string group)
    {
       this.group = group;
    }

     // Property
     public DataObject Data
    {
       set{ dataObject = value; }
       get{ return dataObject; }
    }

     public virtual void Next()
    {
      dataObject.NextRecord();
    }

     public virtual void Prior()
    {
      dataObject.PriorRecord();
    }

     public virtual void New( string name)
    {
      dataObject.NewRecord(name);
    }

     public virtual void Delete( string name)
    {
      dataObject.DeleteRecord(name);
    }

     public virtual void Show()
    {
      dataObject.ShowRecord();
    }

     public virtual void ShowAll()
    {
      Console.WriteLine("Customer Group: " + group);
      dataObject.ShowAllRecords();
    }
  }

   // "RefinedAbstraction"

   class Customers : CustomersBase
  {
     // Constructor
     public Customers( string group) : base(group)
    {  
    }

     public override void ShowAll()
    {
       // Add separator lines
      Console.WriteLine();
      Console.WriteLine ("------------------------");
       base.ShowAll();
      Console.WriteLine ("------------------------");
    }
  }

   // "Implementor"

   abstract class DataObject
  {
     public abstract void NextRecord();
     public abstract void PriorRecord();
     public abstract void NewRecord( string name);
     public abstract void DeleteRecord( string name);
     public abstract void ShowRecord();
     public abstract void ShowAllRecords();
  }

   // "ConcreteImplementor"

   class CustomersData : DataObject
  {
     private ArrayList customers = new ArrayList();
     private int current = 0;

     public CustomersData()
    {
       // Loaded from a database
      customers.Add("Jim Jones");
      customers.Add("Samual Jackson");
      customers.Add("Allen Good");
      customers.Add("Ann Stills");
      customers.Add("Lisa Giolani");
    }

     public override void NextRecord()
    {
       if (current <= customers.Count - 1)
      {
        current++;
      }
    }

     public override void PriorRecord()
    {
       if (current > 0)
      {
        current--;
      }
    }

     public override void NewRecord( string name)
    {
      customers.Add(name);
    }

     public override void DeleteRecord( string name)
    {
      customers.Remove(name);
    }

     public override void ShowRecord()
    {
      Console.WriteLine(customers[current]);
    }

     public override void ShowAllRecords()
    {
       foreach ( string name in customers)
      {
        Console.WriteLine(" " + name);
      }
    }
  }
}

Output
Jim Jones
Samual Jackson
Allen Good

------------------------
Customer Group: Chicago
Jim Jones
Samual Jackson
Allen Good
Ann Stills
Lisa Giolani
Henry Velasquez
------------------------

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值