适配器模式(Adapter Pattern)

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

示例
有一个Message实体类,某个类对它的操作有Insert()和Get()方法。现在需要把这个类转到另一个接口,分别对应Add()和Select()方法。

  MessageModel

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Pattern.Adapter
  6. {
  7. /// <summary>
  8. /// Message实体类
  9. /// </summary>
  10. public class MessageModel
  11. {
  12. /// <summary>
  13. /// 构造函数
  14. /// </summary>
  15. /// <param name="msg">Message内容</param>
  16. /// <param name="pt">Message发布时间</param>
  17. public MessageModel(string msg, DateTime pt)
  18. {
  19. this._message = msg;
  20. this._publishTime = pt;
  21. }
  22.  
  23. private string _message;
  24. /// <summary>
  25. /// Message内容
  26. /// </summary>
  27. public string Message
  28. {
  29. get { return _message; }
  30. set { _message = value; }
  31. }
  32.  
  33. private DateTime _publishTime;
  34. /// <summary>
  35. /// Message发布时间
  36. /// </summary>
  37. public DateTime PublishTime
  38. {
  39. get { return _publishTime; }
  40. set { _publishTime = value; }
  41. }
  42. }
  43. }

  SqlMessage

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Pattern.Adapter
  6. {
  7. /// <summary>
  8. /// 源(Adaptee)角色
  9. /// Sql方式操作Message
  10. /// </summary>
  11. public class SqlMessage
  12. {
  13. /// <summary>
  14. /// 获取Message
  15. /// </summary>
  16. /// <returns></returns>
  17. public List<MessageModel> Get()
  18. {
  19. List<MessageModel> l = new List<MessageModel>();
  20. l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
  21.  
  22. return l;
  23. }
  24.  
  25. /// <summary>
  26. /// 插入Message
  27. /// </summary>
  28. /// <param name="mm">Message实体对象</param>
  29. /// <returns></returns>
  30. public bool Insert(MessageModel mm)
  31. {
  32. // 代码略
  33. return true;
  34. }
  35. }
  36. }

  IMessage

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Pattern.Adapter
  6. {
  7. /// <summary>
  8. /// 目标(Target)角色
  9. /// 操作Message的接口
  10. /// </summary>
  11. public interface IMessage
  12. {
  13. /// <summary>
  14. /// 获取Message
  15. /// </summary>
  16. /// <returns></returns>
  17. List<MessageModel> Select();
  18.  
  19. /// <summary>
  20. /// 插入Message
  21. /// </summary>
  22. /// <param name="mm">Message实体对象</param>
  23. /// <returns></returns>
  24. bool Add(MessageModel mm);
  25. }
  26. }

  Message

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Pattern.Adapter
  6. {
  7. /// <summary>
  8. /// 适配器(Adapter)角色
  9. /// 类适配器
  10. /// 把源适配到这个类
  11. /// </summary>
  12. public class Message : SqlMessage, IMessage
  13. {
  14. /// <summary>
  15. /// 获取Message
  16. /// </summary>
  17. /// <returns></returns>
  18. public List<MessageModel> Select()
  19. {
  20. return base.Get();
  21. }
  22.  
  23. /// <summary>
  24. /// 插入Message
  25. /// </summary>
  26. /// <param name="mm">Message实体对象</param>
  27. /// <returns></returns>
  28. public bool Add(MessageModel mm)
  29. {
  30. return base.Insert(mm);
  31. }
  32. }
  33. }

  Message2

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Pattern.Adapter
  6. {
  7. /// <summary>
  8. /// 适配器(Adapter)角色
  9. /// 对象适配器
  10. /// 把源适配到这个类
  11. /// </summary>
  12. public class Message2 : IMessage
  13. {
  14. private SqlMessage _sqlMessage;
  15.  
  16. /// <summary>
  17. /// 构造函数
  18. /// </summary>
  19. public Message2()
  20. {
  21. _sqlMessage = new SqlMessage();
  22. }
  23.  
  24. /// <summary>
  25. /// 获取Message
  26. /// </summary>
  27. /// <returns></returns>
  28. public List<MessageModel> Select()
  29. {
  30. return _sqlMessage.Get();
  31. }
  32.  
  33. /// <summary>
  34. /// 插入Message
  35. /// </summary>
  36. /// <param name="mm">Message实体对象</param>
  37. /// <returns></returns>
  38. public bool Add(MessageModel mm)
  39. {
  40. return _sqlMessage.Insert(mm);
  41. }
  42. }
  43. }

  Client

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. using Pattern.Adapter;
  13.  
  14. public partial class Adapter : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. IMessage m;
  19.  
  20. m = new Message();
  21. Response.Write("类适配器方式<br />");
  22. Response.Write(m.Add(new MessageModel("插入", DateTime.Now)));
  23. Response.Write("<br />");
  24. Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString());
  25. Response.Write("<br /><br />");
  26.  
  27. m = new Message2();
  28. Response.Write("对象适配器方式<br />");
  29. Response.Write(m.Add(new MessageModel("插入", DateTime.Now)));
  30. Response.Write("<br />");
  31. Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString());
  32. Response.Write("<br />");
  33. }
  34. }

  运行结果
  类适配器方式
  True
  SQL方式获取Message 2007-4-8 20:59:29

  对象适配器方式
  True
  SQL方式获取Message 2007-4-8 20:59:29

