原文:乐在其中设计模式(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()[].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()[].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

[源码下载]

乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)的更多相关文章

  1. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  2. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  3. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  4. Java设计模式之适配器模式(Adapter Pattern)

    Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...

  5. 夜话JAVA设计模式之适配器模式(adapter pattern)

    适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...

  6. 【UE4 设计模式】适配器模式 Adapter Pattern

    概述 描述 将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper). 套路 Target(目标抽象类) 目标抽象类定义了客户所需要的接口,可 ...

  7. 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调

    模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...

  8. 设计模式 - 适配器模式(adapter pattern) 具体解释

    适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...

  9. 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释

    适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...

随机推荐

  1. 每个Android开发者必须知道的资源集锦

    英文原文:Resources every Android developer must know 随着 Android 平台持续惊人的增长,越来越多的开发人员开始工作于 Android 应用程序.而且 ...

  2. 零基Android手机嵌入式开发培训课程

    亲爱的朋友,我这里有一组当然想和大家分享,假设有兴趣在这个过程中,可以加我QQ2059055336和我联系. 课程章节分布: 第一部分 嵌入式C与数据结构篇 (20讲) 第二部分ARM裸机开发篇 (4 ...

  3. Xshell怎样登陆本地虚拟机

    Xshell怎样登陆本地虚拟机 本经验介绍了怎样使用Xshell登陆本地虚拟机,这里以centos为例.其实其它远程登陆,原理也是一样的.   工具/原料 VMware虚拟机 Xshell远程登陆工具 ...

  4. hdu1281+hdu2819(最大匹配数)

    分析:将行和列缩点,即行对应二分图的X部,列对应二分图的Y部,然后交点为连接该行和该列的一条边.匹配时每点都会把整行整列占了,因此就不会出现冲突了. 传送门:hdu1281 棋盘游戏 #include ...

  5. 10招让你成为杰出的Java程序员(转)

    如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...

  6. hdu5119(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5119 分析:dp[i][j]表示由前i个数组成异或和为j的方法数,则dp[i][j]=d[i-1][j ...

  7. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  8. HDU 4869 Turn the pokers(推理)

    HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确 ...

  9. 从零開始学android&lt;SeekBar滑动组件.二十二.&gt;

    拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object    ↳ an ...

  10. 安卓---项目中插入百度地图sdk

    百度地图 应用里面 自带地图 搜房网 下载百度地图的sdk 熟悉api 注冊百度开发人员的账号 2.12 仅仅要有一个ak就能够 高版本号须要提供应用程序的包名和签名返回开发人员的序列号 使用百度地图 ...