Net设计模式实例之桥接模式( Bridge Pattern)(3)

2、代码

1 抽象接口CustomersBase 及其具体实现类Customers
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 Add(string customer)
    {
        _dataObject.AddRecord(customer);
    }
 
    public virtual void Delete(string customer)
    {
        _dataObject.DeleteRecord(customer);
    }
 
    public virtual void Show()
    {
        _dataObject.ShowRecord();
    }
 
    public virtual void ShowA ll ()
    {
        Console .WriteLine("Customer Group: " + group);
        _dataObject.ShowA ll Records();
    }
}
 
/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
class Customers : CustomersBase
{
    // Constructor
    public Customers(string group)
 
        : base (group)
    {
    }
    public override void ShowA ll ()
    {
        // Add separator lines
        Console .WriteLine();
        Console .WriteLine("**************************" );
        base .ShowA ll ();
        Console .WriteLine("**************************" );
    }
}
 
2 抽象数据对象类DataObject 及其具体实现类CustomersData
abstract class DataObject
{
    public abstract void NextRecord();
 
    public abstract void PriorRecord();
 
    public abstract void AddRecord(string name);
 
    public abstract void DeleteRecord(string name);
 
    public abstract void ShowRecord();
 
    public abstract void ShowA ll Records();
 
}
/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
class CustomersData : DataObject
{
    private List <string > _customers = new List <string >();
    private int _current = 0;
 
    public CustomersData()
    {
        // Loaded from a database
        _customers.Add("James Hao" );
        _customers.Add(" 灵动生活" );
        _customers.Add(" **" );
        _customers.Add("* *" );
        _customers.Add("** " );
    }
 
    public override void NextRecord()
    {
        if (_current <= _customers.Count - 1)
        {
            _current++;
        }
    }
 
    public override void PriorRecord()
    {
        if (_current > 0)
        {
            _current--;
        }
    }
 
    public override void AddRecord(string customer)
    {
        _customers.Add(customer);
    }
    public override void DeleteRecord(string customer)
    {
        _customers.Remove(customer);
    }
    public override void ShowRecord()
    {
        Console .WriteLine(_customers[_current]);
    }
    public override void ShowA ll Records()
    {
        foreach (string customer in _customers)
        {
            Console .WriteLine(" " + customer);
        }
    }
}

你可能感兴趣的:(桥接模式,C#设计模式,Net设计模式,桥接模式实例,BridgePattern)