Java Adapter Adapter Mode (class adapter, object adapter) Advantages and disadvantages comparison _java

Source: Internet
Author: User
Tags inheritance

Java Adapter Mode

Recent Learning Java Basics, learning the adapter when puzzled a lot of information on the Internet to check the data, for adapter a lot of information, but the comparison of this article is good, here records, we need to see.

Adapter mode is to transform the interface of a class into another interface that the client expects, so that the two classes that do not work together can not match the original interface. Functionally, classes that are incompatible with these interfaces generally have the same or similar functionality. Usually we fix this interface incompatibility by modifying the interface of the class, but the adapter pattern comes in handy if we don't want to modify the original interface for an application, or if we don't have the source code for the object at all.

Advantages of Adapters:

1, the target class and the adapter class decoupling

2, the increase of class transparency and reusability, the implementation of the specific packaging in the adapter class, for the client class is transparent, and improve the suitability of the user's reusability

3, flexibility and scalability are very good, in line with the principle of opening and closing

The roles involved in the adapter include the following:

Target: defines a specific interface that a client uses.

Client: uses the target interface to work with objects that are consistent with the target interface.

Adapter (adaptee): an existing interface that needs to be fitted.

Adapter (Adapter): An interface that is responsible for converting an Adaptee interface to target. The adapter is a concrete class, which is the core of the pattern .

The adapters are divided into class adapters and object adapters, which are described in detail below.

Class adapters

The so-called class adapter, refers to the adapter adapter inherits our Adaptee, and implements target interface targets. Because it is a single inheritance in Java, this adapter can only serve the inherited adaptee. The code is as follows:

To be fit (adaptee)

Package com.bluemsun.classadapter;

public class Person {
  private int id;
  private String name;
  /**
   * Person can now only speak English */public
  void Sayenglish () {
    System.out.println ("person can say english!");
  }
  
  /**
   * Omit Setter,getter.
   */
}

Destination Interface (target)

Package com.bluemsun.classadapter;

/**
 * Target requires person to speak English, French, Japanese. But now the person can only speak English
 * @author Administrator
 * * *
* Public Interface Target_person {
  void Sayenglish ();
  void Sayfrench ();
  void Sayjapanese ();
}

Adapters (Adapter)

Package com.bluemsun.classadapter;

/**
 * Class adapter, because the person is inherited, and only single inheritance in Java, so this adapter is only for the class of man
 * This adapter allows the class to implement the method specified by the target interface without modifying the source code.
 Author Administrator
 * */public
class Adapter_person extends person implements target_person{

  @ Override public
  void Sayfrench () {
    System.out.println ("The person can say french!")
  ;

  @Override public
  void Sayjapanese () {
    System.out.println (' person can say japanese! ');
  }

Clients (client)

Package com.bluemsun.classadapter;

public class Test {public
  static void Main (string[] args) {
    Target_person person = new Adapter_person ();
    
    Person.sayenglish ();
    Person.sayfrench ();
    Person.sayjapanese ();
  }

The simple code above demonstrates the role of the class adapter. As we began to say, this adapter adapter can only serve the class of person. At this point you might think, if I need to fit a lot of classes, do I need to write a adapter for each class that needs to fit? Is there a more flexible way? The answer is: Yes! Is the object adapter we're talking about here.

Object adapters

The object adapter, simply put, is that the adapter implements our target interface, but does not inherit the class that needs to be fitted. Instead, the adapter's constructor is passed in to match the class that needs to be adapted. The code is as follows: (Target,adaptee ibid.)

Adapters (Adapter)

Package com.bluemsun.objectdapter;

Import Com.bluemsun.classadapter.Person;
Import Com.bluemsun.classadapter.Target_Person;

/**
 * Object adapters, unlike class adapters: Object adapters can fit multiple sources to the target
 * @author Administrator */Public
class Adapter_ Person implements target_person{  //Only implements the target interface private person person
  ;
  
  In the constructor, Adaptee class person is passed in to the public
  Adapter_person {
    This.person = person;

  Implement the Sayenglish () in the target interface-call Sayenglish () in Adaptee ()
  @Override public
  void Sayenglish () {
    This.person.sayEnglish ();
  }

  Implement other methods in the interface
  @Override public
  void Sayfrench () {
    System.out.println ("person can say french!");
  }

  @Override public
  void Sayjapanese () {
    System.out.println (' person can say japanese! ');
  }

Clients (client)

Package com.bluemsun.objectdapter;

Import Com.bluemsun.classadapter.Person;
Import Com.bluemsun.classadapter.Target_Person;

public class Test {public
  static void Main (string[] args) {
    Target_person person = new Adapter_person ( ));
    
    Person.sayenglish ();
    Person.sayfrench ();
    Person.sayjapanese ();
  }

An object adapter can fit multiple classes with a suitable fit. You only need to pass different matching classes in the adapter construction method. With flexibility.

Advantages of class Adapters:

1, because the adapter class is a subclass of the adapter class, it is possible to replace some adapters in the adaptor class to make the adapter more flexible.

Disadvantages of class Adapters:

1, for Java, C # and other languages that do not support multiple inheritance, one at a time can only fit one adapter class, and the target abstract class can only be an interface, not a class, its use has certain limitations, can not be a suitable for the class and his subclasses at the same time fit to the target interface.

Advantages of Object adapters:

1, to adapt to a number of different adapters to the same goal, that is, the same adapter can be the adaptor class and his subclasses are suitable to the target interface.

Disadvantages of object adapters:

1, compared with the class adapter mode, it is not easy to replace the adaptor class method.

  Thank you for reading, I hope to help you, thank you for your support for this site!

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.