Adapter mode: Class Adapter, Object Adapter

Source: Internet
Author: User

Adapter mode Adapter): 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.

Applicable scenarios:

1. Existing class interfaces do not meet our needs;

2. Create a reusable class so that the class can work collaboratively with other unrelated classes or unforeseen classes that may be incompatible with those interfaces;

3. Use existing subclasses if they are not subclass to match their interfaces.

Generic class diagram:

 

What we often hear in our lives is

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/142152GR-0.jpg "/>

Power adapter, which is used for current conversion and rectification. The existence of the adapter isAlready existsInterfaces)ConvertSuitable for our needs and can be used by us. In real life, the adapter is moreMiddle LayerTo achieve this conversion.

In the above general class diagram, The Cient class is ultimately facing the Target interface or abstract class), it can only use sub-classes that comply with this Target standard; the Adaptee class is an adapted object, also known as the source role). Because it contains special operations, functions, and so on, we want to use it in our own system, convert it to a class that complies with our standards, so that the Client class can choose to use the ConcreteTarget class or the Adatee class with special functions in a transparent manner.

The code is implemented as follows:

// Existing class Adaptee {public void specificRequest () {System. out. println ("the adaptive class has special functions... ");}}


// Target interface, or standard interface Target {public void request ();} // specific Target class, only provides the common function class ConcreteTarget implements Target {public void request () {System. out. println ("common classes have common functions... ");}}

 

// The Adapter class inherits the adaptive class and implements 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 the common 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:

Common classes have common functions... the adapted classes have special functions...

The above implementation adapter is calledClass AdapterBecause the Adapter class inherits both the Adaptee adaptive class) and the Target interface is implemented because Java does not support multi-inheritance ), in the Client class, we can select and create any required subclass to implement specific functions.

Another adapter mode isObject AdapterInstead of implementing multiple inheritance or inheritanceDirect association, OrDelegateClass diagram:

 

The code is implemented as follows:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1421524D6-1.jpg "/>

// Adapter class,Direct associationAdaption class, while implementing the Standard Interface class Adapter implements Target {// directly associate with the adaptation classPrivate Adaptee adaptee; // You can pass in the public Adapter (Adaptee adaptee) {this. adaptee = adaptee;} public void request () {// used hereDelegateTo complete special functions.This. adaptee. SpecificRequest ();}}


// Test class public class Client {public static void main (String [] args) {// use the common function class Target concreteTarget = new ConcreteTarget (); concreteTarget. request (); // use a special function class, that is, the adaptation class. // you must first create an object of the adaptation 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 only thing we need to modify is the internal structure of the Adapter class, that is, the Adapter itself must first have an object of the adaptive class, and then put the specific special functionsDelegateTo implement this object. The Object Adapter mode allows the Adapter class to adapt to different classes based on the introduced Adaptee object. Of course, in this case, we can extract an interface or abstract class for Multiple Adaptive classes. It seems that the Object Adapter mode is more flexible.

Summary:

1. the adapter mode is alsoPackaging Mode, Which has the same packaging function as the previous Decorator decoration mode. In addition, the Object Adapter mode also has the explicit delegation meaning, in which the class adapter actually has this meaning, ), so I think it is a bit similar to the Proxy mode;

2. From the comparison above, Decorator, Proxy, and Adapter are both in addition to the initial motivation and description of each mode in order to achieve their main purpose.Additional and special functions can be added or decreased Before and After packaging.Because I think they all have the meaning of delegate implementation in it;

3. In my book, The adapter mode is not suitable for use in the detailed design stage.Compensation ModeUsed for later System Extension and modification.

My related articles:

Decorative mode Decorator) and dynamic proxy powerful combination http://haolloyin.blog.51cto.com/1177454/338671

Java Implementation http://haolloyin.blog.51cto.com/1177454/333257 of Dynamic Proxy Mode

Proxy) Proxy mode Java Implementation 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

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.