二十四种设计模式:适配器模式(Adapter Pattern)的更多相关文章

  1. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  2. 二十四种设计模式:解释器模式(Interpreter Pattern)

    解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...

  3. 二十四种设计模式:观察者模式(Observer Pattern)

    观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...

  4. 二十四种设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...

  5. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  6. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  7. 二十四种设计模式:状态模式(State Pattern)

    状态模式(State Pattern) 介绍允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它所属的类. 示例有一个Message实体类,对它的操作有Insert()和Get()方法, ...

  8. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  9. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...

随机推荐

  1. Qt调用WebService

    从网上查找Qt调用WebService的方案,需要下载三方的类库,而且需要使用好几个控制台命令,才能生成代理客户端类.因为只是简单的测试,没有采用这种方式,直接使用HTTP的Get获取网站内容,也非常 ...

  2. D - Mysterious Present

    这个题和求最长递增序列的题类似,为了能输出一组可行的数据,我还用了一点儿链表的知识. Description Peter decided to wish happy birthday to his f ...

  3. UINavigationController的使用(多视图控制器)

    一,重点 当视图控制器控制多视图时,所有页都有导航栏,如果我们有的页面不希望有导航栏咋办?网上没有搜索到结果,我探索到之后发表于此: [super navigationController].navi ...

  4. 自从学了SQL编程,哪里不会点哪里!!!

    在学习SQL编程前,先给大家分享几个段子吧,咱先乐呵乐呵! <桃花庵--程序员版> 写字楼里写字间,写字间中程序员:程序人员写程序,又将程序换酒钱: 酒醒只在屏前坐,酒醉还来屏下眠:酒醉酒 ...

  5. Android沉浸式(侵入式)标题栏(状态栏)Status(二)

     Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...

  6. 8、网页制作Dreamweaver(jQuery基础:安装、语法)

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...

  7. jquery-ui 进度条

    <!DOCTYPE html> <html> <head> <meta charset="utf8" /> <title> ...

  8. 尽可能使用 const

    前言 const 关键字是常量修辞符,如果要告知编译器某个变量在程序中不会发生改变,则可将其声明为 const. 但,对 const 关键字的认识不能仅仅停留在这一层 - 它提供了很多更强大的功能. ...

  9. SecureCRT最佳配色方法+直接修改默认配置方法 - imsoft.cnblogs

    SecureCRT默认显示效果是黑白且刺眼的主题,看起来很不舒服.经过一番搜索,总结结果如下,直接设置默认属性,设置一次,不需再改. 效果图: 具体操作方法: Options->Global O ...

  10. Java自带的keytool命令

    使用Java自带的keytool命令,在命令行生成. 1.生成服务器端私钥kserver.keystore文件 keytool -genkey -alias serverkey -validity 1 ...