An initial discussion of the Java design pattern in adapter mode

Source: Internet
Author: User

1. Overview

Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible to work together those classes that would otherwise not work together because of incompatible interfaces.

2. Issues to solve

That is, the adapter mode makes it possible for those classes that cannot work together because the interface is incompatible.

Here are two very vivid examples.

3. Roles in the pattern

3.1 Destination Interface (target): the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface.

3.2 Classes that need to be adapted (Adaptee): A class or an adapter class that needs to be adapted.

3.3 Adapter (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be adapted.

4. How to Implement

(1) Class of Adapter mode (implemented with inheritance)

(2) object adapter (implemented by object combination)

Class diagram for Adapter mode

One. class's Adapter mode

[Java]View PlainCopyprint?
  1. A class that already exists that has a special function but does not conform to our existing standard interface
  2. Class Adaptee {
  3. public void Specificrequest () {
  4. System.out.println ("The adapted class has a special function ...");
  5. }
  6. }
  7. Target interface, or standard interface
  8. Interface Target {
  9. public void request ();
  10. }
  11. Specific target class, only provide normal function
  12. Class Concretetarget implements Target {
  13. public void request () {
  14. System.out.println ("ordinary class with normal function ...");
  15. }
  16. }
  17. Adapter class, inherited by the appropriate class, while implementing the standard interface
  18. Class Adapter extends Adaptee implements target{
  19. public void request () {
  20. super.specificrequest ();
  21. }
  22. }
  23. Test class public class Client {
  24. public static void Main (string[] args) {
  25. //Use common function class
  26. Target concretetarget = new Concretetarget ();
  27. Concretetarget.request ();
  28. //Using special function classes, i.e. adaptation classes
  29. Target adapter = New Adapter ();
  30. Adapter.request ();
  31. }
  32. }
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:

[Java]View PlainCopy print?
    1. Ordinary class has normal function ...
    2. The adapted class has special functions ...
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 as follows:

[Java]View PlainCopy print?
  1. Adapter class, directly associated with the appropriate class, while implementing a standard interface
  2. Class Adapter implements target{
  3. //Direct correlation is adapted class
  4. private Adaptee adaptee;
  5. //You can pass through the constructor function to the adapted class object that needs to be fitted
  6. Public Adapter (adaptee adaptee) {
  7. this.adaptee = adaptee;
  8. }
  9. public void request () {
  10. //This is the way to accomplish special functions using delegates
  11. this.adaptee.specificRequest ();
  12. }
  13. }
  14. Test class
  15. Public class Client {
  16. public static void Main (string[] args) {
  17. //Use common function class
  18. Target concretetarget = new Concretetarget ();
  19. Concretetarget.request ();
  20. //Using a special function class, i.e. an adaptation class,
  21. //need to first create an object of the appropriate class as a parameter
  22. Target adapter = New Adapter (new Adaptee ());
  23. Adapter.request ();
  24. }
  25. }
The adapter class, which is directly associated with the appropriate class, implements the standard interface class Adapter implements target{//directly associated with the adapter 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 () {//Here is the way to accomplish special functions using a delegate 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.

5. Pattern Summary

5.1 Advantages

5.1.1 through the adapter, the client can invoke the same interface, which is therefore transparent to the client. This is simpler, more straightforward, and more compact.

5.1.2 the existing classes and solves the problem that the existing class and reuse environment requirements are inconsistent.

5.1.3 Decouples the target class from the adapter class by introducing an adapter class to reuse existing adapter classes without modifying the original code.

5.1.41 object adapters can adapt multiple different adapter classes to the same target, that is, the same adapter can be used to match the matching class and its subclasses to the target interface.

5.2 Disadvantages

For an object adapter, the implementation of the replacement adapter is more complex.

5.3 Applicable Scenarios

5.3.1 systems require the use of existing classes, and the interfaces of these classes do not conform to the interface of the system.

5.3.2 wants to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.

5.3.32 classes do things the same or similar, but with different interfaces.

5.3.4 the old system developed classes have implemented some functionality, but the client can only be accessed as a different interface, but we do not want to manually change the original class.

5.3.5 uses third-party components, the component interface definition differs from its own definition, and does not want to modify its own interface, but uses the functionality of the third-party component interface.

6. Examples of adapter applications

6.1 Developers who have used ADO should have used DataAdapter, which is used as an adapter between a dataset and a data source. DataAdapter provides this adapter by mapping fill and update.

6.2 Phone Power Adapter

Transferred from: http://blog.csdn.net/jason0539

An initial discussion of the Java design pattern in 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.