分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Decouple an abstraction from its implementation so that the two can vary independently.

Participants

The classes and/or objects participating in this pattern are:

  • Abstraction (BusinessObject)

    • Defines the abstraction's interface.
    • Maintains a reference to an object of type Implementor.
  • RefinedAbstraction (CustomersBusinessObject)
    • Extends the interface defined by Abstraction.
  • Implementor (DataObject)
    • Defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
  • ConcreteImplementor (CustomersDataObject)
    • Implements the Implementor interface and defines its concrete implementation.

Sample Code in C#


This structural code demonstrates the Bridge pattern which separates (decouples) the interface from its implementation. The implementation can evolve without changing clients which use the abstraction of the object.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Bridge Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Bridge Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            Abstraction ab = new RefinedAbstraction();            // Set implementation and call            ab.Implementor = new ConcreteImplementorA();            ab.Operation();            // Change implementation and call            ab.Implementor = new ConcreteImplementorB();            ab.Operation();        }        #endregion    }    /// <summary>    /// The 'Abstraction' class    /// </summary>    internal class Abstraction    {        #region Fields        /// <summary>        /// The implementor.        /// </summary>        protected Implementor implementor;        #endregion        // Property        #region Public Properties        /// <summary>        /// Sets the implementor.        /// </summary>        public Implementor Implementor        {            set            {                this.implementor = value;            }        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public virtual void Operation()        {            this.implementor.Operation();        }        #endregion    }    /// <summary>    /// The 'Implementor' abstract class    /// </summary>    internal abstract class Implementor    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public abstract void Operation();        #endregion    }    /// <summary>    /// The 'RefinedAbstraction' class    /// </summary>    internal class RefinedAbstraction : Abstraction    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            this.implementor.Operation();        }        #endregion    }    /// <summary>    /// The 'ConcreteImplementorA' class    /// </summary>    internal class ConcreteImplementorA : Implementor    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            Console.WriteLine("ConcreteImplementorA Operation");        }        #endregion    }    /// <summary>    /// The 'ConcreteImplementorB' class    /// </summary>    internal class ConcreteImplementorB : Implementor    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            Console.WriteLine("ConcreteImplementorB Operation");        }        #endregion    }}// Output:/*ConcreteImplementorA OperationConcreteImplementorB Operation*/

