1. Proxy mode definition
Provides a proxy for other objects to control access to this object. (you go to rent a house, not directly to the landlord. Instead, you provide you with the landlord's information through the intermediary, and then you go to the corresponding landlord)
2. Application scenario
- Rental intermediary
- Marriage introduction
- Intermediary looking for a job (migrant workers go to the electronics factory to screw through the intermediary)
...
3. UML class diagram
- Abstract topic roles: declare common interface methods for real and proxy topics
- Real subject role: the proxy class represents the real object and is responsible for executing the real logical business object of the system
- Agent subject role: agent class, which holds the reference of RealSubject and has full proxy right to RealSubject
(holding the parent class reference of the subclass implementation in the proxy, you can call the real implementation method of the subclass, and you can also do some additional things before calling the method. The code function is enhanced, which is a bit similar to the aop aspect in spirng. For example, if you want to get married, the wedding layout and post wedding cleaning up should be handed over to the proxy class implementation. You only need to get married. The wedding cloth Set and clean up (like code enhancement)
4. General code implementation
package com.zheng.demo4; public class Client { public static void main(String[] args) { new Proxy(new RealSubject()).request(); } //Abstract theme role interface ISubject { public void request();//What to do } //Real role static public class RealSubject implements ISubject { @Override public void request() { System.out.println("I'm getting married"); } } //delegable role static class Proxy implements ISubject { ISubject subject; public Proxy(ISubject subject) { this.subject = subject; } @Override public void request() { before(); subject.request();//Call the business logic of the real object after(); } //Method enhancement, things done before marriage public void before() { System.out.println("Decorate the wedding scene"); } //After marriage public void after() { System.out.println("Cleaning of the wedding site"); } } }
5. Results
6. Practical example (static proxy)
Young people are urged to marry and introduced
Code structure
One is static agent and the other is dynamic agent. There are improvements to static agents for dynamic agents.
abstract
package com.zheng.demo4; public interface IPerson { void findLove(); }
Specific person
package com.zheng.demo4; public class LiSi implements IPerson { @Override public void findLove() { System.out.println("Li Si put forward the request of the marriage object"); } }
agent
package com.zheng.demo4; public class LiSiFather implements IPerson { LiSi liSi; public LiSiFather(LiSi liSi) { this.liSi = liSi; } @Override public void findLove() { liSi.findLove(); System.out.println("Li Si's father began to look for it"); System.out.println("Try to socialize"); } }
client
package com.zheng.demo4; public class TestClient { public static void main(String[] args) { new PersonProxy(new LiSi()).findLove(); } }
result
Disadvantages: Li Si's father can only find objects for Li Si, not for others.
The solution is to use dynamic proxy, make the proxy class into an intermediary, and introduce it to anyone. The bottom layer of dynamic proxy generally does not need to be implemented, but directly calls the ready-made API.
package com.zheng.demo4; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MeiPoProxy implements InvocationHandler { private IPerson target; public IPerson getInstance(IPerson target) { this.target = target; Class<?> clazz = target.getClass(); return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { before(); Object result = method.invoke(this.target, args); after(); return result; } //Method enhancement public void before() { System.out.println("I'm a marriage agency. I've received your request"); } //Method enhancement public void after() { System.out.println("Exchange information and start dating"); } }
Client call
package com.zheng.demo4; public class TestClient { public static void main(String[] args) { new MeiPoProxy().getInstance(new LiSi()).findLove(); } }
result:
Call the newProxyInstance method of the proxy class
Entering this method, you can see that an instance object is generated
7. To generate objects for a dynamic proxy
- 1. Get the reference of the proxy object, and get all its interfaces, and get reflection
- 2. JDK dynamic proxy class regenerates a new class, and the new class should implement all interfaces implemented by the proxy class
- 3. Dynamically generate java code, and the newly added business logic method is called by a certain logic code
- 4. Compile the newly generated java code class file
- 5. Reload into the JVM to run.
8. Advantages and disadvantages of agent mode
advantage:
- Proxy objects are separated from real objects
- Reduce system coupling and good expansibility
- Protect target object
- Enhance target object functionality
Disadvantages:
- Increase in the number of classes
- Adding proxy objects slows down the processing of requests
- Increase system complexity
9. The difference between static agent and dynamic agent
- Static proxy can only be completed manually. New methods are added to the proxy class, and the proxy class also needs to be added synchronously, which violates the opening and closing principle
- Dynamic agent generates code dynamically at runtime, cancels the extension restrictions on the proxy class, and follows the opening and closing principle
- The dynamic agent extends the enhanced logic of the target class, combined with the policy pattern