码迷,mamicode.com
首页 > 其他好文 > 详细

C# 桥模式代码

时间:2014-07-19 16:33:01      阅读:127      评论:0      收藏:0      [点我收藏+]

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

 

 

没有看懂学习中

C# 桥模式代码,布布扣,bubuko.com

C# 桥模式代码

标签:io   for   cti   代码   re   c   

原文地址:http://www.cnblogs.com/songsong2012/p/3854065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!