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

简介: 原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)[索引页][源码下载]乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabcd 介绍 将一个类的接口转换成客户希望的另外一个接口。
原文: 乐在其中设计模式(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
[源码下载]
目录
相关文章
|
4月前
|
设计模式
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
4月前
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
7月前
|
设计模式
设计模式-05建造者模式(Builder Pattern)
设计模式-05建造者模式(Builder Pattern)
|
8月前
|
设计模式 安全 Java
【设计模式】JAVA Design Patterns——Curiously Recurring Template Pattern(奇异递归模板模式)
该文介绍了一种C++的编程技巧——奇异递归模板模式(CRTP),旨在让派生组件能继承基本组件的特定功能。通过示例展示了如何创建一个`Fighter`接口和`MmaFighter`类,其中`MmaFighter`及其子类如`MmaBantamweightFighter`和`MmaHeavyweightFighter`强制类型安全,确保相同重量级的拳手之间才能进行比赛。这种设计避免了不同重量级拳手间的错误匹配,编译时会报错。CRTP适用于处理类型冲突、参数化类方法和限制方法只对相同类型实例生效的情况。
|
8月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
241 3
|
8月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
228 3
|
2月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
48 3
|
1月前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
94 12
|
2月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
86 4
|
4月前
|
API C#
C# 一分钟浅谈:文件系统编程
在软件开发中,文件系统操作至关重要。本文将带你快速掌握C#中文件系统编程的基础知识,涵盖基本概念、常见问题及解决方法。文章详细介绍了`System.IO`命名空间下的关键类库,并通过示例代码展示了路径处理、异常处理、并发访问等技巧,还提供了异步API和流压缩等高级技巧,帮助你写出更健壮的代码。
61 2