http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 8576|回复: 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();
    }
}






1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-1-9 15:52:33 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-9 16:12:19 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-9 16:24:21 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-9 17:00:00 | 显示全部楼层
感恩无私的分享与奉献
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-12-22 19:33

© 2014-2021

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