Design mode [6.2] - talk about adapter mode again

Posted by jcavard on Tue, 14 Dec 2021 02:11:07 +0100

The adapter mentioned here is not the usual class adapter, object adapter and interface adapter. What is implemented here is an adapter that uniformly manages all classes. To find the three main adapter patterns for design patterns, click https://blog.csdn.net/Aphysia...

Adapter mode (Baidu Encyclopedia): in computer programming, adapter mode (sometimes also known as packaging style or packaging) adapts the interface of a class to what users expect. An adaptation allows classes that cannot work together usually because the interfaces are incompatible. The way is to wrap the class's own interface in an existing class.

It can be understood that the two interfaces or classes may not be compatible. What the adapter mode needs to do is to manage them uniformly so that they can work together. Take a simple example: the memory card and notebook cannot be connected directly, but we use a card reader, which is equivalent to an adapter, to connect them.

1. Examples of not using adapters:
  • Requirements: the work of the program ape is program(), and the work of the teacher is teach(), so the specific work of these different occupations is different, and the content of these program ape program() methods may also be different, such as JD, Alibaba, Tencent, etc. the teachers are the same, and the work content of teachers in different schools is also different. Therefore, we must define interfaces. Different work contents can also be implemented by implementing their own interfaces.

    The code results are as follows:

  IProgramer. Class (interface of program code)

package com.noadapter;

public interface IProgramer {
    public void program();
}

  Programer. Class (the class of the program, which implements the interface of the code)

package com.noadapter;

public class Programer implements  IProgramer {
    @Override
    public void program() {
        System.out.println("I'm an excellent program ape. I roll code all day");
    }
}

The following teacher interfaces and classes that implement teachers are also the same as those in the above program:

ITeacher. Class (teacher teaching interface):

package com.noadapter;

public interface ITeacher {
    public void teach();
}

  Teacher. Class (teacher class that implements teaching):

package com.noadapter;

public class Teacher implements ITeacher {
    @Override
    public void teach() {
        System.out.println("I am a teacher and the flower of my motherland");
    }
}

  MyTest.class test class:

package com.noadapter;

public class MyTest {
    public static void main(String []args){
        ITeacher teacher = new Teacher();
        IProgramer programer = new Programer();
        //Their methods must be accessed one by one
        teacher.teach();
        programer.program();
    }
}

Operation results:

Understanding: if the adapter is not used to blur, we need to define all types of work objects (program apes, teachers, etc.), implement their own interfaces for them, and then call their methods. In this way, it is troublesome to write as many method calls as there are many types of work.

2. Define only one adapter implementation class

Based on the previous modification, iworkadapter is added Class and its implementation class workeradapter Class, and changed the test method, but nothing else has changed. The code structure is as follows:

Added iworkadapter Class (adapter interface):

public interface IWorkAdapter {
    //The reason why the parameter is Object is that it should be compatible with all types of work objects
    public void work(Object worker);
}

Added workadapter Class (adapter class):

public class WorkAdaper implements IWorkAdapter {
    @Override
    public void work(Object worker) {
        if(worker instanceof IProgramer){
            ((IProgramer) worker).program();
        }
        if(worker instanceof ITeacher){
            ((ITeacher) worker).teach();
        }
    }
}

Changed test class mytest class:

public class MyTest {
    public static void main(String []args){
        ITeacher teacher = new Teacher();
        IProgramer programer = new Programer();
        //Put the two types of work into the object array
        Object[] workers = {teacher,programer};
        //Define an adapter
        IWorkAdapter adapter = new WorkAdaper();
        //Adapter traversal object
        for(Object worker:workers){
            adapter.work(worker);
        }
    }
}

The results remain the same:

Analysis: writing only one adapter is like gathering the interfaces together. A layer is added in the middle, which shields the differences between calling different types of work (program ape, teacher), so as to understand the coupling.

3. Mode of multiple adapters

That is, define an adapter for each type of work (modify based on one adapter)

Modify iworkadapter class

public interface IWorkAdapter {
    //The reason why the parameter is Object is that it should be compatible with all types of work objects
    public void work(Object worker);
    //Judge whether the current adapter supports the specified type of work object
    boolean supports(Object worker);
}

Define a teacheradapter class

public class TeacherAdapter implements IWorkAdapter{
    @Override
    public void work(Object worker) {
        ((ITeacher)worker).teach();
    }

    @Override
    public boolean supports(Object worker) {
        return (worker instanceof ITeacher);
    }
}

Define a programmer adapter class

public class ProgrammerAdapter implements IWorkAdapter{
    @Override
    public void work(Object worker) {
        ((IProgramer)worker).program();

    }
    @Override
    public boolean supports(Object worker) {
        return (worker instanceof IProgramer);
    }
}

Test class (Test.class):

public class MyTest {
    public static void main(String []args){
        ITeacher teacher = new Teacher();
        IProgramer programer = new Programer();
        //Put the two types of work into the object array
        Object[] workers = {teacher,programer};
        //Adapter traversal object
        for(Object worker:workers){
            IWorkAdapter adapter = getAdapter(worker);
            adapter.work(worker);
        }
    }
    public static IWorkAdapter getAdapter(Object object){
        IWorkAdapter teacherAdapter = new TeacherAdapter();
        IWorkAdapter programmerAdapter = new ProgrammerAdapter();
        IWorkAdapter[] adapters = {teacherAdapter,programmerAdapter};
        for(IWorkAdapter adapter:adapters){
            if(adapter.supports(object)){
                return adapter;
            }
        }
        return null;
    }
}

Personal understanding: in fact, the essence of multiple adapters is to obtain the adapter that supports the object and use the object through the adapter.

Topics: Java