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

简介:
[索引页]
[源码下载]


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


作者: webabcd


介绍
将一个类的接口转换成客户希望的另外一个接口。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


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


OK
[源码下载]
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344517 ,如需转载请自行联系原作者
相关文章
|
4月前
|
设计模式 前端开发 Java
【设计模式】【结构型模式】适配器模式(Adpter)
一、入门 什么是适配器模式? 适配器模式是Java中常用的结构型设计模式,它的核心作用就像现实中的电源转换器一样---让原本不兼容的两个接口能够协同工作。 为什么要用适配器模式? 假设我们需要在电商系
110 10
|
6月前
|
设计模式 Java 数据安全/隐私保护
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。
|
8月前
|
设计模式 Java 开发者
「全网最细 + 实战源码案例」设计模式——适配器模式
适配器模式(Adapter Pattern)是一种结构型设计模式,通过引入适配器类将一个类的接口转换为客户端期望的另一个接口,使原本因接口不兼容而无法协作的类能够协同工作。适配器模式分为类适配器和对象适配器两种,前者通过多重继承实现,后者通过组合方式实现,更常用。该模式适用于遗留系统改造、接口转换和第三方库集成等场景,能提高代码复用性和灵活性,但也可能增加代码复杂性和性能开销。
168 28
|
9月前
|
设计模式 JSON 前端开发
前端必须掌握的设计模式——适配器模式
适配器模式是一种结构型设计模式,用于使接口不兼容的对象能够相互合作。通过在客户端和系统之间引入一个“中间层”适配器,将不同类型的输入数据转换为系统能处理的标准格式,减轻系统的负担,提高扩展性和可维护性。例如,MacBook的扩展坞将多种接口(如HDMI、USB)转换为Type-C接口,实现多接口兼容。
|
11月前
|
设计模式 安全 Java
C# 一分钟浅谈:设计模式之单例模式
【10月更文挑战第9天】单例模式是软件开发中最常用的设计模式之一,旨在确保一个类只有一个实例,并提供一个全局访问点。本文介绍了单例模式的基本概念、实现方式(包括饿汉式、懒汉式和使用 `Lazy&lt;T&gt;` 的方法)、常见问题(如多线程和序列化问题)及其解决方案,并通过代码示例详细说明了这些内容。希望本文能帮助你在实际开发中更好地应用单例模式,提高代码质量和可维护性。
370 1
|
11月前
|
设计模式 Java
Java设计模式之适配器模式
这篇文章详细讲解了Java设计模式中的适配器模式,包括其应用场景、实现方式及代码示例。
185 0
|
12月前
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
10月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
192 3
|
9月前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
496 12
|
10月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
380 4