适配,即在不改变原有实现的基础上,将不兼容的接口转换为兼容的接口。

将一个类的接口转换成客户程序希望的另一个接口;Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

 

两种结构“对象适配器”和“类适配器”

interface IStack//客户期望的接口
{
    void Push(object item);
    object Pop();
    Object Peek();
}

1、对象适配器(推荐)

C#面向对象设计模式学习笔记(6) - Adapter 适配器模式(结构型模式) 

//适配对象 { ArrayList adpatee;//被适配的对象 AnotherType adpatee2;//被适配的对象 public Adapter() { adpatee = new ArrayList(); } public void Push(object item) { adpatee.add(item); } object Pop() { return adpatee.RemoveAt(adpatee.Count - 1); } object Peek() { return adpatee[adpatee.Count - 1]; } }
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2021-06-20
  • 2021-10-27
  • 2021-07-28
  • 2021-07-06
猜你喜欢
  • 2022-01-18
  • 2022-12-23
  • 2021-08-29
  • 2021-07-31
  • 2022-02-10
  • 2022-01-12
相关资源
相似解决方案