This real-world code demonstrates the Bridge pattern in which a Business Object abstraction is decoupled from the implementation in DataObject. The DataObject implementations can evolve dynamically without changing any clients.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Bridge Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// Startup class for Real-World Bridge Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Create RefinedAbstraction            var customers = new Customers("Chicago") { Data = new CustomersData() };            // Set ConcreteImplementor            // Exercise the bridge            customers.Show();            customers.Next();            customers.Show();            customers.Next();            customers.Show();            customers.Add("Henry Velasquez");            customers.ShowAll();        }        #endregion    }    /// <summary>    /// The 'Abstraction' class    /// </summary>    internal class CustomersBase    {        #region Fields        /// <summary>        /// The group.        /// </summary>        protected readonly string Group;        /// <summary>        /// The data object.        /// </summary>        private DataObject dataObject;        #endregion        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="CustomersBase"/> class.        /// </summary>        /// <param name="group">        /// The group.        /// </param>        public CustomersBase(string group)        {            this.Group = group;        }        #endregion        // Property        #region Public Properties        /// <summary>        /// Gets or sets the data.        /// </summary>        public DataObject Data        {            get            {                return this.dataObject;            }            set            {                this.dataObject = value;            }        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The add.        /// </summary>        /// <param name="customer">        /// The customer.        /// </param>        public virtual void Add(string customer)        {            this.dataObject.AddRecord(customer);        }        /// <summary>        /// The delete.        /// </summary>        /// <param name="customer">        /// The customer.        /// </param>        public virtual void Delete(string customer)        {            this.dataObject.DeleteRecord(customer);        }        /// <summary>        /// The next.        /// </summary>        public virtual void Next()        {            this.dataObject.NextRecord();        }        /// <summary>        /// The prior.        /// </summary>        public virtual void Prior()        {            this.dataObject.PriorRecord();        }        /// <summary>        /// The show.        /// </summary>        public virtual void Show()        {            this.dataObject.ShowRecord();        }        /// <summary>        /// The show all.        /// </summary>        public virtual void ShowAll()        {            Console.WriteLine("Customer Group: " + this.Group);            this.dataObject.ShowAllRecords();        }        #endregion    }    /// <summary>    /// The 'RefinedAbstraction' class    /// </summary>    internal class Customers : CustomersBase    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Customers"/> class.        /// </summary>        /// <param name="group">        /// The group.        /// </param>        public Customers(string group)            : base(group)        {        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The show all.        /// </summary>        public override void ShowAll()        {            // Add separator lines            Console.WriteLine();            Console.WriteLine("------------------------");            base.ShowAll();            Console.WriteLine("------------------------");        }        #endregion    }    /// <summary>    /// The 'Implementor' abstract class    /// </summary>    internal abstract class DataObject    {        #region Public Methods and Operators        /// <summary>        /// The add record.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public abstract void AddRecord(string name);        /// <summary>        /// The delete record.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public abstract void DeleteRecord(string name);        /// <summary>        /// The next record.        /// </summary>        public abstract void NextRecord();        /// <summary>        /// The prior record.        /// </summary>        public abstract void PriorRecord();        /// <summary>        /// The show all records.        /// </summary>        public abstract void ShowAllRecords();        /// <summary>        /// The show record.        /// </summary>        public abstract void ShowRecord();        #endregion    }    /// <summary>    /// The 'ConcreteImplementor' class    /// </summary>    internal class CustomersData : DataObject    {        #region Fields        /// <summary>        /// The current.        /// </summary>        private int current;        /// <summary>        /// The customers.        /// </summary>        private List<string> customers = new List<string>();        #endregion        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="CustomersData"/> class.        /// </summary>        public CustomersData()        {            // Loaded from a database             this.customers.Add("Jim Jones");            this.customers.Add("Samual Jackson");            this.customers.Add("Allen Good");            this.customers.Add("Ann Stills");            this.customers.Add("Lisa Giolani");        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The add record.        /// </summary>        /// <param name="customer">        /// The customer.        /// </param>        public override void AddRecord(string customer)        {            this.customers.Add(customer);        }        /// <summary>        /// The delete record.        /// </summary>        /// <param name="customer">        /// The customer.        /// </param>        public override void DeleteRecord(string customer)        {            this.customers.Remove(customer);        }        /// <summary>        /// The next record.        /// </summary>        public override void NextRecord()        {            if (this.current <= this.customers.Count - 1)            {                this.current++;            }        }        /// <summary>        /// The prior record.        /// </summary>        public override void PriorRecord()        {            if (this.current > 0)            {                this.current--;            }        }        /// <summary>        /// The show all records.        /// </summary>        public override void ShowAllRecords()        {            foreach (string customer in this.customers)            {                Console.WriteLine(" " + customer);            }        }        /// <summary>        /// The show record.        /// </summary>        public override void ShowRecord()        {            Console.WriteLine(this.customers[this.current]);        }        #endregion    }}// Output:/*Jim JonesSamual JacksonAllen Good------------------------Customer Group: ChicagoJim JonesSamual JacksonAllen GoodAnn StillsLisa GiolaniHenry Velasquez------------------------*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Bridge(C#)相关推荐

  1. Design Pattern - Visitor(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Memento(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Flyweight(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. WSO2 API Manager安装部署配置
  2. 数据中心未来将向“四高”演进
  3. java监听器用法(二):窗口监听器
  4. 微型计算机最早提出于,计算机基础题1、世界上第一台电子计算机诞生于A)1943年B-查字典问答网...
  5. 易学易用的Windows PowerShell(转)
  6. iOS中Lua脚本应用笔记一:脚本概念相关
  7. 软件详细设计文档模板
  8. Java实现俄罗斯方块游戏(简单版)
  9. MyEclipse10破解详解过程
  10. 人生若梦,神马都是浮云,,,,,,,
  11. Espresso:自定义Idling Resource
  12. Android 高级混淆和代码保护技术
  13. 芜湖人社×美创科技,人社局数据安全管理制度与数据分类分级建设
  14. 微信扫一扫功能扫描二维码调用外部浏览器打开指定页面实现微信中下载APP的功能
  15. 10月Tiobe编程语言排行榜更新:C语言和Java均败了!
  16. windows下文本转语音TTS库封装
  17. 数值分析9.Approximating Eigenvalues
  18. 漫步数学分析三十五——均值定理
  19. J2EE基础之易错小札
  20. 百度地图使用,小车,图标的定位

热门文章

  1. WCF中使用HttpContext.Current的办法
  2. IP Precedence DSCP、TOS
  3. 试读angular源码第三章:初始化zone
  4. Spring(1)_Bean初始化_逻辑图
  5. get_k_data 接口文档 全新的免费行情数据接口
  6. Android应用程序进程启动过程的源代码分析(2)
  7. shell脚本分析mysql慢查询日志(slow log)
  8. 用cxf开发restful风格的WebService
  9. linux正则表达式BRE
  10. javascript自动跳转