C# 橋模式代碼

來源:互聯網
上載者:User

標籤:io   for   cti   代碼   re   c   

// Bridge pattern -- Real World example
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; }
  get{ return 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()
 {
  foreach( string name in customers )
   Console.WriteLine( " " + name );
 }
}
/**////

/// Client test
///

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

 

 

沒有看懂學習中

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.