Design mode: Adapter mode (Adapter)

Source: Internet
Author: User

? Adapter mode: Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for classes that are not working together because of incompatible interfaces.
The adapter mode has two different forms: the class Adapter mode and the object adapter mode.

Class Adapter
The class adapter pattern transforms the API of the class that is appropriate to the target class

? The roles involved in the adapter pattern:

    1. Target Role: This is the expected interface.
    2. source Role (Adaptee): Interfaces that need to be adapted
    3. Adapter Role (Adapter): The adapter class is at the heart 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.

To give a simple example:
1 target role

publicinterface Target{    publicvoidrequest();}

2 Source Roles

publicclass Adaptee{    publicvoidspecificRequest()    {        System.out.println("被适配的类Adaptee");    }}

3 Adapter Role (class adapter determines that target cannot be a class, only interface, because Java does not support multiple inheritance relationships)

publicclass Adapter extends Adaptee implements Target{    @Override    publicvoidrequest()    {        super.specificRequest();    }}

4 test Code

        new Adapter();        adapter.request();

Object Adapter
To give a simple example

    1. Target role (IBID., where target role target can be a class)
    2. Source Role (IBID.)
    3. Adapter role
publicclass ObjectAdapter implements Target{    private Adaptee adaptee;    publicObjectAdapter(Adaptee adaptee)    {        this.adaptee = adaptee;    }    @Override    publicvoidrequest()    {        this.adaptee.specificRequest();    }}

4. Test the code:

        new ObjectAdapter(new Adaptee());        adapter.request();

Trade-offs between class adapters and object adapters

    • The way that class adapters use object integration is statically defined, and the way object adapters use object composition is a dynamic combination.
    • 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 no longer possible to take the subclass of Adaptee. For object adapters, an adapter can adapt many different sources to the same target. In other words, the same adapter can adapt the source class and its subclasses to the target interface. Because the object adapter is a combination of objects, it doesn't matter if the object type is correct.
    • For class adapters, the adapter can redefine part of the behavior of Adaptee and want to override the partial implementation of the parent class when the applies class overrides it. 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 have the adapter group the subclasses. Although it is difficult to redefine the behavior of adaptee, it is convenient to add some new behaviors, and the newly added behavior can be applied to all sources at the same time.
    • For class adapters, only one object is introduced, no additional references are required to indirectly get adaptee, and for object adapters, additional references are required to indirectly get adaptee.
      Summary: It is recommended to use the object adapter as much as possible, conforming to the carp principle.

Adapter mode in the JDK
Java.util.arrays#aslist ()
Java.io.InputStreamReader (InputStream)
Java.io.OutputStreamWriter (OutputStream)

Summary
? Pros : Better reusability: The system needs to use 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. Better extensibility: When implementing adapter functionality, you can invoke the features you have developed to naturally extend the functionality of your system.
? disadvantage : Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. If it is not necessary, you can refactor the system directly without using the adapter.

Applicable scenarios

    • You want to use a class that already exists, and its interface doesn't suit your needs.
    • You want to create a reusable class that can work with other unrelated classes or classes that are not predictable.
    • (Use only with object adapter) you want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces.

Default Adapter
When you want to implement an interface but do not want to implement all the methods in the interface, just want to implement a subset of the methods, the default adapter mode is used. Its method adds an abstract class to the interface and the concrete implementation class, and the abstract class is all the methods that implement the target interface. The specific implementation class only needs to overwrite the method that it needs to complete.
A simple example:
1 interface classes (there are many ways to eat, sleep, work, and exercise, but I just want to sleep and drink)

publicinterface Live{    void sleep();    void eat();    void work();    void train();}

2 Abstract class:

public   class  livedefault  implements  live  {  @Override  public  void  sleep  () {}  @Override  public  void   Eat  () {}  @Override  public  void  work  () {}  @Override  public  void   Train  () {}}  

3 Implementation Classes

publicclass LiveImpl extends LiveDefault{    @Override    publicvoideat()    {        System.out.println("哇塞,好好吃");    }    @Override    publicvoidsleep()    {        System.out.println("好困,就是不想起床");    }}

Resources
1. "Java and Mode" adapter mode
2. "23 Design Patterns"
3. "Java Mode (adapter mode)"
4. Design Patterns in the "digital JDK"

Design mode: Adapter 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.