前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式之适配器模式

设计模式之适配器模式

作者头像
beginor
发布2020-08-06 21:14:10
3110
发布2020-08-06 21:14:10
举报

设计模式之适配器模式

结构

适配器模式
适配器模式

说明

Convert the interface of a class into another interface clients expect. An adapter lets classes work together that could not otherwise because of incompatible interfaces.

将某个类的接口转换成客户端期望的另一个接口表示。适配器模式可以消除由于接口不匹配所造成的类兼容性问题。

适用条件

  • 需要使用一个已经存在的类, 但接口与设计要求不符;
  • 需要创建一个可复用的类, 该类可以与其它不相干的类或是将来不可预见的类协同工作。

实现

代码语言:javascript
复制
interface ITarget {

   void Request();

}

class Adapter : ITarget {

   private readonly Adaptee _adaptee;

   public Adapter(Adaptee adaptee) {
      this._adaptee = adaptee;
   }
   
   void ITarget.Request() {
      this._adaptee.PerformRequest();
   }
}

class Adaptee {
   
   public void PerformRequest() {
      Console.WriteLine("Addaptee perform request.");
   }

}

class Program {

   static void Main(string[] args) {
      ITarget target = new Adapter(new Adaptee());
      target.Request();

      Console.ReadLine();
   }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 设计模式之适配器模式
    • 说明
      • 适用条件
        • 实现
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档