适配器模式(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
相关推荐
君主黑暗1 天前
设计模式-适配器模式
设计模式·适配器模式
砍光二叉树3 天前
【设计模式】结构型-适配器模式
设计模式·适配器模式
魑魅魍魉都是鬼7 天前
Java 适配器模式(Adapter Pattern)
java·开发语言·适配器模式
彭于晏Yan9 天前
Spring Boot中适配器模式的实现方式
spring boot·设计模式·适配器模式
geovindu15 天前
python: Adapter Pattern
java·python·设计模式·适配器模式
Anurmy15 天前
设计模式之适配器模式
设计模式·适配器模式
JTCC15 天前
Java 设计模式西游篇 - 第八回:适配器模式通万国 女儿国语言无障碍
python·设计模式·适配器模式
无心水15 天前
【OpenClaw:实战部署】7、Channel子系统设计:如何优雅接入10+消息渠道?——统一接口+适配器模式实战
适配器模式
青春易逝丶16 天前
适配器模式
适配器模式
sg_knight1 个月前
适配器模式(Adapter)
python·设计模式·适配器模式·adapter