首页 > 其他分享 >桥接模式

桥接模式

时间:2023-06-03 21:45:18浏览次数:31  
标签:Abstraction IImplementor 桥接 模式 public implementation interface class

The Bridge design pattern decouples an abstraction from its implementation so tha the two can vary independently.

桥接模式将抽象和实现解耦,以便两者可以独立变化。

UML Class Diagram

 Abstraction: defines the abstraction's interface;matains a reference to an object of type implementor

 RefinedAbstraction: extends the interface defined by Abstraction.

 Implementor: defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface;in fact 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: implements the implementor interface and defines its concrete implementation.

Structure Code in C#

    /// <summary>
    /// This is an abstract class that contains members that define an abstract business object and its funcationality.
    /// It contains a reference to an object of type IImplementor and delegates all of the rea work to this object.
    /// It can also act as the base class for other abstraction.
    /// </summary>
    public class Abstraction
    {
        protected IImplementor? implementor;
        public IImplementor Implementor { set { implementor = value; } }
        public virtual void Operation()
        {
            implementor?.Operation();
        }
    }

    /// <summary>
    /// This is going to be a concrete class which form the Abstraction class.
    /// This Redefined Abstraction class extends the interface defined by Abstraction class.
    /// </summary>
    public class RefinedAbstraction : Abstraction
    {
        public override void Operation()
        {
            implementor?.Operation();
        }
    }
Abstraction
/// <summary>
    /// This is going to be an interface that acts as a bridge between the abstraction classes and implementer classes.
    /// The Implementor interface defines the operations for all implementation classes.
    /// It doesn't have to match the Abstraction's interface.
    /// In fact, the two interfaces can be entirely different.
    /// </summary>
    public interface IImplementor
    {
        void Operation();
    }

    /// <summary>
    /// This is going to be a class which implements the IImplementor interface and also provide the implementation details for the associated Abstraction class.
    /// Each Concrete implementation corresponds to a specific platform.
    /// </summary>
    public class ConcreteImplementor : IImplementor
    {
        public void Operation()
        {
            Console.WriteLine("ConcreteImplementor operation");
        }
    }
Implementation

When do we need to use Bridge Design Pattern in C#?

  • we want to hide the implementation details from the client.
  • we want to selection or switching of the implemtation to be at runtime rather than desgin time.
  • we want both the abstration and implenmentation classes to be extensible by the subclasses.
  • we want to avoid tight coupling binding between an abstraction and its implementation.
  • The changes in the implementation of an abtraction should have no impact on clients.
  • when we want to share an implementation among multiple objects and this should be hidden from the client.

 

标签:Abstraction,IImplementor,桥接,模式,public,implementation,interface,class
From: https://www.cnblogs.com/qindy/p/17406440.html

相关文章

  • 模板方法模式
    TheTemplateMethoddesignpatterndefinestheskeletonofanalgorithminanoperation,deferingsomestepstosubclasses.Thispatternletssubclassesredefinecertainstepsofanalgorithmwihoutchangingthealgorithm'sstructure.模板方法设计模式在操作中......
  • 创建型设计模式
    TheCreationalDesignPatternareCategorizedintotwotypes. Object-CreationalPatterns:Object-CreationalPatternsdealwithobjectcreation.Here,itdeferspartofitsobjectcreationtoanotherobject.Class-CreationalPatterns:Class-CreationalPa......
  • 策略模式
    TheStrategydesignpatterndefinesafamiliyofalgorithms,encapsulateeachone,andmaketheminterchangeable.Thispatternletsthealgorithmvaryindependentlyfromclientthatuseit.策略模式定义一系列算法,封装它,使他们可以互换,这种设计模式使算法独立于客......
  • 3月25日邓老师设计模式面试资料02
    Spring面试专题1.Spring应该很熟悉吧?来介绍下你的Spring的理解  有些同学可能会抢答,不熟悉!!!  好了,不开玩笑,面对这个问题我们应该怎么来回答呢?我们给大家梳理这个几个维度来回答1.1Spring的发展历程  先介绍Spring是怎么来的,发展中有哪些核心的节点,当前的最新版本是......
  • 大话设计模式之单例,策略,简单工厂
    基于实际面试题实现importjava.util.HashMap;importjava.util.Map;importjava.util.Random;/***用java设计一个机房环境监测系统的代码框架,机房有多种传感器,可以实时检测机房的温度、湿度、噪音等数据,*并把数据实时发送到监控中心,其中某项数据超过不健康阈值立即上......
  • 树莓派如果通过 raspi-config 关闭桌面模式 All In One
    树莓派如果通过raspi-config关闭桌面模式AllInOne树莓派设置启动模式:切换桌面模式和命令行模式DesktopCommandLineGUIvsCLI图形化界面vs命令行$sudoraspi-config$sudovim/boot/config.txt$sudoreboot$sudoshutdwon-hnowautologinde......
  • 大白话讲解数据库的三级模式(所谓的内外模式在生活中到底是什么东西?)
    具象化理解数据库的三级模式形象一点来说,把数据看做货物,数据库是仓库,模式就是表格。你有一个仓库,仓库里成千上万的货物,随便你怎么堆,你堆个正方体,堆个圆柱体,甚至随便乱堆都行,你怎么堆的叫内模式。完事你写了一张表,表上对全部货物按某个标准分类,而且标清了啥货物在哪(这个是模式内......
  • Java开发 - 让你少走弯路的Redis主从实现单节点哨兵模式
    前言前一篇中,我们讲解了Redis主从的搭建方式,其实很简单呐有木有,都是配置,连句代码都没有,是不是感觉高估了Redis主从的搭建方式?哈哈,没关系,跟着博主,包你全会。今天我们的主题是哨兵,没错,就是哨兵!有了Redis,要是没有哨兵,那真是太可惜了,哨兵是很神圣的一种重要的监测工具,有了哨兵,在Redis主......
  • 单例模式8种写法
    0.为什么需要单例模式?节省内存和计算保证结果正确方便管理使用场景:1.饿汉式(静态常量)—推荐指数:★★☆☆☆优点:不会有线程安全问题。缺点:在类加载的时候就创建对象,如果一直没使用到该对象的话,就造成了内存浪费,如果对象初始化的工作有很多,也会影响到性能。代码展示://......
  • 单例模式的运用
    目录一、介绍二、饿汉式2.1静态变量方式2.2静态代码块方式2.3枚举方式三、懒汉式3.1线程不安全方式3.2线程安全方式3.3双重检查锁方式3.4静态内部类方式四、破坏单例模式4.1序列化破坏4.2序列化破坏解决办法4.3反射破坏4.4反射破坏解决办法一、介绍单例模式:属于创建......