首页 > 编程语言 >c#设计模式-适配器模式

c#设计模式-适配器模式

时间:2022-12-07 00:44:15浏览次数:58  
标签:Adapter c# 适配器 模式 Adaptee void 设计模式 public

 

原文网址:https://www.cnblogs.com/guyun/p/6183346.html

一、 适配器(Adapter)模式

 

适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作。

 

名称由来

 

这很像变压器(Adapter),变压器把一种电压变换成另一种电压。美国的生活用电电压是110V,而中国的电压是220V。如果要在中国使用美国电器,就必须有一个能把220V电压转换成110V电压的变压器。这个变压器就是一个Adapter。

 

Adapter模式也很像货物的包装过程:被包装的货物的真实样子被包装所掩盖和改变,因此有人把这种模式叫做包装(Wrapper)模式。事实上,大家经常写很多这样的Wrapper类,把已有的一些类包装起来,使之有能满足需要的接口。

 

适配器模式的两种形式

 

适配器模式有类的适配器模式和对象的适配器模式两种。我们将分别讨论这两种Adapter模式。

 

二、 类的Adapter模式的结构:

 

 

由图中可以看出,Adaptee类没有Request方法,而客户期待这个方法。为了使客户能够使用Adaptee类,提供一个中间环节,即类Adapter类,Adapter类实现了Target接口,并继承自Adaptee,Adapter类的Request方法重新封装了Adaptee的SpecificRequest方法,实现了适配的目的。

 

因为Adapter与Adaptee是继承的关系,所以这决定了这个适配器模式是类的。

 

该适配器模式所涉及的角色包括:

 

目标(Target)角色:这是客户所期待的接口。因为C#不支持多继承,所以Target必须是接口,不可以是类。

 

源(Adaptee)角色:需要适配的类。

 

适配器(Adapter)角色:把源接口转换成目标接口。这一角色必须是类。

 

三、 类的Adapter模式示意性实现:

 

下面的程序给出了一个类的Adapter模式的示意性的实现:

 

复制代码
//  Class Adapter pattern -- Structural example  
using System;

// "ITarget"
interface ITarget
{
    // Methods
    void Request();
}

// "Adaptee"
class Adaptee
{
    // Methods
    public void SpecificRequest()
    {
        Console.WriteLine("Called SpecificRequest()");
    }
}

// "Adapter"
class Adapter : Adaptee, ITarget
{
    // Implements ITarget interface
    public void Request()
    {
        // Possibly do some data manipulation
        // and then call SpecificRequest
        this.SpecificRequest();
    }
}

/// <summary>
/// Client test
/// </summary>
public class Client
{
    public static void Main(string[] args)
    {
        // Create adapter and place a request
        ITarget t = new Adapter();
        t.Request();
    }
}
复制代码

 

四、 对象的Adapter模式的结构:

 

 

从图中可以看出:客户端需要调用Request方法,而Adaptee没有该方法,为了使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而将客户端与Adaptee衔接起来。由于Adapter与Adaptee是委派关系,这决定了这个适配器模式是对象的。

 

该适配器模式所涉及的角色包括:

 

目标(Target)角色:这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。

 

源(Adaptee)角色:需要适配的类。

 

适配器(Adapter)角色:通过在内部包装(Wrap)一个Adaptee对象,把源接口转换成目标接口。

 

五、 对象的Adapter模式示意性实现:

 

下面的程序给出了一个类的Adapter模式的示意性的实现:

 

复制代码
// Adapter pattern -- Structural example  
using System;

// "Target"
class Target
{
    // Methods
    virtual public void Request()
    {
        // Normal implementation goes here
    }
}

// "Adapter"
class Adapter : Target
{
    // Fields
    private readonly Adaptee _adaptee = new Adaptee();

    // Methods
    override public void Request()
    {
        // Possibly do some data manipulation
        // and then call SpecificRequest
        _adaptee.SpecificRequest();
    }
}

// "Adaptee"
class Adaptee
{
    // Methods
    public void SpecificRequest()
    {
        Console.WriteLine("Called SpecificRequest()");
    }
}

/// <summary>
/// Client test
/// </summary>
public class Client
{
    public static void Main(string[] args)
    {
        // Create adapter and place a request
        Target t = new Adapter();
        t.Request();
    }
}
复制代码

 

六、 在什么情况下使用适配器模式

 

在以下各种情况下使用适配器模式:

 

  1. 系统需要使用现有的类,而此类的接口不符合系统的需要。
  2. 想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。这些源类不一定有很复杂的接口。
  3. (对对象适配器而言)在设计里,需要改变多个已有子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器,而这不太实际。

 

七、 一个实际应用Adapter模式的例子

 

