乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

  1. 云栖社区>
  2. 博客>
  3. 正文

乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

技术小胖子 2017-11-14 02:41:00 浏览675
展开阅读全文
[索引页]
[源码下载]


乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)


作者:webabcd


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


示例
有一个Message实体类,某个类对它的操作有Insert()和Get()方法。现在需要把这个类转到另一个接口,分别对应Add()和Select()方法。
MessageModel
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Adapter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Message实体类 
InBlock.gif        /// </summary> 
InBlock.gif        public class MessageModel 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="msg">Message内容</param> 
InBlock.gif                /// <param name="pt">Message发布时间</param> 
InBlock.gif                public MessageModel(string msg, DateTime pt) 
InBlock.gif                { 
InBlock.gif                        this._message = msg; 
InBlock.gif                        this._publishTime = pt; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private string _message; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Message 
InBlock.gif                { 
InBlock.gif                        get { return _message; } 
InBlock.gif                        set { _message = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private DateTime _publishTime; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message发布时间 
InBlock.gif                /// </summary> 
InBlock.gif                public DateTime PublishTime 
InBlock.gif                { 
InBlock.gif                        get { return _publishTime; } 
InBlock.gif                        set { _publishTime = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
SqlMessage
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Adapter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 源(Adaptee)角色 
InBlock.gif        /// Sql方式操作Message 
InBlock.gif        /// </summary> 
InBlock.gif        public class SqlMessage 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public List<MessageModel> Get() 
InBlock.gif                { 
InBlock.gif                        List<MessageModel> l = new List<MessageModel>(); 
InBlock.gif                        l.Add(new MessageModel("SQL方式获取Message", DateTime.Now)); 
InBlock.gif 
InBlock.gif                        return l; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 插入Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public bool Insert(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        // 代码略 
InBlock.gif                        return true
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
IMessage
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Adapter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 目标(Target)角色 
InBlock.gif        /// 操作Message的接口 
InBlock.gif        /// </summary> 
InBlock.gif        public interface IMessage 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                List<MessageModel> Select(); 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 插入Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                bool Add(MessageModel mm); 
InBlock.gif        } 
InBlock.gif}
 
Message
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Adapter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 适配器(Adapter)角色 
InBlock.gif        /// 类适配器 
InBlock.gif        /// 把源适配到这个类 
InBlock.gif        /// </summary> 
InBlock.gif        public class Message : SqlMessage, IMessage 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public List<MessageModel> Select() 
InBlock.gif                { 
InBlock.gif                        return base.Get(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 插入Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public bool Add(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        return base.Insert(mm); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Message2
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Adapter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 适配器(Adapter)角色 
InBlock.gif        /// 对象适配器 
InBlock.gif        /// 把源适配到这个类 
InBlock.gif        /// </summary> 
InBlock.gif        public class Message2 : IMessage 
InBlock.gif        { 
InBlock.gif                private SqlMessage _sqlMessage; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                public Message2() 
InBlock.gif                { 
InBlock.gif                        _sqlMessage = new SqlMessage(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public List<MessageModel> Select() 
InBlock.gif                { 
InBlock.gif                        return _sqlMessage.Get(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 插入Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public bool Add(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        return _sqlMessage.Insert(mm); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
client
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifusing Pattern.Adapter; 
InBlock.gif 
InBlock.gifpublic partial class Adapter : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                IMessage m; 
InBlock.gif 
InBlock.gif                m = new Message(); 
InBlock.gif                Response.Write("类适配器方式<br />"); 
InBlock.gif                Response.Write(m.Add(new MessageModel("插入", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString()); 
InBlock.gif                Response.Write("<br /><br />"); 
InBlock.gif 
InBlock.gif                m = new Message2(); 
InBlock.gif                Response.Write("对象适配器方式<br />"); 
InBlock.gif                Response.Write(m.Add(new MessageModel("插入", DateTime.Now))); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(m.Select()[0].Message + " " + m.Select()[0].PublishTime.ToString()); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif        } 
InBlock.gif}
 
 
运行结果
类适配器方式
True
SQL方式获取Message 2007-4-8 20:59:29 

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


参考
http://www.dofactory.com/Patterns/PatternAdapter.aspx


OK
[源码下载]
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344517,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论