24 Design Modes: Adapter mode (Adapter pattern)

Source: Internet
Author: User

Adapter mode (Adapter pattern)


Introduced
Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

Example
There is a message entity class that has the insert () and get () methods for its operations. Now you need to move this class to another interface, corresponding to the Add () and select () methods.

Messagemodel

Using system;using system.collections.generic;using system.text;namespace pattern.adapter{//<summary>//M Essage entity class///</summary> public class Messagemodel {//<summary>///constructor/// </summary>//<param name= "msg" >message content </param>//<param name= "PT" >message release Time &            Lt;/param> public Messagemodel (String msg, DateTime pt) {this._message = msg;        This._publishtime = pt;        } private string _message; <summary>//message content///</summary> public string Message {get            {return _message;}        set {_message = value;}        } private DateTime _publishtime;            <summary>//Message release time///</summary> public DateTime Publishtime {            get {return _publishtime;}        set {_publishtime = value;} }    }} 

  Sqlmessage

Using system;using system.collections.generic;using system.text;namespace pattern.adapter{//    <summary>    ///source (adaptee) role///    SQL Operation message///    </summary> public    class Sqlmessage    {//        < Summary>///Receive a message///</summary>//        <returns></returns> public        List <MessageModel> Get ()        {            list<messagemodel> L = new list<messagemodel> ();            L.add (New Messagemodel ("SQL Get Message", DateTime.Now));            return l;        }        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ param>        //<returns></returns> public bool Insert (messagemodel mm)        {            //code slightly            return True;}}    }

  IMessage

Using system;using system.collections.generic;using system.text;namespace pattern.adapter{//    <summary>    ///target role///interface///operation message///    </summary> public    interface IMessage    {        //< Summary>///Receive a message///</summary>//        <returns></returns>        list< Messagemodel> Select ();        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ param>        //<returns></returns> bool Add (Messagemodel mm);}    }

  Message

Using system;using system.collections.generic;using system.text;namespace pattern.adapter{//    <summary>    ////Adapter (Adapter)////class Adapter//////to the    source to this class//</summary> public class    Message:sqlmessage, IMessage {//<summary>///        Receive Message//</summary>//        <returns></ returns> public        list<messagemodel> Select ()        {            return base. Get ();        }        <summary>///INSERT message///</summary>//        <param name= "MM" >message entity object </ 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 (Adapter)///object Adapter///The source is adapted to this class///</summary> Publ        IC class Message2:imessage {private Sqlmessage _sqlmessage;  <summary>///constructor function///</summary> public Message2 () {_sqlmessage =        New Sqlmessage ();        }//<summary>//Get message///</summary>//<returns></returns>        Public list<messagemodel> Select () {return _sqlmessage.get (); }///<summary>///INSERT message///</summary>//<param name= "MM" >message solid pair Elephant </param>///<returns></returns> public bool Add (Messagemodel mm) {ret        Urn _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 Voi        d Page_Load (object sender, EventArgs e) {IMessage m;        m = new Message ();        Response.Write ("Class adapter mode <br/>");        Response.Write (M.add (New Messagemodel ("Insert", 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 ("Object adapter mode <br/>");        Response.Write (M.add (New Messagemodel ("Insert", DateTime.Now));        Response.Write ("<br/>"); Response.Write (M.select () [0]. Message + "" + m.select () [0].     Publishtime.tostring ());   Response.Write ("<br/>"); }}

  Run results
Class Adapter Mode
True
SQL Get message 2007-4-8 20:59:29

Object Adapter Mode
True
SQL Get message 2007-4-8 20:59:29

24 Design Modes: Adapter mode (Adapter pattern)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.