适配器模式(Adapter Pattern)

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

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

  MessageModel

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Adapter
{
/// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
} private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
} private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}

  SqlMessage

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Adapter
{
/// <summary>
/// 源(Adaptee)角色
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now)); return l;
} /// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}

  IMessage

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Adapter
{
/// <summary>
/// 目标(Target)角色
/// 操作Message的接口
/// </summary>
public interface IMessage
{
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
List<MessageModel> Select(); /// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
bool Add(MessageModel mm);
}
}

  Message

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Adapter
{
/// <summary>
/// 适配器(Adapter)角色
/// 类适配器
/// 把源适配到这个类
/// </summary>
public class Message : SqlMessage, IMessage
{
/// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Select()
{
return base.Get();
} /// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Add(MessageModel mm)
{
return base.Insert(mm);
}
}
}

  Message2

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Adapter
{
/// <summary>
/// 适配器(Adapter)角色
/// 对象适配器
/// 把源适配到这个类
/// </summary>
public class Message2 : IMessage
{
private SqlMessage _sqlMessage; /// <summary>
/// 构造函数
/// </summary>
public Message2()
{
_sqlMessage = new SqlMessage();
} /// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Select()
{
return _sqlMessage.Get();
} /// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Add(MessageModel mm)
{
return _sqlMessage.Insert(mm);
}
}
}

  Client

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using Pattern.Adapter; public partial class Adapter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IMessage m; m = new Message();
Response.Write("类适配器方式<br />");
Response.Write(m.Add(new MessageModel("插入", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString());
Response.Write("<br /><br />"); m = new Message2();
Response.Write("对象适配器方式<br />");
Response.Write(m.Add(new MessageModel("插入", DateTime.Now)));
Response.Write("<br />");
Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString());
Response.Write("<br />");
}
}

  运行结果
  类适配器方式
  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. hadoop中Text类 与 java中String类的区别

    hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...

  2. php大力力 [021节]mysql表名在mac下不能大写

    2015-08-27 php大力力021.mysql表名在mac下不能大写 刚才数据库里面,phpMyAdmin狂出错. Some errors have been detected on the s ...

  3. BZOJ 3450 Easy

    注意细节啊... 和上一道差不多. #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  4. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...

  5. HTTP Live Streaming直播(iOS直播)技术分析与实现

    前些日子,也是项目需要,花了一些时间研究了HTTP Live Streaming(HLS)技术,并实现了一个HLS编码器HLSLiveEncoder,当然,C++写的.其功能是采集摄像头与麦克风,实时 ...

  6. Squid Proxy Server 3.1

    Improve the performance of your network using the caching and access control capabilitiess of squid. ...

  7. HDU 5644 (费用流)

    Problem King's Pilots (HDU 5644) 题目大意 举办一次持续n天的飞行表演,第i天需要Pi个飞行员.共有m种休假计划,每个飞行员表演1次后,需要休假Si天,并提供Ti报酬来 ...

  8. if语句解一元二次方程~

    #include<stdio.h>#include<math.h> void main(){  double a,b,c,x1,x2;  printf("请输入a&q ...

  9. Extjs学习笔记(-):ComboBox联动

    http://www.cnblogs.com/wumin97136/archive/2007/12/24/1012720.html http://examples.ext.net/ http://ex ...

  10. 转载:CancellationToken

    http://www.cnblogs.com/Abbey/archive/2011/09/12/2174208.html 最近在学习.NET中的线程同步.其中一个重要的技术叫线程的取消(中止),涉及的 ...