苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper万能框架(V2.3-含.netcore) HttpHelper官方出品,无敌框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 最牛的爬虫类,没有爬不到只有想不到 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 523|回复: 4

[学习心得] C#桥梁模式列子

[复制链接]
发表于 2019-1-9 15:00:55 | 显示全部楼层 |阅读模式
一 、  另外一个实际应用 Bridge  模式的例子

该例子演示了业务对象(BusinessObject)通过 Bridge 模式与数据对象(DataObject)解耦。数据对象的实现可以在不改变客户端代码的情况下动态进行更换。


1.png


[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
    public virtual void Next()
    { dataObject.NextRecord(); }
    public virtual void Prior()
    { dataObject.PriorRecord(); }
    public virtual void New(string name)
    { dataObject.NewRecord(name); }
    public virtual void Delete(string name)
    { dataObject.DeleteRecord(name); }
    public virtual void Show()
    { dataObject.ShowRecord(); }
    public virtual void ShowAll()
    {
        Console.WriteLine("Customer Group: {0}", group);
        dataObject.ShowAllRecords();
    }
}
// "RefinedAbstraction"
class CustomersBusinessObject : BusinessObject
{
    // Constructors
    public CustomersBusinessObject(string group) : base(group) { }
    // Methods
    public override void ShowAll()
    {
        // Add separator lines
        Console.WriteLine();
        Console.WriteLine("------------------------");
        base.ShowAll();
        Console.WriteLine("------------------------");
    }
}
// "Implementor"
abstract class DataObject
{
    // Methods
    public abstract void NextRecord();
    public abstract void PriorRecord();
    public abstract void NewRecord(string name);
    public abstract void DeleteRecord(string name);
    public abstract void ShowRecord();
    public abstract void ShowAllRecords();
}
// "ConcreteImplementor"
class CustomersDataObject : DataObject
{
    // Fields
    private readonly ArrayList customers = new ArrayList();
    private int current;
    // 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);
    }
}
/// <summary>
/// Client test
/// </summary>
public class BusinessApp
{
    public static void Main(string[] args)
    {
        // Create RefinedAbstraction
        CustomersBusinessObject customers = new CustomersBusinessObject(" Chicago ") { DataObject = new CustomersDataObject() };
        // Set ConcreteImplementor
        // Exercise the bridge
        customers.Show();
        customers.Next();
        customers.Show();
        customers.Next();
        customers.Show();
        customers.New("Henry Velasquez");
        customers.ShowAll();
    }
}




发表于 2019-1-9 15:52:33 | 显示全部楼层
发表于 2019-1-9 16:12:19 | 显示全部楼层
发表于 2019-1-9 16:24:21 | 显示全部楼层
发表于 2019-1-9 17:00:00 | 显示全部楼层
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备17001017号-1)

GMT+8, 2019-12-7 23:06

© 2017-2018

快速回复 返回顶部 返回列表