The best mode for the right adapter

Source: Internet
Author: User

I used to have a pair of headset millet in front of me. But I did not cherish, until she fell into my glass of water, I regret mo, the most painful thing in the world is this. If God gave me another chance, I would say to you: I should buy a waterproof headset. yesterday evening back to the dormitory, can't help the temptation of food, big night, we can not eat, see also OK ah. So, Baidu search "the tongue on the Chinese", look at, feel thirsty. So rub rub hurriedly find water to drink, drink when still don't forget to put mobile phone in hand, earphone inserted in ear, then, clapped, my earphone cool bath, may be because the weather is hotter, earphone also want cool cool.

I went to the market did not find 2.5 of headphones, basically 3.5 interface, no way I just bought a 3.5-interface headphones, the boss told you: "I give you an adapter" This is not a problem to conquer. The headset on the 3.5 interface was not available on my phone because it was not designed according to the 2.5 interface, and now I want to use the headset. Then there is the "adapter (Adapter)" This one thing came out. The definition of the adapter pattern: Transforms the interface of a class into the second interface expected by the client, so that those classes that would otherwise not work together because of incompatible interfaces can work together.

My headphone jack is 2,5, unlucky bear children, bought the wrong, bought a 3.5, how to do? This time I need an "adapter" to help me solve this problem.

Today's design patterns start with our headphones---adapter mode. First take a look at the structure of our adapters:

Some of the above class diagrams are described:

Destination interface (target): the interface that the customer expects.

A target can be a detailed or abstract class, or it can be an interface.


Classes that need to be adapted (Adaptee): Classes or adapter classes that need to be adapted.
Adapter (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be adapted.

Next, take a look at our code implementation in the example above:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{    class program    {        static void Main (string[] args)        {            target target = New Adapter ();            Target. Provide2_5 ();            Console.read ();        }    }    Targe here is equivalent to 2.5 jacks for headphones    class Target    {public      virtual void  provide2_5 ()      {          Console.WriteLine ("I am the interface of 2.5");}     }    Adaptee here is equivalent to 3.5 jack headphone    class Adaptee    {public       virtual void  provide3_5 ()      {        Console.WriteLine ("I am the interface of 3.5");}    }    Adapter here corresponds to the converter    class Adapter:target    {       private adaptee adaptee = new Adaptee ();       public override void Provide2_5 ()      {           adaptee. Provide3_5 ();}}}    
programming as life, the right is the best. Eagle hit the sky, fish xiang diving bottom. Nature has become rich and colorful for them. Just because they found the right place for themselves. The same is true of human beings, to find their own position, your life will be filled with comfort and righteousness.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

The best mode for the right adapter

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.