Design mode-Adapter mode

Source: Internet
Author: User

Adapter Mode (Adapter): Transforms the interface of one class into another interface that the customer wants. A d a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

Applicable scenarios:

1, the existing class interface does not meet our needs;

2. Create a reusable class that can work with other unrelated classes or unforeseeable classes (that is, classes that may not necessarily be compatible with the interface);

3. Use some subclasses that already exist, without having to subclass each to match their interfaces.

General class Diagram:

What we often hear in our lives is

Power adapter, which is a device for current conversion (rectification). The existence of an adapter is to transform what is already present (the interface) into What is appropriate for our needs and can be exploited by us. In real life, the adapter is more of an intermediate layer to achieve this conversion effect.

In the general class diagram above, the Cient class eventually faces the target interface (or abstract class), which can only use subclasses that conform to this target standard, whereas the Adaptee class is an object (also known as a source role) that is adapted because it contains specific (special) operations, functions, etc. So we want to use it in our own system and convert it into a class that conforms to our standards, so that the Client class can arbitrarily choose to use the Concretetarget class or the Adatee class with special functionality in a transparent manner.

The code is implemented as follows:

Existing classes with special features but not conforming to our existing standard interface class Adaptee {public void Specificrequest () {System.out.println ("The adapted class has special features ...");}}

Target interface, or standard interface interface target {public void request ();} Specific target class, only provide normal function class Concretetarget implements target {public void request () {System.out.println ("ordinary class with normal function ...");}}

The adapter class inherits the appropriate class, while implementing the standard interface class Adapter extends adaptee implements target{public void request () {super.specificrequest ();}}

Test class public class Client {public static void main (string[] args) {//Use normal function class target concretetarget = new Concretetarget (); Concretetarget.request ();//Use a special function class, that is, the adaptation class target adapter = new adapter (); Adapter.request ();}}

Test results:

Ordinary class has normal function ... The adapted class has special functions ...

The above-implemented adapter is called the class adapter because the Adapter class inherits both the Adaptee (the adapter Class) and the Target interface (because Java does not support multiple inheritance, so this is implemented) in the Client Class, we can choose and create any kind of sub-class that meets the requirements as needed to achieve the specific functionality.

Another type of adapter pattern is an object adapter , which is not implemented using multiple inheritance or inheritance, but instead uses a Direct association , or a delegate , as follows:

The code is implemented as follows:

The adapter class, which is directly associated with the appropriate class, implements the standard interface class Adapter implements target{//directly associated with the appropriate class private adaptee adaptee; The Adapter (adaptee adaptee) {this.adaptee = Adaptee) can be passed through the constructor to the appropriate matching class object that needs to be adapted. public void request () {//This is the way to accomplish special functions using delegates this.adaptee. Specificrequest ();}}

Test class public class Client {public static void main (string[] args) {//Use normal function class target concretetarget = new Concretetarget (); Concretetarget.request ();//use special function class, that is, the adaptation class,//need to first create an object of the appropriate class as the parameter target adapter = new Adapter (new Adaptee()); Adapter.request ();}}

The test results are consistent with the above. From the class diagram we also know that the need to modify is only the internal structure of the Adapter class, that is, the Adapter itself must first have an object to be adapted to the class, and then the specific special functions entrusted to this object to implement. Using the object adapter pattern, the Adapter class (the adaptation Class) can be adapted to the functions of several different adaptive classes according to the incoming Adaptee object, and of course, at this point we can extract an interface or abstract class for multiple adaptive classes. In this case, it seems that the object adapter pattern is a little more flexible.

Summary:

1, the adapter mode is also a packaging mode , and the previous Decorator decorative mode also has the function of packaging; In addition, the object adapter pattern also has the meaning of an explicit delegate (in fact, the class adapter also has this meaning, but more implicit), then I think it with Proxy mode is a bit similar;

2, from the above point of view, Decorator, Proxy, Adapter in the realization of their own main purpose (this depends on the model of the original motive, description), can be in the packaging before and after the additional, special functions on the increase or decrease , Because I think they all have the realization meaning of the Commission inside;

3, I read the book said the adapter mode is not suitable for use in the detailed design phase, it is a compensation mode , designed to be used in the late system expansion, modification.

My related articles:

The combination of decoration mode (Decorator) and dynamic agent http://haolloyin.blog.51cto.com/1177454/338671

The Java implementation of dynamic proxy mode ( http://haolloyin.blog.51cto.com/1177454/333257 )

(proxy) The Java implementation of the agent mode http://haolloyin.blog.51cto.com/1177454/333189

This article is from the "Ant" Blog, please be sure to keep this source http://haolloyin.blog.51cto.com/1177454/346128

Design mode-Adapter mode

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.