Adapter (Adapter) mode

Source: Internet
Author: User
Tags wrapper

Structural patterns (structural pattern) describe how a class or object can be combined to form a larger structure. The structure pattern describes two different things: a class and an instance of a class. According to this point, the structure pattern can be divided into the structure pattern of the class and the structure pattern of the object.

Subsequent content will include the following architectural patterns: Adapter mode (Adapter): Match interfaces of different classes synthesis mode (composite): A tree structure of the simple and Composi Te objects Decoration mode (decorator): Add responsibilities to Objects dynamically proxy mode (proxy): An object representing another object Meta-mode (Flyweight): A fine-grained instance used for efficient sharing façade mode (façade): A single class This represents an entire SU bSYSTEM Bridge Model: Separates an object interface from its implementation
First, adapter (Adapter) mode

The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that do not match the original interface can work together.

origin of the name

This is much like a transformer (Adapter), which transforms one voltage into another. Electricity in the United States is 110V and the voltage in China is 220V. If you want to use American electrical appliances in China, you must have a transformer that converts a 220V voltage to a 110V voltage. This transformer is a adapter.

The adapter model is also similar to the packing process of the goods: the real appearance of the packaged goods is concealed and changed by the packaging, so some people call this pattern the packaging (wrapper) mode. In fact, we often write many of these wrapper classes, wrapping up some of the existing classes so that they can meet the needs of the interface.

two forms of adapter pattern

Adapter mode has both the adapter mode of the class and the adapter pattern of the object. We will discuss both of these adapter modes separately. second, the structure of the adapter pattern of the class:

As can be seen from the diagram, the Adaptee class has no request method, and the customer expects this method. In order to enable customers to use the Adaptee class, provide an intermediate link, i.e. class adapter class, adapter class implements the target interface and inherits from Adaptee, The request method of the adapter class encapsulates the Adaptee Specificrequest method and achieves the goal of adaptation.

Because adapter and adaptee are inherited relationships, this determines that the adapter pattern is class.

The roles involved in this adapter pattern include:

Destination (target) Role: This is the interface that the customer expects. Because C # does not support multiple inheritance, target must be an interface, not a class.
Source (adaptee) role: A class that needs to fit.
Adapter (Adapter) Role: Converts the source interface to the target interface. This role must be a class.
Three, the class of adapter mode schematic implementation:

The following program gives a schematic implementation of the Adapter pattern of a class://Class Adapter patterns--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 ();
}
}

IV. structure of the object's adapter pattern:


As you can see from the diagram: The client needs to invoke the request method, and Adaptee does not, and in order for the client to be able to use the Adaptee class, a wrapper (wrapper) class adapter is required. The wrapper class wraps a Adaptee instance to connect the client to the adaptee. Because adapter and adaptee are delegated, this determines that the adapter pattern is an object.

The roles involved in this adapter pattern include:

Destination (target) Role: This is the interface that the customer expects. A target can be a specific or abstract class, or it can be an interface.
Source (adaptee) role: A class that needs to fit.
Adapter (Adapter) Role: Converts the source interface into a target interface by wrapping (Wrap) A Adaptee object inside the inner package.
Five, the object of the adapter mode of the implementation of the schematic:

The following program gives a schematic implementation of a class's Adapter pattern://Adapter Patterns--structural example
Using System;

"Target"
Class Target
{
Methods
Virtual public void Request ()
{
Normal implementation goes here
}
}

"Adapter"
Class Adapter:target
{
Fields
Private Adaptee 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 ();
}
}[Dvnews_page]

Six, under what circumstances use the adapter mode

Use adapter mode in the following situations:

1, the system needs to use existing classes, and this kind of interface does not meet the needs of the system.
2. You want to create a reusable class that works with some classes that don't have much to do with each other, including classes that might be introduced in the future. These source classes do not necessarily have very complex interfaces.
3, (for object adapters) in the design, you need to change the interface of many existing subclasses, if you use the adapter mode of the class, it is necessary to make an adapter for each subclass, which is impractical.
Examples of 七、一个 practical application of adapter patterns

The following program demonstrates the application of class adapter and object 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.