C # Design Pattern Series: Adapter mode (Adapter)

Source: Internet
Author: User

Adapter mode design ideas in life will often be applied to, such as when we are charging the phone, it is not possible to directly charge on the 220V power supply directly, but with the mobile phone charger to convert the voltage required to be able to charge the normal, otherwise it can not be completed charging, the charger has played a role in adaptation.

1. Introduction to Adapter Mode

1.1>, definition

The adapter pattern is to convert the interface of one class to another that the customer wants, so that those classes that would otherwise not work together because of incompatible interfaces can work together.

The adapter can be structurally divided into class adapters and object adapters. The class adapter uses an inheritance relationship to adapt the class, and the object adapter is adapted using the method of the object reference.

C # When implementing class adapters, Target can only be an interface. When implementing an object adapter, target can be an abstract class or an interface.

1.2>, frequency of Use

Medium-high

2. Class Adapter Mode structure

2.1>, Structure diagram

Class Adapter Structure diagram

Object Adapter Structure diagram

2.2>, participants

Adapter Mode participants:

◊target:client the interfaces that are used in relation to a particular domain.

◊client: Class coordinated with objects that match the target interface.

◊adaptee: A class interface that needs to be adapted.

◊adapter: Adapter, which is responsible for Adaptee interface with the target interface.

In adapter mode, the class adapter implements the function of the adapter, which joins adapter between the client and adaptee so that the client sends the request to the class adapter with the target, and then adapter calls Adaptee, This allows the client to invoke Adaptee.

3, the adapter mode structure implementation

3.1>, class adapter architecture implementation

ITarget.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.classadapter{public    interface ITarget    {        void Request ();    }}

Adaptee.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.classadapter{public    class Adaptee    {public        void Specificrequest ()        {            Console.WriteLine ("Called Specificrequest ()");        }    }}

Adapter.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.classadapter{public    class Adapter:adaptee, ITarget    {        public void Request ()        {this            . Specificrequest ();}}}    

Client.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.classadapter{public    class Client    {        static void Main (string[] args)        {            ITarget t = new Adapter ();            T.request ();}}}    

Run output:

Called Specificrequest () Press any key to continue ...

3.2>, Object Adapter architecture implementation

The client needs to invoke the request method, and Adaptee does not have the method, and in order for the client to use the Adaptee class, a class adapter is provided. This class contains an example of a adaptee that connects the client to the adaptee.

ITarget.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.objectadapter{public    interface ITarget    {        void Request ();    }}

Target.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.objectadapter{public    class Target:itarget    {public        virtual void Request ()        {            Console.WriteLine ("Called Target Request ()");        }    }}

Adaptee.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.objectadapter{public    class Adaptee    {public        void Specificrequest ()        {            Console.WriteLine ("Called Specificrequest ()");        }    }}

Adapter.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.objectadapter{public    class Adapter:target    {        private adaptee _adaptee = new Adaptee ();        public override void Request ()        {            _adaptee. Specificrequest ();}}}    

Client.cs:

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.structural.objectadapter{public    class Client    {        static void Main (string [] args)        {            ITarget t = new Adapter ();            T.request ();}}}    

4, Adapter mode practice Application

  For example, use the adapter mode solution for the power adapter to charge the phone.

4.1>, class adapter architecture implementation

ITarget.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.classadapter{public    interface ITarget    {        void Getpower ();}    }

Power.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.classadapter{public    class Power    {public        void getpower220v ()        {            Console.WriteLine ("Get 220V voltage from power supply");}        }    }

Adapter.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.classadapter{public    class Adapter:power, ITarget    {        public void Getpower ()        {this            . getpower220v ();            Console.WriteLine ("Get the phone's charging voltage! ");        }    }}

Client.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.classadapter{public    class Client    {        static void Main (string[] args)        {            Console.WriteLine ("Mobile:");            ITarget t = new Adapter ();            T.getpower ();}}}    

Run output:

Mobile: Get 220V voltage from the power source to get the charging voltage of the phone! Please press any key to continue ...

4.2>, Object Adapter architecture implementation

ITarget.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.objectadapter{public    interface ITarget    {        void Getpower ();}    }

Power.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.objectadapter{public    class Power    {public        void getpower220v ()        {            Console.WriteLine ("Get 220V voltage from power supply");}        }    }

Adapter.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.objectadapter{public    class Adapter:itarget    {public        Power _ Power;        Public Adapter (Power Power)        {            this._power = power;        }        <summary>        ////Get the desired voltage////</summary> public void Getpower ()        {            _power. getpower220v ();            Console.WriteLine ("Get the phone's charging voltage! ");        }    }}

Client.cs

Using system;using system.collections.generic;using system.linq;using system.text;namespace designpatterns.adapterpattern.practical.objectadapter{public    class Client    {        static void Main (string[] args)        {            Console.WriteLine ("Mobile:");            ITarget t = new Adapter (new Power ());            T.getpower ();}}}    

5, adapter mode application analysis

The adapter mode applies to the case:

◊ When an existing class is applied and its interface does not conform to the required condition;

◊ you want to create a reusable class that can work with the classes of the original interface;

◊ in object adaptation, object adapters can adapt their parent class interfaces when they want to match a number of subclasses.

Adapter Mode Features:

Class Adapter

◊ allows adapter to redefine part of the behavior of Adaptee. Because adapter is a subclass of Adaptee;

◊ only one object is introduced, and no additional pointers are needed to get adaptee indirectly.

Object Adapter

◊ allows one adapter to work concurrently with multiple adaptee. Adapter can also add functionality to all adaptee at once;

◊ makes it difficult to redefine the behavior of adaptee. You need to generate a subclass of Adaptee, and then let adapter introduce this subclass instead of referencing Adaptee itself.

C # Design Pattern Series: Adapter mode (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.