Six-adapter mode for Java design mode (Adapter)

Source: Internet
Author: User

The adapter pattern transforms the interface of a class into another interface that the client expects, in order to eliminate compatibility issues with classes caused by mismatched interfaces. There are three main categories: The adapter mode of the class, the adapter mode of the object, and the adapter mode of the interface. First, let's take a look at the class's adapter pattern , first look at the class diagram:

The core idea is: There is a source class, has a method, to be adapted, the target interface when targetable, through the adapter class, the function of the source extension to targetable, see the code:

[Java]View Plaincopy
    1. public class Source {
    2. public void Method1 () {
    3. System.out.println ("This is original method!");
    4. }
    5. }
[Java]View Plaincopy
    1. Public interface Targetable {
    2. /* Same as the method in the original class */
    3. public void method1 ();
    4. /* Method for new class */
    5. public void method2 ();
    6. }
[Java]View Plaincopy
    1. public class Adapter extends Source implements targetable {
    2. @Override
    3. public void Method2 () {
    4. System.out.println ("This is the targetable method!");
    5. }
    6. }

The adapter class inherits the source class, implements the Targetable interface, and the following is the test class:

[Java]View Plaincopy
    1. public class Adaptertest {
    2. public static void Main (string[] args) {
    3. targetable target = new Adapter ();
    4. Target.method1 ();
    5. TARGET.METHOD2 ();
    6. }
    7. }

Output:

This is original method!
This is the targetable method!

Thus, the implementation class of the Targetable interface has the function of the source class.

The adapter mode of the object

The basic idea is the same as the adapter pattern of the class, only the adapter class is modified, this time does not inherit the source class, but holds an instance of the source class to solve the compatibility problem. Look at the picture:

Only need to modify the source code of the adapter class:

[Java]View Plaincopy
  1. public class Wrapper implements targetable {
  2. private source source;
  3. Public Wrapper (source source) {
  4. Super ();
  5. This.source = source;
  6. }
  7. @Override
  8. public void Method2 () {
  9. System.out.println ("This is the targetable method!");
  10. }
  11. @Override
  12. public void Method1 () {
  13. Source.method1 ();
  14. }
  15. }

Test class:

[Java]View Plaincopy
    1. public class Adaptertest {
    2. public static void Main (string[] args) {
    3. SOURCE Source = new source ();
    4. targetable target = new Wrapper (source);
    5. Target.method1 ();
    6. TARGET.METHOD2 ();
    7. }
    8. }

The output is the same as the first, except that the method of adaptation is different.

The third adapter mode is the adapter mode of the interface, the adapter of the interface is this: sometimes we write an interface with a number of abstract methods, when we write the implementation class of the interfaces, we must implement all the methods of the interface, which is obviously sometimes wasteful, because not all of the methods we need , sometimes only need some, here in order to solve this problem, we introduced the interface of the adapter pattern, with the help of an abstract class, the abstract class implementation of the interface, the implementation of all methods, and we do not deal with the original interface, only with the abstract class to get in touch, so we write a class, inherit the abstract class, Just rewrite the method we need. Look at the class diagram:

This is a good understanding, in practical development, we also often encounter this interface in the definition of too many methods, so that sometimes we do not need in some implementation classes. Look at the code:

[Java]View Plaincopy
    1. Public interface Sourceable {
    2. public void method1 ();
    3. public void method2 ();
    4. }

Abstract class Wrapper2:

[Java]View Plaincopy
    1. Public abstract class Wrapper2 implements sourceable{
    2. public void Method1 () {}
    3. public void Method2 () {}
    4. }
[Java]View Plaincopy
    1. public class SourceSub1 extends Wrapper2 {
    2. public void Method1 () {
    3. System.out.println ("The Sourceable interface ' s first sub1!");
    4. }
    5. }
[Java]View Plaincopy
    1. public class SourceSub2 extends Wrapper2 {
    2. public void Method2 () {
    3. System.out.println ("The Sourceable interface ' s second sub2!");
    4. }
    5. }
[Java]View Plaincopy
    1. public class Wrappertest {
    2. public static void Main (string[] args) {
    3. sourceable source1 = new SourceSub1 ();
    4. sourceable source2 = new SourceSub2 ();
    5. Source1.method1 ();
    6. SOURCE1.METHOD2 ();
    7. Source2.method1 ();
    8. SOURCE2.METHOD2 ();
    9. }
    10. }

Test output:

The Sourceable interface ' s first sub1!
The Sourceable interface ' s second sub2!

We have achieved the effect!

Having talked so much, summarize the application scenarios for three adapter modes:

Class's adapter pattern: When you want to convert a class to a class that satisfies another new interface , you can use the class's adapter pattern, create a new class, inherit the original class, and implement the new interface.

object's adapter pattern: When you want to convert an object to an object that satisfies another new interface, you can create a wrapper class that holds an instance of the original class, and in the method of the wrapper class, the method that invokes the instance is the line.

Interface Adapter Mode: When you do not want to implement all the methods in an interface, you can create an abstract class wrapper, implement all the methods, we write other classes, when we inherit the abstract class.

Six-adapter mode for Java design mode (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.