Java design mode Series (vi) adapter mode

Source: Internet
Author: User

Java design mode Series (vi) adapter mode

The adapter mode transforms the interface of one class into another interface that the client expects, so that two classes that would otherwise not work together because of an interface mismatch can work together.

Structure of the adapter mode:

    1. class's Adapter mode
    2. The adapter mode of the object
First, class adapter mode

The adapter mode of the class transforms the API of the class that is adapted into the API of the target class.

As you can see, the Adaptee class does not have a sampleOperation2 () method, and the client expects this method. To enable clients to use the Adaptee class, provide an intermediate link, the class Adapter, to connect Adaptee APIs with the Target class API. Adapter and adaptee are inheritance relationships, which determines that the adapter pattern is class:

The roles involved in the pattern are:

(1) Target role: This is the expected interface. Note: Because the class adapter pattern is discussed here, the target cannot be a class.

(2) source (adapee) role: The interface that needs to be adapted now.

(3) adapter (adaper) Role: The adapter class is the core of this pattern. The adapter converts the source interface into the target interface. Obviously, this role cannot be an interface, but must be a concrete class.

Source
publicinterface Target {    /** 这是源类Adaptee也有的方法 */    publicvoidsampleOperation1();    /** 这是源类Adapteee没有的方法 */    publicvoidsampleOperation2();}

The above gives the source code of the target role, which is implemented in the form of a JAVA interface. As you can see, this interface declares two methods: SampleOperation1 () and SampleOperation2 (). The source role, Adaptee, is a concrete class that has a SampleOperation1 () method, but no SampleOperation2 () method.

publicclass Adaptee {    publicvoidsampleOperation1(){}}

The adapter role adapter extends the Adaptee and also implements the target interface. Because Adaptee does not provide the SampleOperation2 () method, and the target interface requires this method, the adapter role adapter implements this method.

publicclassextendsimplements Target {    /**     * 由于源类 Adaptee 没有方法 sampleOperation2()     * 因此适配器补充上这个方法     */    @Override    publicvoidsampleOperation2() {        //写相关的代码    }}
Second, the adapter mode of the object

As with the class's adapter pattern, the object's adapter pattern transforms the API of the class being adapted into the API of the target class, unlike the class's adapter pattern, where the object's adapter pattern is not connected to the Adaptee class using an inheritance relationship, but instead uses a delegation relationship to connect to the Adaptee class.

 Public InterfaceTarget {/** This is the source class Adaptee also have the method * /     Public void SampleOperation1();/** This is not the source class Adapteee method * /     Public void SampleOperation2();} Public classAdaptee { Public void SampleOperation1(){}} Public classAdapterImplementsTarget {PrivateAdaptee adaptee; Public Adapter(Adaptee adaptee) { This.Adaptee= Adaptee; }/** Source Class Adaptee has method SampleOperation1, so the adapter class can be delegated directly * /     Public void SampleOperation1(){ This.Adaptee.SampleOperation1(); }The /** source class Adaptee has no method SampleOperation2, so the adapter class needs to supplement this method * /     Public void SampleOperation2(){//write the relevant code}}

It is recommended to use the implementation of object adapters as much as possible, using compositing/aggregation and less inheritance.

Third, the structure of the default adaptation mode

The default adaptation mode is a "mediocre" adapter pattern.

publicinterface AbstractService {    publicvoidserviceOperation1();    publicintserviceOperation2();    publicserviceOperation3();}
publicclassimplements AbstractService{    @Override    publicvoidserviceOperation1() {    }    @Override    publicintserviceOperation2() {        return0;    }    @Override    publicserviceOperation3() {        returnnull;    }}

As you can see, the interface Abstractservice requires the definition of three methods, namely ServiceOperation1 (), ServiceOperation2 (), ServiceOperation3 (), and the abstract adapter class Serviceadapter provides mediocre implementations for all three of these methods. Therefore, any concrete class that inherits from the abstract class Serviceadapter can choose the method implementation that it needs, regardless of the other unwanted methods.

The adapter mode is intended to change the interface of the source so that it is compatible with the target interface. The default adaptation is slightly different, and it is a mediocre implementation provided to facilitate the creation of a non-mediocre adapter class.

At any time, if you are not ready to implement all of the methods of an interface, you can use the default adaptation mode to create an abstract class that gives a concrete implementation of the mediocrity of all methods. Thus, subclasses that inherit from this abstract class do not have to implement all the methods.

Advantages and disadvantages of adapter mode

Benefits of Adapter Mode

    1. Better reusability
        
      The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system. The adapter mode allows for better reuse of these features.

    2. Better extensibility

      When implementing the adapter functionality, you can invoke the features you have developed to naturally extend the functionality of the system.

Disadvantages of Adapter Mode

Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter.

Record a little bit every day. Content may not be important, but habits are important!

Java design mode Series (vi) 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.