Design Pattern Framework 4.0 C# 之三 Bridge
2010-12-06 08:49
399 查看
using System;
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Bridge.NETOptimized
{
/// <summary>
/// MainApp startup class for .NET optimized
/// Bridge Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Create RefinedAbstraction
Customers customers = new Customers { Group = "Chicago" };
// Set ConcreteImplementor
customers.DataObject = new CustomersData();
// Exercise the bridge
customers.Show();
customers.Next();
customers.Show();
customers.Next();
customers.Show();
customers.Add("Henry Velasquez");
customers.ShowAll();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Abstraction' class
/// </summary>
class CustomersBase
{
// Gets or sets customer group
public string Group { get; set; }
// Gets or sets data object
public IDataObject DataObject { get; set; }
public virtual void Next()
{
DataObject.NextRecord();
}
public virtual void Prior()
{
DataObject.PriorRecord();
}
public virtual void Add(string name)
{
DataObject.AddRecord(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();
}
}
/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
class Customers : CustomersBase
{
public override void ShowAll()
{
// Add separator lines
Console.WriteLine();
Console.WriteLine("------------------------");
base.ShowAll();
Console.WriteLine("------------------------");
}
}
/// <summary>
/// The 'Implementor' interface
/// </summary>
interface IDataObject
{
void NextRecord();
void PriorRecord();
void AddRecord(string name);
void DeleteRecord(string name);
void ShowRecord();
void ShowAllRecords();
}
/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
class CustomersData : IDataObject
{
private List<string> _customers;
private int _current = 0;
// Constructor
public CustomersData()
{
// Simulate loading from database
_customers = new List<string>
{ "Jim Jones", "Samual Jackson", "Allan Good",
"Ann Stills", "Lisa Giolani" };
}
public void NextRecord()
{
if (_current <= _customers.Count - 1)
{
_current++;
}
}
public void PriorRecord()
{
if (_current > 0)
{
_current--;
}
}
public void AddRecord(string customer)
{
_customers.Add(customer);
}
public void DeleteRecord(string customer)
{
_customers.Remove(customer);
}
public void ShowRecord()
{
Console.WriteLine(_customers[_current]);
}
public void ShowAllRecords()
{
_customers.ForEach(customer =>
Console.WriteLine(" " + customer));
}
}
}
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Bridge.NETOptimized
{
/// <summary>
/// MainApp startup class for .NET optimized
/// Bridge Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Create RefinedAbstraction
Customers customers = new Customers { Group = "Chicago" };
// Set ConcreteImplementor
customers.DataObject = new CustomersData();
// Exercise the bridge
customers.Show();
customers.Next();
customers.Show();
customers.Next();
customers.Show();
customers.Add("Henry Velasquez");
customers.ShowAll();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Abstraction' class
/// </summary>
class CustomersBase
{
// Gets or sets customer group
public string Group { get; set; }
// Gets or sets data object
public IDataObject DataObject { get; set; }
public virtual void Next()
{
DataObject.NextRecord();
}
public virtual void Prior()
{
DataObject.PriorRecord();
}
public virtual void Add(string name)
{
DataObject.AddRecord(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();
}
}
/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
class Customers : CustomersBase
{
public override void ShowAll()
{
// Add separator lines
Console.WriteLine();
Console.WriteLine("------------------------");
base.ShowAll();
Console.WriteLine("------------------------");
}
}
/// <summary>
/// The 'Implementor' interface
/// </summary>
interface IDataObject
{
void NextRecord();
void PriorRecord();
void AddRecord(string name);
void DeleteRecord(string name);
void ShowRecord();
void ShowAllRecords();
}
/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
class CustomersData : IDataObject
{
private List<string> _customers;
private int _current = 0;
// Constructor
public CustomersData()
{
// Simulate loading from database
_customers = new List<string>
{ "Jim Jones", "Samual Jackson", "Allan Good",
"Ann Stills", "Lisa Giolani" };
}
public void NextRecord()
{
if (_current <= _customers.Count - 1)
{
_current++;
}
}
public void PriorRecord()
{
if (_current > 0)
{
_current--;
}
}
public void AddRecord(string customer)
{
_customers.Add(customer);
}
public void DeleteRecord(string customer)
{
_customers.Remove(customer);
}
public void ShowRecord()
{
Console.WriteLine(_customers[_current]);
}
public void ShowAllRecords()
{
_customers.ForEach(customer =>
Console.WriteLine(" " + customer));
}
}
}
相关文章推荐
- Design Pattern Framework 4.0 C# 设计模式 ef4,sliverlight4 N层项目
- Design Pattern Framework 4.0 C# 之一 Abstract
- Design Pattern Framework 4.0 C# 之二 Adapter
- Design Pattern Framework 4.0 C# 之四 Builder
- "Design Pattern FrameworkTM 3.5" c#版的.
- "Design Pattern FrameworkTM 3.5" c#版的.
- Bridge Pattern using C#
- Observer Design Pattern in C#!
- 《设计模式--基于C#的工程化实现及扩展》 Security Design Pattern 系列 3 检查点模式(Check Point)
- C#设计模式(8)——桥接模式(Bridge Pattern)
- Design Pattern Bridge 桥设计模式
- design_pattern_bridge
- design_pattern_bridge
- Bridge Design Pattern
- Bridge Design Pattern (桥模式)
- 《设计模式--基于C#的工程化实现及扩展》 Security Design Pattern 系列 4 角色模式(Role Pattern)
- Observer Design Pattern in C#!
- Framework Design Guidelines---Naming Guidelines(C# 命名规则)
- .NET Design Pattern Framework 4.5 +QQ:386184911
- Observer Design Pattern in C#!