Design Patterns -Adapter mode

Adapter mode (Adapter Pattern) is a structural model. The interface is mainly used to solve the problem of incompatibility, will transform interface of a class into a client expect another interfaces so that two classes can work together originally because of mismatched interfaces can not work together.

Related concepts

  • Target class (Target): defines the required interface client may be an abstract class or interface, it may be a concrete class
  • Fitter (Adaptee): needs to be adapted to the role, which is already existing class or object, the fitter general class is a concrete class that contains the customer wants to use business methods may not be appropriate in some cases class source code with those
  • Adapter (Adapter): its duty is to take the fitter converted to the target character, adapting to Adaptee and Target, in the object adapter, which enables both have contact through inheritance Target and objects associated with a Adaptee

Adapter Mode is divided into two: the object adapter, adapter type.

Object Adapter

image
You can see the object adapter is very similar to the previous mode == == decoration, is by combining / expansion to achieve the effect of the polymerization.

Class Adapter

image
Adapter class through inheritance / implementation to expand, need to consider coupling brings.

one example

I work with a computer is macbookpro, recently wanted an external monitor, and other displays only to find the time to get to the wiring inside the display is not in use on the mac lightning interfaces.
So I need an adapter.
image
Target is the example figure above yellow type-c interfaces, the figure above is the Adaptee red lightning interfaces, the whole adapter is Adapter.

Target class

public interface TypeC {
    void useTypeCPort();
}

Adaptation by category

public class Lightning {
    public void extent() {
        System.out.println("通过lightning接口外接显示器");
    }
}

Object Adapter

public class PortObjectAdapter implements TypeC {
    private Lightning lightning;

    public PortObjectAdapter(Lightning lightning) {
        this.lightning = lightning;
    }

    @Override
    public void useTypeCPort() {
        System.out.println("使用type-c转接头");
        lightning.extent();
    }
}

Class Adapter

public class PortClassAdapter extends Lightning implements TypeC {
    @Override
    public void useTypeCPort() {
        System.out.println("使用type-c转接头");
        super.extent();
    }
}

use

        //对象适配器
        System.out.println("----对象适配器----");
        Lightning lightning = new Lightning();
        PortObjectAdapter adapter = new PortObjectAdapter(lightning);
        adapter.useTypeCPort();
        //类适配器
        System.out.println("----类适配器----");
        PortClassAdapter adapter1 = new PortClassAdapter();
        adapter1.useTypeCPort();
----对象适配器----
使用type-c转接头
通过lightning接口外接显示器
----类适配器----
使用type-c转接头
通过lightning接口外接显示器

to sum up

  • Adapter model is relatively simple, is to adapt them to another interface requirements in the case do not modify the original interface.
  • Reality, often the target class and fitter classes already exist, so you only need to introduce an additional adapter class can be.
  • To the target class and the classes are adapted by introducing a decoupling adapter class.
  • In line with "the principle of opening and closing," can easily be replaced and the new adapter class.
  • Excessive use of the system will become very complicated, for example, clearly see the A interface is called, in fact, become interior adapted to achieve B interface.

    You can obtain the relevant code here: Design mode -Adapter mode

Guess you like

Origin www.cnblogs.com/xuxiaojian/p/11493512.html