适配器模式(AdapterPattern)

1.适配器模式定义

使接口不兼容的对象能够相互合作
在这里插入图片描述

2.UML类图

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

3.实现代码

接口对象:

public interface IAdaptee
{
    public void AdapteeMethod();
}

public interface ITarget
{
    public void TargetMehtod();
}

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

适配器类:

public class Adapte : IAdaptee
{
    public Target _target;

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

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

测试类:

Adapte adapte = new Adapte(new Target());
adapte.AdapteeMethod();

// Target call TargetMehtod

相关推荐

  1. 适配器模式

    2024-06-18 03:58:01       25 阅读
  2. 适配器模式

    2024-06-18 03:58:01       30 阅读
  3. 适配器模式

    2024-06-18 03:58:01       25 阅读
  4. 适配器模式

    2024-06-18 03:58:01       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 03:58:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 03:58:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 03:58:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 03:58:01       20 阅读

热门阅读

  1. Aeron:Online Resources

    2024-06-18 03:58:01       8 阅读
  2. 安卓gradel下载失败解决方案

    2024-06-18 03:58:01       8 阅读
  3. 实战

    实战

    2024-06-18 03:58:01      9 阅读
  4. 力扣719.找出第K小的数对距离

    2024-06-18 03:58:01       9 阅读
  5. 10月,2024北京养老展,北京老年生活用品展览会

    2024-06-18 03:58:01       9 阅读
  6. python之面向对象编程

    2024-06-18 03:58:01       9 阅读
  7. Python2.7中os.path模块常用函数记录

    2024-06-18 03:58:01       7 阅读