Adapter模式

      概述

Adapter模式又叫适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。

     设计

那么是如何“适配”的呢?比如,东西A想使用东西B,但是A无法使用B,怎么办?A就让东西C帮忙,A通过C就可以用B了。其实AB就好像是两节水管,C就好像是连接水管的接头。

     实现

UML图:
我的设计模式学习--Adapter模式

 标准示例:

 1我的设计模式学习--Adapter模式// Adapter
 2我的设计模式学习--Adapter模式
 3我的设计模式学习--Adapter模式// Intent: "Convert the interface of a class into another interface 
 4我的设计模式学习--Adapter模式// clients expect. Adapter lets classes work together that couldn't
 5我的设计模式学习--Adapter模式// otherwise because of incompatible interfaces". 
 6我的设计模式学习--Adapter模式
 7我的设计模式学习--Adapter模式// For further information, read "Design Patterns", p139, Gamma et al.,
 8我的设计模式学习--Adapter模式// Addison-Wesley, ISBN:0-201-63361-2
 9我的设计模式学习--Adapter模式
10


示例代码为:

  1我的设计模式学习--Adapter模式using System;
  2我的设计模式学习--Adapter模式
  3我的设计模式学习--Adapter模式namespace Example
  4


同样的方法我们来解决中国人和英国人对话的问题:
 1我的设计模式学习--Adapter模式namespace DesignPattern.Template
 2}

相关文章:

  • 2022-02-22
  • 2021-04-13
  • 2022-01-26
  • 2021-07-25
  • 2021-12-28
  • 2021-07-13
  • 2021-12-10
  • 2021-12-29
猜你喜欢
  • 2021-11-21
  • 2021-11-03
  • 2021-06-14
  • 2021-09-11
  • 2021-09-06
  • 2021-07-19
  • 2022-12-23
相关资源
相似解决方案