Adapter mode (adapter) (C #)

Source: Internet
Author: User

1. Intention

Converts an interface of a class to another interface that the customer wants. The A d a p t e r mode makes those classes that cannot work together due to interface incompatibility work together.

 

2. Applicability

Use the d a p te r mode in the following cases

• You want to use an existing class, and its interface does not meet your needs.

• You want to create a reusable class that can work collaboratively with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces.

• (Applies only to object a d a p t e r) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.

 

3. structure chart

 

4. Participants

• Ta R G E T

-Define interfaces related to specific fields used by c l I e n t.

• C l I e n t

-Collaboration with objects that comply with the ta rg e t interface.

• A d a p t e

-Define an existing interface, which must be adapted.

• A D A P T E R

-Adapt the d a p t e interface to the ta rg e t interface.

 

5. Collaboration

• The client calls some operations on the d a p t e r instance. The adapter then calls the d a p t e Operation to implement this request.

 

Class adapters and object adapters have different trade-offs.

Class adapter:

• Use a specific a d a p t e r class to match a d a p t e and ta rg e t. The result is that when we want to match a class

And all its child classes, Class a d ap t e r will not be competent.

• Enables a d a p t e r to redefine partial behavior of a d a p t e, because a d a pt e r is a subclass of a d a p t e.

• Only one object is introduced, and no extra pointer is required to indirectly obtain a d a p t e.

Object Adapter:

• One a d a p t e r is allowed with multiple a d a p t e-that is, a d a p t e itself and all its subclasses (if any)) -work simultaneously. A d a p t er can also add a function to all a d a p t e at a time.

• Making it difficult to redefine the behavior of a d a p t e. This requires generating a subclass of a d a p t e and making a d a p t e r Reference This subclass instead of referencing a d a p t e itself.

 

6. Implementation

1) when C ++ is used to implement the adapter class, the d a p t e r class should inherit the ta rg e t class in the public way, and Inherit Class a d a p t e in private mode. Therefore, the d a p t e r class should be the child type of Ta RG e t, but not the child type of a d a p t e.

2) There are many ways to insert an adapter.

 

 

7. Related Modes

The structure of mode B r I d g e is similar to that of the Object Adapter, but the starting point of mode B r I d G E is different: the objective of B r I d G E is to separate the interface parts and the implementation parts so that they can be easily and independently changed. While a d a P T E R

It means to change the interface of an existing object.

The d e c o r a t o r mode enhances the functions of other objects without changing their interfaces. Therefore, d e c o r a t o r is more transparent to applications than the adapter. The result is that d e c o r a t o r supports recursive combination, and it is impossible to simply use the adapter.

To achieve this.

Mode p r o x y defines a proxy for another object without changing its interface.

Class Diagram implemented in C:

Source code of all classes:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace simpleadaptorpattern {// declare the interface or abstract class target Abstract Public class taiget {public abstract void request ();} // declare the class adaptee to be adapted, that is, the class public class adaptee {public void specificrequest () {console. writeline ("this is the result of a special request class! ") ;}} // Declare the adapter class adapter, using the Object Adapter method, that is, a reference pointing to adaptee. Public class adaptor: taiget {private adaptee myadaptee; // This is a private reference. Public override void request () {// throw new notimplementedexception (); If (myadaptee = NULL) myadaptee = new adaptee (); myadaptee. specificrequest (); // The operations that really need to be performed.} // Code class program {static void main (string [] ARGs) {taiget target = new adaptor (); target. request (); // This is the visible interface executed by the client. Console. readkey ();}}}

Run:


Intuitive relationship diagram:

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.