Structural model - Adapter mode (c)

The project source address: https: //github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)

1. Definitions

Converting the interface of a class into another interface to customer expectations. Adapter mode for those classes of incompatible interfaces can work together.

2. Applicable scene

  • Existing classes, methods, and it needs not match (the same or similar methods result)
  • Software design patterns are not considered in the design stage, along with software maintenance. Since different products, different manufacturers cause similar functions interface solutions are not the same as the

3. The adapter type and role

Object Adapter

In line with the principle of a combination of multiplexing, using delegation mechanism.

Object Adapter

Class Adapter

By class inheritance to achieve.

Class Adapter

As is clear from Figure adapter mode contains about three roles:

  1. Target (target abstract class): abstract class defines the desired target customer interface, or interface is an abstract class, the class may be a concrete. In class adapter, because the Java language does not support multiple inheritance, so it can only interface.

  2. Adapter (adapter class): it can call another interface, as a converter for adapting Adaptee and Target. It is the core of the Adapter pattern.

  3. Adaptee (adapted class): class ie adaptation adapted role, which defines an existing interface. This interface requires adaptation, fitter class wrapped the client wants business methods.

4. The relevant design patterns

Mode adapter mode and appearance

  • Adapter complex is to define a new interface with the existing interface, the appearance model.

Example 5. Mode

5.1 adapter class

Adaptee has an adapter class is:

public class Adaptee {
    public void adapteeRequest() {
        System.out.println("被适配者的方法");
    }
}

Target The target is the interface method:

public interface Target {
    void request();
}

ConcreteTarget This is the goal of the method:

/** 具体的实现类 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("ConcreteTarget目标方法");
    }
}

Adapter And this is the adaptation class: inherited classes are adapted to achieve the goal of an interface method:

public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        super.adapteeRequest();
    }
}

Client we have to test it:

public class Test {
    public static void main(String[]args){
        Target target = new ConcreteTarget();
        target.request();

        /** 现在,我们就来通过适配器类来进行实现 */
        Target adapterTarget = new Adapter();
        adapterTarget.request();
    }
}

Test Results:

Test Results

At this class diagram:

FIG adapter class Class

5.2 Object Adapter

There is a target interface:

public interface Target {
    void request();
}

There is a specific target class:

/** 具体的实现类 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("ConcreteTarget目标方法");
    }
}

And adapted classes:

public class Adaptee {
    public void adapteeRequest() {
        System.out.println("被适配者的方法");
    }
}

The above adaptation class and there are some differences, here adapted class is not inherited, but rather as a combination of attributes to the inside, and adapted to call the class methods inside the object:

public class Adapter implements Target {

    private Adaptee adaptee = new Adaptee();

    @Override
    public void request() {
        adaptee.adapteeRequest();
    }
}

test:

public class Test {
    public static void main(String[]args){
        Target target = new ConcreteTarget();
        target.request();

        /** 现在,我们就来通过适配器类来进行实现 */
        Target adapterTarget = new Adapter();
        adapterTarget.request();
    }
}

Test Results:

Test Results

At this class diagram:

FIG class object adaptation

5.3 Adapter demo

A simple abstract scene: mobile phone charging needs to 220V AC into DC 5V cell phone lithium battery needs of our demo is to write a power adapter, the AC220v -> DC5V

First, there is the adaptation of a class: 220V class

public class AC220 {
    public int outputAC220V() {
        int output = 220;
        System.out.println("输出220V的交流电"+output+"V");
        return output;
    }
}

We have a goal method interface: DC 5V into the

public interface DC5 {
    int outputDC5V();
}

This is the adaptation class (using an object adapter):

public class PowerAdapter implements DC5 {

    private AC220 ac220 = new AC220();

    @Override
    public int outputDC5V() {
        int adapterInput = ac220.outputAC220V();
        /** 变压器 */
        int adapterOutput = adapterInput / 44;
        System.out.println("通过PowerAdapter电源适配器输入AC"+adapterInput+"V"+"输出DC:"+adapterOutput+"V");
        return adapterOutput;
    }
}

test:

public class Test {
    public static void main(String[]args){
        DC5 dc5 = new PowerAdapter();
        dc5.outputDC5V();
    }
}

Test Results:

Test Results

6. pros and cons

advantage:

  • Class can improve transparency and multiplexing, multiplexing existing class but no change
  • Target adapter class and class decoupling, improve program scalability
  • In line with the principle of opening and closing

Disadvantages:

  • Adapter writing process takes full account, it may increase the complexity of the system
  • Increasing the difficulty readable system code

7. Extended -JDK1.7 and a frame of the source adapter mode

javax.xml.bind.annotation.adapters.XmlAdapter

org.springframework.aop.framework.adapter.AdvisorAdapter、MethodBeforeAdviceAdapter

org.springframework.orm.jpa.JpaVendorAdapter

org.springframework.web.servlet.HandlerAdapter、org.springframework.web.servlet.DispatcherServlet、org.springframework.web.servlet.mvc.Controller

Guess you like

Origin www.cnblogs.com/gj-blog/p/10929498.html