Design Pattern - Bridge(C#)

本文深入探讨了桥接设计模式的概念,通过示例代码展示了如何将抽象与其实现分离,使得两者可以独立变化。桥接模式允许实施细节的改变不会影响到使用抽象的客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/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.youkuaiyun.com/jiangjunshow
这里写图片描述
电动汽车数据集:2025年3K+记录 真实电动汽车数据:特斯拉、宝马、日产车型,含2025年电池规格和销售数据 关于数据集 电动汽车数据集 这个合成数据集包含许多品牌和年份的电动汽车和插电式车型的记录,捕捉技术规格、性能、定价、制造来源、销售和安全相关属性。每一行代表由vehicle_ID标识的唯一车辆列表。 关键特性 覆盖范围:全球制造商和车型组合,包括纯电动汽车和插电式混合动力汽车。 范围:电池化学成分、容量、续航里程、充电标准和速度、价格、产地、自主水平、排放、安全等级、销售和保修。 时间跨度:模型跨度多年(包括传统和即将推出的)。 数据质量说明: 某些行可能缺少某些字段(空白)。 几个分类字段包含不同的、特定于供应商的值(例如,Charging_Type、Battery_Type)。 各列中的单位混合在一起;注意kWh、km、hr、USD、g/km和额定值。 列 列类型描述示例 Vehicle_ID整数每个车辆记录的唯一标识符。1 制造商分类汽车品牌或OEM。特斯拉 型号类别特定型号名称/变体。型号Y 与记录关联的年份整数模型。2024 电池_类型分类使用的电池化学/技术。磷酸铁锂 Battery_Capacity_kWh浮充电池标称容量,单位为千瓦时。75.0 Range_km整数表示充满电后的行驶里程(公里)。505 充电类型主要充电接口或功能。CCS、NACS、CHAdeMO、DCFC、V2G、V2H、V2L Charge_Time_hr浮动充电的大致时间(小时),上下文因充电方法而异。7.5 价格_USD浮动参考车辆价格(美元).85000.00 颜色类别主要外观颜色或饰面。午夜黑 制造国_制造类别车辆制造/组装的国家。美国 Autonomous_Level浮点自动化能力级别(例如0-5),可能包括子级别的小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值