Adapter design mode, adapter Mode

Source: Internet
Author: User

Adapter design mode, adapter Mode
Adapter design mode definition:

Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

It sounds amazing. In fact, the adapter is everywhere in our life. For example, we use a notebook to give an example. We all know that the voltage of the outlet is 220 v, the voltage that the notebook can basically accept is 19 V, how to enable the notebook power-on requires an adapter to use an input voltage of 220V to convert it into an output voltage of 19V to the notebook end. The adapter is to convert something that is not suitable to us into the final result we want.

During development, we can interact with external manufacturers. Sometimes we have developed a complete set of systems, which may lead to factory replacement for many reasons, so how can we use the interface of this new manufacturer without changing the original system code? Then we need an adapter to pack the class.

Public interface IInhere {string text {get; set;} string getText (); void setText (string text );}

class InhereClass:IInhere    {        public string text { get; set; }        public string getText()        {            return text;        }        public void setText(string text)        {            this.text = text;        }    }

New manufacturer interface:

interface INew    {        int number        {            get;            set;        }        int getNumber();        void setNumber(int number);    }
class NewClass:INew    {        public int number { get; set; }        public int getNumber()        {            return this.number;        }        public void setNumber(int number)        {            this.number = number;        }    }

Adapter code:

Class Adapter: IInhere {private NewClass newClass = new NewClass (); public string text {get; set;} public string getText () {return newClass. getNumber (). toString ();} public void setText (string text) {try {newClass. setNumber (Convert. toInt32 (text);} catch (ArgumentException ex) {throw new ArgumentException ("input error, please input a valid number character format ");}}}

Test code:

static void Main(string[] args)        {            InhereClass _inhereClass=new InhereClass();            _inhereClass.setText("10");            Console.WriteLine(_inhereClass.getText());            NewClass _iNew = new NewClass();            _iNew.setNumber(100);            Console.WriteLine(_iNew.getNumber());            IInhere adapter=new Adapter();            adapter.setText("1000");            Console.WriteLine(adapter.getText());        }

Result:

Summary:

We found that the original factory interface uses the String type, but the new factory interface uses the Int type. We add an Adapter class in it for data packaging and processing, in this way, our system does not need to care about the type of the new manufacturer, because it will eventually be converted to the desired type through the Adapter for corresponding processing.

Related Article

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.