The "Adapter Mode" of Design Patterns Written for Myself

1. Introduction

That is, define a packaging class for packaging incompatible objects

Wrapping class = adapter
Wrapped class = adaptee

1.1 Scene

  • The system needs to reuse existing classes, and the interface of this class does not meet the requirements of the system
  • Multiple components have similar functions, but the interfaces are not uniform and may switch frequently

1.2 Advantages

  • Better reusability The
    system needs to use existing classes, and such interfaces do not meet the needs of the system
  • Transparent and simple The
    client can call the same interface, so it is transparent to the client
  • Better scalability
    When implementing the adapter function, you can call the function developed by yourself, thereby naturally expanding the function of the system
  • Decoupling Decouple
    the target class and the adaptor class, and reuse the existing adaptor class by introducing an adaptor class without modifying the original code
  • Comply with the open-close principle. The
    same adapter can adapt both the adaptor class and its subclasses to the target interface; different adapters can be implemented for different target interfaces without modifying the class to be adapted

1.3 Disadvantages

  • Excessive use of adapters will make the system very messy and difficult to grasp as a whole

1.4 Classification

  • Class adapter
  • Object adapter

2. Class adapter

Use the inheritance relationship to convert the method of the wrapped class into the target method

2.1 Scene

  • Need to redefine some of the behavior of Adaptee
  • When you want to use it conveniently

2.2 Advantages

  • Easy to use and simplified code.
    Only one object is introduced, and no additional fields are needed to refer to the Adaptee instance.
    Disadvantages

2.3 Disadvantages

  • High coupling, low flexibility,
    using object inheritance is a static definition

2.4 Use

1. 创建目标接口
public interface Target {
    
    
    void requestFnc();
}

2. 创建被包装的类
public class Adaptee {
    
    
    public void sourceFnc() {
    
    
        System.out.println("adaptee fnc.");
    }
}

3. 创建适配类
/**
 * 类的适配器模式:使用继承关系连接到Adaptee类
 *
 * @author dkangel
 */
public class ClassAdapter extends Adaptee implements Target {
    
    
    /**
     * 适配目标方法
     */
    @Override
    public void requestFnc() {
    
    
        super.sourceFnc();
        System.out.println("convert sourceFnc to requestFnc.");
    }
}

4. 使用
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Target target = new ClassAdapter();
        target.requestFnc();
    }
}

3. Object adapter

Like the adapter of the class, the method of the wrapped class is converted into the target method

Different: the adapter of the class uses the inheritance relationship to connect to the adapter, and the adapter of the object uses the delegation relationship to connect to the adapter

2.1 Scene

  • Flexible use
  • Need to configure the source class and its subclasses at the same time

2.2 Advantages

  • High flexibility and low coupling The
    method adopted 对象组合is a dynamic combination method

2.3 Disadvantages

  • Complex use
    requires the introduction of object instances

2.4 Use

1. 创建目标接口
public interface Target {
    
    
    void requestFnc();
}

2. 创建被包装的类
public class Adaptee {
    
    
    public void sourceFnc() {
    
    
        System.out.println("adaptee fnc.");
    }
}

3. 创建适配类
/**
 * 对象的适配器模式:使用委派关系连接到Adaptee类
 *
 * @author dkangel
 */
public class ObjAdapter implements Target {
    
    

    private Adaptee adaptee;

    public ObjAdapter(Adaptee adaptee) {
    
    
        this.adaptee = adaptee;
    }

    @Override
    public void requestFnc() {
    
    
        this.adaptee.sourceFnc();
        System.out.println("convert sourceFnc to requestFnc.");
    }
}

Guess you like

Origin blog.csdn.net/Dkangel/article/details/105725574