Adapter mode: adapter from aliang. Net)

Source: Internet
Author: User
The adapter mode converts an interface of a class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together.

Name Origin

This is like a transformer, which converts one voltage to another. The domestic electricity voltage in the United States is 110 V, while that in China is 220 V. If you want to use us appliances in China, you must have a transformer that converts the 220v voltage to the 110v voltage. This transformer is an adapter.

The adapter mode is also similar to the packaging process of the goods: the actual appearance of the packaged goods is covered and changed by the packaging, so someone calls this mode the wrapper mode. In fact, we often write many such Wrapper Classes to package existing classes so that they can have interfaces that meet the needs.

Two adapter Modes

The adapter mode can be a class adapter mode or an Object Adapter mode. We will discuss the two adapter modes respectively.

Ii. Structure of the adapter mode of the class:


The figure shows that the adaptee class does not have a request method, and the customer expects this method. To enable the customer to use the adaptee class and provide an intermediate link, that is, the class adapter class, the adapter class implements the Target Interface and inherits from adaptee, the request method of the adapter Class re-encapsulates the specificrequest method of adaptee to achieve adaptation.

Because the adapter and adaptee are inherited, this determines that the adapter mode is a class.

The role involved in this adapter mode includes:

Target role: This is the interface the customer expects. Because C # does not support multi-inheritance, the target must be an interface, not a class.
Source (adaptee) role: the class to be adapted.
Adapter role: converts the source interface to the target interface. This role must be a class.

Iii. Implementation of the adapter mode of the class:

The followingProgramA schematic Implementation of the adapter mode of the class is provided:

 // Class adapter pattern -- Structural example  Using System; // "Itarget"  Interface Itarget { // Methods    Void Request ();} // "Adaptee"  Class Adaptee { // Methods    Public   Void Specificrequest () {console. writeline ( "Called specificrequest ()" );}} // "Adapter"  Class Adapter: adaptee, itarget { // Implements itarget Interface    Public   Void Request (){ // Possibly do some data manipulation      // And then call specificrequest      This . Specificrequest ();}} /**/ /// <Summary> /// Client Test  /// </Summary>  Public   Class Client { Public   Static  Void Main ( String [] ARGs ){ // Create adapter and place a request Itarget T = New Adapter (); T. Request ();}}

4. Structure of the object's adapter mode:


It can be seen that the client needs to call the request method, but adaptee does not have this method. To enable the client to use the adaptee class, a Wrapper class adapter is required. This packaging class encapsulates an adaptee instance to connect the client with adaptee. Since the adapter and adaptee are delegates, this determines that the adapter mode is object.

The role involved in this adapter mode includes:

Target role: This is the interface the customer expects. The target can be a specific or abstract class or interface.
Source (adaptee) role: the class to be adapted.
Adapter role: a adaptee object is encapsulated internally to convert the source interface to the target interface.

V. Implementation of the adapter mode of the object:

The following program provides a schematic Implementation of the adapter mode of the class:

 // Adapter pattern -- Structural example  Using System; // "Target"  Class Target { // Methods    Virtual   Public   Void Request (){ // Normal implementation goes here }} // "Adapter"  Class Adapter: Target { // Fields    Private Adaptee = New Adaptee (); // Methods    Override   Public   Void Request (){ // Possibly do some data manipulation      // And then call specificrequest Adaptee. specificrequest ();}} // "Adaptee"  Class Adaptee { // Methods    Public   Void Specificrequest () {console. writeline ( "Called specificrequest ()" );}} /**/ /// <Summary> /// Client Test  /// </Summary>  Public   Class Client { Public   Static   Void Main ( String [] ARGs ){ // Create adapter and place a request Target T = New Adapter (); T. Request ();}}

6. Under what circumstances should I use the adapter mode?

Use the adapter mode in the following situations:

1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.
2. Create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.
3. In the design (for object adapters), you need to change multiple existing subclass interfaces. If you use the class adapter mode, you need to create an adapter for each subclass, this is not practical.

VII. An example of practical application of the adapter Mode

The following program demonstrates the application of class adapter and Object Adapter.

 // Example of implementing the adapter Pattern  Using System; // Target  Public   Interface ICAR { Void Drive ();} // Direct use without Adapter  Public   Class Ctoyota: ICAR { Public   Void Drive () {console. writeline ( "Vroom, we're off in our Toyota" );}} // Adaptee  Public   Class Ccessna { Public  Void Fly () {console. writeline ( "Static Runup OK, we're off in our c172" );}} // Class Adapter  Public   Class Cdrivablecessna: ccessna, ICAR { Public   Void Drive (){ Base . Fly ();}} // Object Adapter  Public   Class Cdrivablecessna2: ICAR { Private Ccessna m_ocontained; Public Cdrivablecessna2 () {m_ocontained = New Ccessna ();} Public   Void Drive () {m_ocontained.fly ();}} // Client  Public   Class Client { Public   Static   Void Main ( String [] ARGs) {ICAR ocar = New Ctoyota (); console. Write ( "Class adapter: driving an automobile" ); Ocar. Drive (); ocar = New Cdrivablecessna (); console. Write ( "Driving a Cessna" ); Ocar. Drive (); ocar = New Cdrivablecessna2 (); console. Write ( "Object Adapter: driving a Cessna" ); Ocar. Drive ();}}

8. About the adapter Mode
Note the following when implementing the adapter mode:

1. The target interface can be omitted and the mode degrades. However, this method seems Mediocre but not mediocre. It can make adaptee unnecessary (refer to the default adapter mode ). The expression is that the parent class implements the default method, and the Child class only needs to implement its own unique method. This is similar to the template mode.
2. the adapter class can be an abstract class.
3. the adapter mode with parameters. In this way, the adapter class can return a suitable instance to the client according to the parameters.

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.