proxy pattern
What is agent mode?
Agent mode is to find an intermediary to help you do something. You just need to pay attention to your own business, and other intermediaries will help you.
There are three elements in the agent model:
- Abstract objects: Interfaces
- Proxy objects: classes
- Real objects: classes
Static agent
Static proxy: you can create proxy objects by yourself. Every real object needs to be created by you.
Disadvantage: if there are many real objects to be represented, you need to write many proxy classes.
The main idea of static proxy is to put an object of the proxy class in the proxy class and implement the method of the proxy class through the proxy class. Before or after the proxy method is executed, you can perform some additional operations.
Code implementation:
interface Eat{ void eat(); } //Real class class Person implements Eat{ private String name; Person(String name){ this.name = name; } public String getName(){ return this.name; } @Override public void eat() { System.out.println(this.name+"having dinner"); } } //proxy class class ProxyPerson implements Eat{ private Person person; ProxyPerson(Person person){ this.person = person; } @Override public void eat() { System.out.println("help"+person.getName()+"cook"); person.eat(); System.out.println("help"+person.getName()+"Washing dishes"); } } public class StaticProxyTest { public static void main(String[] args) { ProxyPerson pp = new ProxyPerson(new Person("Xiaoming")); pp.eat(); } }
Operation result:
This is an example of a static agent.
Dynamic agent
In the static proxy, the proxy class is fixed, and the dynamic proxy can make the proxy class generate dynamically. The reflection mechanism used here.
Dynamic Proxy uses a Proxy class and InvocationHandler interface to dynamically create a Proxy class.
Code implementation:
interface Eat{ void eat(); } //Real class class Person implements Eat{ private String name; Person(String name){ this.name = name; } public String getName(){ return this.name; } @Override public void eat() { System.out.println(this.name+"having dinner"); } } class MyInvocationHandler implements InvocationHandler{ private Object obj; MyInvocationHandler(Object obj){ this.obj = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("cook"); method.invoke(obj); System.out.println("Washing dishes"); return null; } } public class StaticProxyTest { public static void main(String[] args) { Eat person = new Person("tom");//Real object InvocationHandler handler = new MyInvocationHandler(person); Eat eat = (Eat) Proxy.newProxyInstance(Person.class.getClassLoader(),Person.class.getInterfaces(),handler); eat.eat(); } }
Operation result:
Dynamic agent implementation steps
Proxy.newProxyInstance(Person.class.getClassLoader(),Person.class.getInterfaces(),handler);
This sentence is mainly used to create an object of a proxy class.
In the newProxyInstance method, a proxy class is created using reflection, and the proxy class implements the interface implemented by Person. There is also a constructor with InvocationHandler as the parameter in the proxy class. When calling the method of the proxy object, it actually calls the invoke method in the InvocationHandler.
summary
Today, I'm just going to have a general understanding of static agents and dynamic agents, and I need to understand their underlying implementation slowly.