欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 适配器模式(AdapterPattern)

适配器模式(AdapterPattern)

2024/10/25 2:19:05 来源:https://blog.csdn.net/weixin_45136016/article/details/139662665  浏览:    关键词:适配器模式(AdapterPattern)

文章目录

  • 1.适配器模式定义
  • 2.UML类图
  • 3.实现代码

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com