GOF pattern - Bridge

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

Frequency of use: use_medium.gif medium
UML class diagram
bridge.gif
Participants
  • 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
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.
// Bridge pattern -- Structural example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

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();
  }
}
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

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;
using System.Collections;

// "Abstraction"
class BusinessObject {
  
// Fields
  private DataObject dataObject;
  
protected string group;

  
// Constructors
  public BusinessObject( string group ) {
    
this.group = group;
  }

  
// Properties
  public DataObject DataObject {
    
set{ dataObject = value; }
    
getreturn dataObject; }
  }

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

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

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

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

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

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

// "RefinedAbstraction"

class CustomersBusinessObject : BusinessObject {
  
// Constructors
  public CustomersBusinessObject( string group ) 
    : 
base( group ){}

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

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

// "ConcreteImplementor" 
class CustomersDataObject : DataObject {
  
// Fields
  private ArrayList customers = new ArrayList();
  
private int current = 0;

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

  
// Methods
  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() {
    
foreachstring name in customers )
      Console.WriteLine( 
" " + name );
  }
}

/// <summary>
///    Client test
/// </summary>
public class BusinessApp {
  
public static void Main( string[] args ) {         
    
// Create RefinedAbstraction
    CustomersBusinessObject customers = 
      
new CustomersBusinessObject(" Chicago ");

    
// Set ConcreteImplementor
    customers.DataObject = new CustomersDataObject();

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

    customers.ShowAll();
  }
}

posted on 2005-07-07 08:58 毒菇求Buy 阅读(165) 评论(0)  编辑 收藏 引用 所属分类: Design PatternC#

只有注册用户登录后才能发表评论。
<2019年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(7)

随笔分类(133)

随笔档案(111)

文章分类(65)

文章档案(53)

相册

收藏夹(30)

BLOG

Book store

Graphics Design

搜索

最新评论

阅读排行榜

评论排行榜