首页 > 其他分享 >适配器模式

适配器模式

时间:2023-06-03 21:45:39浏览次数:30  
标签:Adapter Pattern 适配器 模式 class Design interface Adaptee

The Adpative design pattern converts the interface of a class to another interface clients expect. This design pattern lets classes work together that couldn't otherwise because of incompatible interfaces.

适配器模式将类的接口转换为客户期望的另外一个接口,这种设计模式允许类一起工作,否则由于接口不兼容无法工作。

UML class diagram

组成元素

Target: defines the domain-specific interface that client uses.

Adapter:  adapts the interface Adaptee to the Target interface.

Adaptee: defines the existing interface that needs adapting.

Client:  collaborates with objects conforming to the Target interface.

The Object Adapter Design Pattern in C#

    /// <summary>
    /// The ITarget defines the domain-specific interface used by the client code.
    /// This interface needs to be implemented by the Adapter.
    /// The Client can only see the this interface i.e. the class which implements the ITarget interface.
    /// </summary>
    public interface ITarget
    {
        void Request();
    }

    /// <summary>
    /// This is the class that make two incompatible interfaces or system work together.
    /// The Adapter makes the Adapter's interface compatible with the Target's interface.
    /// </summary>
    public class Adapter : ITarget
    {

        private Adaptee adaptee = new Adaptee();
        public void Request()
        {
            //Possibly do some other work and then call sepcificRequest
            adaptee.SpecificRequest();
        }
    }

    /// <summary>
    /// The 'Adaptee' class
    /// The Adaptee contains some functionality that is required by the client,but this interface is not compatible with the client code.
    /// </summary>
    public class Adaptee
    {
        public void SpecificRequest()
        {
            Console.WriteLine("Called SpecificRequest()");
        }
    }
Object Adapter Design Pattern

 

The Class Adapter Design Pattern in C#

    /// <summary>
    /// The only difference is that the Adapter class now implements the Target interface and is inherited from the Adaptee class.
    /// In the case of the Object Adapter design pattern,the adapter has a reference to the Adaptee object, and using that reference, it will call the adaptee method.
    /// But in the case of Class Adapter Design Pattern, the adapter will call the inherited method of the Adaptee class directly.
    /// </summary>
    public class ClassAdapter : Adaptee, ITarget
    {
        public void Request()
        {
            SpecificRequest();
        }
    }
Class Adapter Design Pattern

The only difference is that the Adapter class now implements the Target interface and is inherited from the Adaptee class. In the case of the Object Adapter Design Pattern, the adapter has a reference to the Adaptee object, and using that reference, it will call the adaptee methods. But in the case of the Class Adapter Design Pattern, the adapter will call the inherited method of the Adaptee class directly.

When to use the Object Adapter Pattern and when to use the Class Adapter Pattern in C#?

It is completely baed on the situation. For Example, if you have a java class and you want to make it compatible with the dotnet class, then you need to use the object Adapter Design Pattern and the reason is it is not possible to make inheritance. On the other hand, if both classes are within the same project and using the same programming language, and if inheritance is possible, then you need to go for Class Adapter Desgin Pattern.

When to use the Adapter Design Pattern in Real-Time Applications?

  • A class needs to be reused that does not have an interface that a client required.
  • Allow a system to use classes of another system that is incompatible with it.
  • Allow communication between a new and already existing system that is independent of each other.
  • Sometimes a toolkit or class library cannot be used because its interface is incompatible with the interface required by an application.

 

标签:Adapter,Pattern,适配器,模式,class,Design,interface,Adaptee
From: https://www.cnblogs.com/qindy/p/17405663.html

相关文章

  • 桥接模式
    TheBridgedesignpatterndecouplesanabstractionfromitsimplementationsothathetwocanvaryindependently.桥接模式将抽象和实现解耦,以便两者可以独立变化。UMLClassDiagram Abstraction:definestheabstraction'sinterface;matainsareferencetoanobj......
  • 模板方法模式
    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.饿汉式(静态常量)—推荐指数:★★☆☆☆优点:不会有线程安全问题。缺点:在类加载的时候就创建对象,如果一直没使用到该对象的话,就造成了内存浪费,如果对象初始化的工作有很多,也会影响到性能。代码展示://......