Comic: What is the "agent mode"?

Image

Image

 

 

----- the next day -----

Image

Image

Image

Image

 

Image

Image

Image

Image

 

————————————

Image

Image

Image

Image

Image

 

 

Image

 

 

Image

 

Image

Image

 

Image

Image

 

public interface IStudentService {
    void insertStudent();
    void deleteStudent();
}

public class StudentService implements IStudentService {

    public void insertStudent(){
        //添加学生
    }

    public void deleteStudent(){
        //删除学生
    }
}

 

Image

Image

 

public class StudentService implements IStudentService {

    public void insertStudent(){
        System.out.println("准备添加学生");
        //添加学生
        System.out.println("添加学生成功");
    }

    public void deleteStudent(){
        System.out.println("准备删除学生");
        //删除学生
        System.out.println("删除学生成功");
    }

}

Image

 

Image

Image

Image

 

Image

Image

 

Image

Image

 

public class StudentServiceProxy implements IStudentService {

    IStudentService studentService;

    public StudentServiceProxy(IStudentService studentService){
        this.studentService = studentService;
    }

    @Override
    public void insertStudent() {
        System.out.println("准备添加学生");
        studentService.insertStudent();
        System.out.println("添加学生成功");
    }

    @Override
    public void deleteStudent() {
        System.out.println("准备删除学生");
        studentService.deleteStudent();
        System.out.println("删除学生成功");
    }
}

 

In the above code, the proxy class and the business class inherit the same interface and override the method of adding/removing students.

 

In the rewritten method, we can not only call the original method of the business class, but also perform additional processing before and after the call, such as adding logs, transactions, and so on.

 

In this way, in the client, as long as we create a proxy class, we can use it like a business class, which is very convenient:

 

public class Client {

    public static void main(String[] args) {
        IStudentService studentServiceProxy = new StudentServiceProxy(new StudentService());
        studentServiceProxy.insertStudent();
        studentServiceProxy.deleteStudent();
    }
}

Image

Image

 

 

Image

Image

Image

 

Taking Java language as an example, Java provides us with a very convenient toolkit for creating dynamic agents. When we generate a dynamic proxy, we need to use the InvocationHandler interface and the Proxy class.

 

The specific implementation process is as follows:

 

1. Implement the InvocationHandler interface and define what to do before and after calling the method:

 

public class StudentInvocationHandler implements InvocationHandler {

    private IStudentService studentService;

    public StudentInvocationHandler(IStudentService studentService){
        this.studentService = studentService;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName() + "方法调用前");
        method.invoke(studentService, args);
        System.out.println(method.getName() + "方法调用后");
        return null;
    }
}

 

2. Dynamically generate proxy objects through the newProxyInstance method of the Proxy class:

 

public class Client {

    public static void main(String[] args) {
        IStudentService studentService = new StudentService();
        InvocationHandler studentInvocationHandler = new StudentInvocationHandler(studentService);
        IStudentService studentServiceProxy = (IStudentService) Proxy.newProxyInstance(studentInvocationHandler.getClass().getClassLoader(), studentService.getClass().getInterfaces(), studentInvocationHandler);
        studentServiceProxy.insertStudent();
        studentServiceProxy.deleteStudent();
    }

}

Image

Image

 

Image

 

Image

 

Image

Image

 

 

—————END—————

Guess you like

Origin blog.csdn.net/taotaobaobei/article/details/112762441