二十四种设计模式:适配器模式(Adapter Pattern)
适配器模式(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