适配器模式(AdapterPattern)

文章目录

1.适配器模式定义

使接口不兼容的对象能够相互合作

2.UML类图

  • 目标角色(Target):定义Client使用的与特定领域相关的接口。
  • 客户角色(Client):与符合Target接口的对象协同。
  • 被适配角色(Adaptee):定义一个已经存在并已经使用的接口,这个接口需要适配。
  • 适配器角色(Adapte) :适配器模式的核心。它将对被适配Adaptee角色已有的接口转换为目标角色Target匹配的接口。对Adaptee的接口与Target接口进行适配.

3.实现代码

接口对象:

csharp 复制代码
public interface IAdaptee
{
    public void AdapteeMethod();
}

public interface ITarget
{
    public void TargetMehtod();
}

public class Target : ITarget
{
    public void TargetMehtod()
    {
        Console.WriteLine("Target call TargetMehtod");
    }
}

适配器类:

csharp 复制代码
public class Adapte : IAdaptee
{
    public Target _target;

    public Adapte(Target target)
    {
        _target = target;
    }

    public void AdapteeMethod()
    {
        _target.TargetMehtod();
    }
}

测试类:

csharp 复制代码
Adapte adapte = new Adapte(new Target());
adapte.AdapteeMethod();

// Target call TargetMehtod
相关推荐
ruleslol17 小时前
java-接口适配器模式 & jsk8 接口默认实现
java·适配器模式
Yeniden3 天前
【设计模式】适配器模式大白话讲解!
设计模式·适配器模式
紫荆鱼6 天前
设计模式-适配器模式(Adapter)
c++·设计模式·适配器模式
czy878747511 天前
用C语言实现适配器模式
c语言·适配器模式
杯莫停丶12 天前
设计模式之:适配器模式
设计模式·适配器模式
WaWaJie_Ngen12 天前
【设计模式】适配器模式(Adapter)
设计模式·适配器模式
LoveXming12 天前
Chapter11—适配器模式
c++·设计模式·适配器模式·开闭原则
草莓熊Lotso14 天前
基于容器适配器模式的 Stack 与 Queue 实现:复用底层容器的优雅设计
c++·网络协议·rpc·适配器模式
Query*17 天前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式
Query*17 天前
Java 设计模式——适配器模式:从原理到3种实战的完整指南
java·设计模式·适配器模式