Adapter design mode

Source: Internet
Author: User

Adapter design Pattern Definition:

Convert 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.

This is not the feeling of special magic, in fact, the adapter in life everywhere, such as we are now using the notebook to give examples, we all know that the general household socket voltage is 220v, then for the notebook basic can accept the voltage is 19V, How to get the laptop to power up requires an adapter to use an input voltage of 220V to convert to a 19V output voltage to the laptop end. The adapter is to convert something that was not appropriate into the final result we wanted.

In the development of our ability to interact with external manufacturers, and sometimes we have developed a complete set of systems, can cause a lot of reasons for manufacturers to replace, then how to change the original system code in the case of the use of this new manufacturer's interface, then we need an adapter to be carried out class packaging.

Example:

First we look at the system's class diagram, we can find that our original system gets a text type of data, and the new interface gets an integral type, how to use the new interface without changing the original interface, we need to add an adapter.

Is the adapter we have added, we adapter continue to be the Iinhere class, we integrate this class after we add a Newclass object, and then we call Iinhere in fact is newclass the method in this class.

Code:

Original Factory Interface:

 Public Interface Iinhere    {        string  text        {            get;             Set ;        }              string GetText ();         void setText (string  text);    }
class Inhereclass:iinhere    {        publicstringgetset;}          Public string GetText ()        {            return  text;        }          Public void setText (string  text)        {            this. Text= text;        }    } 

New Factory Interface:

 interface   INew { int   number { get;         set  ;         int   GetNumber ();  void  setnumber (int   number); }
class newclass:inew    {        publicintgetset;}          Public int GetNumber ()        {            returnthis. number;        }          Public void setnumber (int number )        {            this. Number = number ;        }    }

Adapter Code:

classAdapter:iinhere {PrivateNewclass newclass=NewNewclass ();  Public stringText {Get;Set; }  Public stringGetText () {returnNewclass.getnumber ().        ToString (); }         Public voidSetText (stringtext) {            Try{newclass.setnumber (Convert.ToInt32 (text)); }            Catch(ArgumentException ex) {Throw NewArgumentException ("incoming error, please pass in a valid numeric character form"); }        }    }

Test Code :

Static voidMain (string[] args) {Inhereclass _inhereclass=NewInhereclass (); _inhereclass.settext ("Ten");            Console.WriteLine (_inhereclass.gettext ()); Newclass _inew=NewNewclass (); _inew.setnumber ( -);            Console.WriteLine (_inew.getnumber ()); Iinhere Adapter=NewAdapter (); Adapter.settext (" +");        Console.WriteLine (Adapter.gettext ()); }

Results:

Summarize:

We found that the original factory interface with a string type, but the new factory interface with the int type, we add a adapter class to the data packaging and processing, so that our system based on the need not to care about the new manufacturer is what type, Because eventually the adapter will be converted to the type we want to handle accordingly.

Adapter design mode

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.