下面的程序演示了Class Adapter与Object Adapter的应用。

 

复制代码
// Example of implementing the Adapter pattern
using System;

// Target
public interface ICar
{
    void Drive();
}

// Direct use without Adapter
public class CToyota : ICar
{
    public void Drive()
    {
        Console.WriteLine("Vroom Vroom, we're off in our Toyota");
    }
}

// Adaptee
public class CCessna
{
    public void Fly()
    {
        Console.WriteLine("Static runup OK, we're off in our C172");
    }
}

// Class Adapter
public class CDrivableCessna : CCessna, ICar
{
    public void Drive() { base.Fly(); }
}

// Object Adapter
public class CDrivableCessna2 : ICar
{
    private readonly CCessna _mOContained;

    public CDrivableCessna2()
    {
        _mOContained = new CCessna();
    }

    public void Drive() { _mOContained.Fly(); }
}

// Client
public class Client
{
    public static void Main(string[] args)
    {
        ICar oCar = new CToyota();

        Console.Write("Class Adapter: Driving an Automobile");
        oCar.Drive();
        oCar = new CDrivableCessna();
        Console.Write("Driving a Cessna");
        oCar.Drive();
        oCar = new CDrivableCessna2();
        Console.Write(" Object Adapter: Driving a Cessna");
        oCar.Drive();
    }
}
复制代码

 

八、 关于Adapter模式的讨论

 

Adapter模式在实现时有以下这些值得注意的地方:

 

  1. 目标接口可以省略,模式发生退化。但这种做法看似平庸而并不平庸,它可以使Adaptee不必实现不需要的方法(可以参考Default Adapter模式)。其表现形式就是父类实现缺省方法,而子类只需实现自己独特的方法。这有些像模板(Template)模式。
  2. 适配器类可以是抽象类。
  3. 带参数的适配器模式。使用这种办法,适配器类可以根据参数返还一个合适的实例给客户端。

 

标签:Adapter,c#,适配器,模式,Adaptee,void,设计模式,public
From: https://www.cnblogs.com/bruce1992/p/16961916.html

相关文章

  • cmus
    cmus安裝(Fedora)sudodnfinstallcmus使用因爲是基於終端的音樂播放器,所以cmus的所有操作都是基於鍵盤,你可以理解爲快捷鍵,也可以認爲是操作指令。基本控制cmus......
  • KaLi下载与无法clone GitHub项目
    1.下载地址链接vm安装即可账户密码默认kali**打开终端**输入sudoaptupdate&&sudoaptupgarde更新如git项目失败方式一:查询是否使用代理了gitconfig-......
  • Application entry file "electron\background.js" does not exist
    背景用Vue3+Electron开发了个PDF自由合并客户端。客户端侧代码慢慢膨胀,于是想将默认的路径src/background.js调整到electron/background.js。于是修改了:vue.c......
  • vscode ctrl+鼠标左键没反应
    目录vscodectrl+鼠标左键没反应可能的原因解决方法vscodectrl+鼠标左键没反应vscode刚开始用的好好地,有一天突然发现按ctrl+鼠标左键无法跳转到函数,我就知道我要踩坑了......
  • SpringCloud-负载均衡和通信(Ribbon、Feign)
    1.Ribbon:负载均衡(基于客户端)1.1负载均衡以及RibbonRibbon是什么?SpringCloudRibbon是基于NetflixRibbon实现的一套客户端负载均衡的工具。简单的说,Ribbon是......
  • java Collection 排序
    Integer排序Collections.sort(resList,Comparator.comparingInt(SpecialStateCountVo::getSpecialNum).reversed());String排序list=list.stream().sorted(Comparat......
  • 力扣 leetcode 797. 所有可能的路径
    问题描述给你一个有n个节点的有向无环图(DAG),请你找出所有从节点0到节点n-1的路径并输出(不要求按特定顺序)graph[i]是一个从节点i可以访问的所有节点的列表(即从节......
  • HCIA学习笔记四十九:PPPOE原理及配置
    一、DSL1.1、DSL应用场景• 数字用户线路DSL是以电话线为传输介质的传输技术。1.2、PPPoE在DSL中的应用二、PPPoE原理2.1、PPPoE报文• PPPoE报文是使用Etherne......
  • SpringCloud-Eureka服务注册中心
    1什么是EurekaNetflix在涉及Eureka时,遵循的就是API原则.Eureka是Netflix的有个子模块,也是核心模块之一。Eureka是基于REST的服务,用于定位服务,以实现云端中间件层服务发......
  • 微服务 Microservice
    使用Maven创建microserves项目黄色标记处为需要修改的地方amigosservices是项目名称mvnarchetype:generate-DgroupId=com.amigoscode.app-DartifactId=amigosserv......