Adaptor Design Patterns

Keywords: Java

1.1. What is an adapter

Adapters in life:

Power adapter: converting 220V AC which is not satisfied with the requirements of operation into DC which meets the requirements of operation;

Modem: Converting electrical and optical signals to each other;

Adapter mode:

A design pattern solves the following problems:

Converting what did not meet the requirements of use into what met the requirements of use;

1.2. Adapters in Java

In java, adapters can be classified into three categories according to the different transformation objects:

Class adaptation; Object adaptation; Interface adaptation;

Requirements: Look at the following programs and write implementation classes to meet the needs of User classes.

interface Inter{//Interface
	public void method1();
	public void method2();
	public void method3();
	public void method4();
	public void method5();
}
class User{//User class
	public static void test1(Inter i){
		i.method1();
		i.method2();
	}
	public static void test2(Inter i){
		i.method2();
	}
	public static void test3(Inter i){
		i.method4();
	}
}

Problem: Because only a few methods in the interface are used in many implementation classes, but all methods in the interface have to be implemented, resulting in a lot of waste of methods.

Solutions:

One class can be used to implement the interface, and then other classes can inherit the implementation class. In other inherited classes, only the required functions need to be rewritten, and no more useless methods need to be written.

Using adapter classes to adapt interfaces, specific business logic classes (classes that implement specific functions) do not need to deal directly with interfaces, but can only care about specific business logic.

interface Inter{//Interface
    public void method1();
    public void method2();
    public void method3();
    public void method4();
    public void method5();
}
class User{//User class
    public static void test1(Inter i){
        i.method1();
        i.method2();
    }
    public static void test2(Inter i){
        i.method2();
    }
    public static void test3(Inter i){
        i.method4();
    }
}
/*
According to the requirement, we need to write three implementation classes to implement the interface Inter, which are used by three methods of User class.
*/
//First, create a class specifically to implement the interface
//Generally because this class only implements all abstract functions in the interface.
//There is no specific and meaningful function, so it is generally not allowed to create objects of this class.
//Because other classes inherit it and don't want it to create objects, they need to be declared abstract classes.
abstract class Adaptor implements Inter{
    public void method1(){}
    public void method2(){}
    public void method3(){}
    public void method4(){}
    public void method5(){}
}
class InterImpl1  extends Adaptor{
    public void method1(){
        System.out.println("method1   Realization 1");
    }
    public void method2(){
        System.out.println("method2   Realization 1");
    }
}
class InterImpl2  extends Adaptor{
    public void method2(){
        System.out.println("method2   Realization 2");
    }
}
class InterImpl3  extends Adaptor{
    public void method4(){
        System.out.println("method4   Realization 3");
    }
}

1.3. Summary of adapters

The function of adapter is to adapt and transform things that do not meet the requirements of use into things that meet the requirements of use.

The adapter design pattern solves the problem of how to transform classes, objects or interfaces that do not meet the requirements of use into those that meet the requirements of use.

According to the different objects adapted, adapters can be divided into class adapter, object adapter and interface adapter.

The steps of interface adaptation:

1. Create an adapter class implementation interface, in which all abstract functions in the interface are implemented using empty implementations.

2. Because the functions in the adapter class are empty, it is meaningless to create objects of this class, so the adapter class should be defined as abstract class.

3. To use adapter classes, you only need to write class inheritance adapter classes.

Posted by thenewguy on Sun, 30 Jun 2019 13:05:34 -0700