proxy pattern
Definition: for some reason, an object needs to be provided with a proxy to control access to the object. At this time, the access object is not suitable or can not directly reference the target object. The proxy object acts as an intermediary between the access object and the target object.
Static proxy
Role analysis:
- Abstract role: it is usually solved by using interfaces or abstract classes
- Real role: the role represented
- Agent role: represent the real role. After representing the real role, we usually do some ancillary operations
- Customer: the person who accesses the proxy object!
Code steps:
1. Interface
//Rent a house public interface Rent { public void rent(); }
2. Real role
//landlord or landlady public class Host implements Rent{ @Override public void rent() { System.out.println("The landlord wants to rent the house!"); } }
3. Agent role
public class Proxy implements Rent { private Host host; public Proxy() { } public Proxy(Host host) { this.host = host; } @Override public void rent() { seeHouse(); host.rent(); hetong(); fare(); } private void fare() { System.out.println("Intermediary fee"); } private void hetong() { System.out.println("Sign a lease contract"); } private void seeHouse() { System.out.println("The agent will show you the house"); } }
4. Client access agent role
public class Client { public static void main(String[] args) { //The landlord wants to rent the house Host host = new Host(); //Agents, intermediaries help landlords rent houses, but what? Agents generally have some ancillary operations Proxy proxy = new Proxy(host); //You don't have to face the landlord, just find an intermediary to rent a house proxy.rent(); } }
Benefits of agent mode:
- It can make the operation of real characters more pure! Don't pay attention to some public business.
- The public will be handed over to the agent role! Realize the division of business!
- When the public business is expanded, it is convenient for centralized management.
The main disadvantages are:
- The agent pattern will increase the number of classes in the system design
- Adding a proxy object between the client and the target object will slow down the request processing speed;
- It increases the complexity of the system;
So how to solve the shortcomings mentioned above? The answer is that you can use dynamic proxy
Dynamic agent
-
Dynamic agents have the same role as static agents.
-
The agent class of dynamic agent is dynamically generated, which is not written directly by us!
-
Dynamic agents are divided into two categories: interface based dynamic agents and class based dynamic agents.
- Interface based - JDK dynamic proxy [we use it here]
- Class based: cglib
- java bytecode implementation: Javasist
Two classes need to be understood: Proxy: Proxy and lnvocationHandler: call handler.
The code is as follows:
public class ProxyInvocationHandler implements InvocationHandler { // Proxy interface private Object target; public void setTarget(Object target) { this.target= target; } // Generated proxy class public Object getProxy(){ Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); return proxy; } // Process the proxy instance and return the result @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // The essence of dynamic agent is to implement it by reflection Object result = method.invoke(target,args); return result; } }
Client class:
public class Client { @Test public void test(){ Host host = new Host(); ProxyInvocationHandler pih = new ProxyInvocationHandler(); pih.setTarget(host); Rent proxy = (Rent) pih.getProxy(); proxy.rent(); } }
Benefits of dynamic agents:
- It can make the operation of real characters more pure! Don't pay attention to some public business.
- The public will be handed over to the agent role! Realize the division of business!
- When the public business is expanded, it is convenient for centralized management.
- A dynamic agent class represents an interface, which is generally a corresponding type of business
- A dynamic agent can represent multiple classes as long as it implements the same interface!