1. 程式人生 > >Design Pattern - Bridge(C#)

Design Pattern - Bridge(C#)

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!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

這裡寫圖片描述