23 Design Modes (13): Adapter mode __ Design mode

Source: Internet
Author: User
Tags wrapper

The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that cannot work together because of an interface mismatch can work together.

  Purpose of Adapter mode

   As an example of electrical appliances, laptop plugs are generally three-phase, that is, in addition to the anode, cathode, there is a polar. In some places, there are only two poles in the socket, and there is no polar region. The power outlet does not match the power plug of the portable computer and makes the portable computer unusable. At this point, a three-phase to two-phase converter (adapter) can solve this problem, which is just like what this mode does. structure of adapter patterns

The adapter pattern has two different forms of the adapter pattern of the class and the object's adapter pattern. Class Adapter Pattern

The adapter mode of the class converts the API of the appropriate class into the target class API.

As you can see in the previous illustration, 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, a class adapter, that connects the Adaptee API with the target class API. Adapter and Adaptee are inherited relationships, which determines that the adapter pattern is class:

The roles involved in the pattern are:

destination (target) Role: This is the desired interface. Note: Because the class adapter pattern is discussed here, the target cannot be a class.

Source (adapee) Role: An Adapter interface is now required.

Adapter (adaper) Role: The adapter class is the core of this pattern. The adapter converts the source interface to the target interface. Obviously, this role can not be an interface, but must be a concrete class. Source Code

Public interface Target {
    /**
     * This is the source class Adaptee also some method *
     * public
    void SampleOperation1 (); 
    /**
     * This is the source class Adapteee does not have the method
     * * Public
    void SampleOperation2 (); 
}
The source code for the target role is given above, and the role is implemented as 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.
public class Adaptee {public
    
    void SampleOperation1 () {}

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

public class Adapter extends Adaptee implements Target {
    /**
     * Due to source class Adaptee no Method SampleOperation2 ()
     * Therefore the adapter supplements this method
     * *
    @Override public
    void SampleOperation2 () {
        //write-related code
    }

}
Object Adapter Mode

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


As you can see from the diagram above, the Adaptee class does not have a sampleOperation2 () method, and the client expects it. To enable clients to use the Adaptee class, you need to provide a wrapper (wrapper) class adapter. This wrapper class wraps a Adaptee instance so that the wrapper class can connect the Adaptee API with the target class API. Adapter and Adaptee are delegated, which determines that the adapter pattern is an object. Source Code

Public interface Target {
    /**
     * This is the source class Adaptee also some method *
     * public
    void SampleOperation1 (); 
    /**
     * This is the source class Adapteee does not have the method
     * * Public
    void SampleOperation2 (); 
}

public class Adaptee {public

    void SampleOperation1 () {}
    
}

public class Adapter {
    private adaptee adaptee;
    
    Public Adapter (Adaptee adaptee) {
        this.adaptee = adaptee;
    }
    /**
     * Source Class Adaptee has a method sampleOperation1
     * So the adapter class can be directly delegated
     /public
    void SampleOperation1 () {
        This.adaptee.sampleOperation1 ();
    }
    /**
     * Source Class Adaptee No method sampleOperation2
     * So the adapter class needs to supplement this
     method
    /public void SampleOperation2 () {
        / /write-related code
    }
}

tradeoff between class adapters and object adapters

class adapters are statically defined by using object inheritance, whereas object adapters are dynamically grouped by the way they are grouped.

for class adapters , because the adapter inherits Adaptee directly, the adapter cannot work with Adaptee subclasses because inheritance is a static relationship, and when the adapter inherits Adaptee, it is impossible to deal with adaptee subclasses anymore.

for an object adapter , an adapter can fit a variety of different sources to the same target. In other words, the same adapter can fit both the source class and its subclasses to the target interface. Because the object adapter uses the relationship of the object mix, it doesn't matter if the object type is correct and not subclasses.

for class adapters , the adapter can redefine part of the behavior of the adaptee, which is equivalent to a partial implementation of the parent class overridden by subclasses.

for object adapters , it is difficult to redefine the behavior of Adaptee, in which case you need to define a subclass of Adaptee to implement the redefinition, and then let the adapter combine the subclasses. Although it is difficult to redefine the behavior of adaptee, it is convenient to add some new behavior, and the newly added behavior can be applied to all sources at the same time.

for class adapters , only one object is introduced, and no additional references are required to get adaptee indirectly.

for object Adapters , additional references are required to get adaptee indirectly.

It is recommended that you use the implementation of the object adapter as much as possible, using compositing/aggregation and less inheritance. Of course, concrete analysis of specific problems, according to the need to choose the way to achieve, the most suitable is the best. Advantages of Adapter mode Better reusability

The system needs to use existing classes, and the interfaces of this class do not conform to the requirements of the system. Then the adapter mode allows these functions to be better reused.
Better Scalability

When implementing the adapter functionality, you can invoke the functionality that you develop to naturally extend the functionality of the system.
Disadvantages of Adapter mode

Excessive use of adapters, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the internal is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. Therefore, if it is not necessary, you can not use the adapter, but directly to the system refactoring.

Default Adapter Mode

The default adaptation (default Adapter) mode provides a default implementation for an interface that can be extended from this default implementation without having to extend from an existing interface. As a special case of adapter patterns, the default is that the adaptation mode has a particular application in the Java language.

In many cases, you must have a specific class implement an interface, but this class cannot use all of the methods specified by the interface. The usual approach is that this particular class implements all of the methods, those useful methods have to be implemented, and those that are not used have empty, mediocre implementations.

These empty methods are a waste, sometimes a kind of confusion. Unless you have seen the code for these empty methods, programmers may think that these methods are not empty. Even if he knows that some of these methods are empty, it does not necessarily know which methods are empty and which methods are not empty, unless you have seen the source code or documentation for these methods.

The default adaptation mode is a good way to handle this situation. You can design an abstract adapter class implementation interface that provides an empty method for each of the methods required by the interface. Like the "Up to star" that helped Lu, this abstract class can prevent its specific subclasses from being forced to implement empty methods. structure of default adapter mode

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

Public interface Abstractservice {public
    void ServiceOperation1 ();
    public int serviceOperation2 ();
    Public String ServiceOperation3 ();
}
public class Serviceadapter implements abstractservice{

    @Override public
    void ServiceOperation1 () {
    }

    @Override public
    int ServiceOperation2 () {return
        0;
    }

    @Override public
    String ServiceOperation3 () {return
        null;
    }

}

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

  the intent of the adapter pattern is to change the interface of the source to facilitate the compatibility of the target interface. the intent of the default adaptation is slightly different , and it is a mediocre implementation provided to facilitate the creation of a mediocre adapter class.

At any time, if you are not ready to implement all the methods of an interface, you can create an abstract class using the default fit mode, giving you a mediocre, concrete implementation of all the methods. In this way, subclasses that inherit from this abstract class do not have to implement all the methods.

Text reproduced in  http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

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.