definition:
Provide a surrogate or placeholder for another object to control access to it.
A proxy for other objects to control access to this object.
General description:
It generally contains three roles: abstract theme, concrete theme, and agent theme.
General Class Diagram:
General code:
package Proxy;//Abstract topic class: public interface Subject { public void doSomething();}package Proxy;//Specific topic class public class RealSubject implements Subject{ @Override public void doSomething() { System.out.println("Business Logic..."); }}package Proxy;//Proxy topic class Proxy implements Subject{ private Subject sub = null; @Override public void doSomething() { this.sub.doSomething(); } public Proxy(Subject sub){ this.sub = sub; } }package Proxy;//Client public class Client { public static void main(String[] args) { Subject realsub = new RealSubject(); Subject proxy = new Proxy(realsub); proxy.doSomething(); }}advantage :
1. Clear responsibilities
2. High expansion
Extensions of proxy mode:
Normal Agent:
Specific theme classes are transparent to high-level levels, and specific theme classes are constructed in proxy theme classes.
Code implementation:
package GeneralProxy; public interface Subject { public void doSomething();}package GeneralProxy; public class RealSubject implements Subject{ private String name = null; @Override public void doSomething() { System.out.println(this.name + "Proxy, executing business logic..."); } public RealSubject(Subject proxy,String name) throws Exception{ if(proxy == null){ throw new Exception("Proxy object cannot be created"); }else{ this.name = name; } }}package GeneralProxy; public class Proxy implements Subject{ private Subject realsub = null; public Proxy(String name) { try { realsub = new RealSubject(this, name); } catch (Exception e) { e.printStackTrace(); } } public void doSomething() { realsub.doSomething(); }}package GeneralProxy; public class Client { public static void main(String[] args) { //Ordinary proxy Subject proxy = new Proxy("Zhang San"); proxy.doSomethaing(); } }
Forced proxy:
You must obtain the object of the proxy topic class by accessing the specific topic class, and then use the proxy topic class to control the access.
Code implementation:
package MustProxy; public interface Subject { public void doSomething(); public Subject getProxy();}package MustProxy; public class RealSubject implements Subject{ private String name = null; private Subject proxy = null; @Override public void doSomething() { if(isProxy()){ System.out.println(this.name + "Proxy, executing business logic..."); }else{ System.out.println("Please access the proxy first..."); } } public RealSubject(String name) { this.name = name; } public Subject getProxy() { proxy = new Proxy(this); return this.proxy; } private boolean isProxy(){ if(this.proxy == null){ return false; }else{ return true; } }}package MustProxy; public class Proxy implements Subject{ private Subject realSub = null; public Proxy(Subject realSub) { this.realSub = realSub; } public void doSomething() { this.realSub.doSomething(); } public Subject getProxy() { return this; }}package MustProxy; public class Client { public static void main(String[] args) { Subject realSub = new RealSubject("Zhang San"); realSub.doSomething(); Subject proxy = realSub.getProxy(); proxy.doSomething(); }} Application scenarios
In the real world, the secretary is equivalent to an agent. If the boss holds a meeting, then notify the employees of the meeting time, arrange the venue, sort out the venue after the meeting, and so on, etc., you can hand over the meeting related work to the secretary. The boss only needs to hold the meeting and does not need to do those things in person. Similarly, in our programming, we can also use the proxy pattern to decouple the code that is combined with a series of unrelated logic, such as the log code in the business code can be performed in the proxy. Spring's AOP is a typical dynamic proxy application.
Application form of proxy mode
(1) Remote Proxy - Can hide the fact that an object exists in different address spaces. It also allows clients to access objects on remote machines. Remote machines may have better computing performance and processing speed, and can quickly respond and process client requests.
(2) Virtual Proxy allows objects with high memory overhead to be created when needed. Create only when we really need this object.
(3) Copy-On-Write Proxy is used to control the copying of objects by delaying the copying of objects until the customer really needs them. It is a variant of the virtual proxy.
(4) Protection (Access)Proxy) provides different levels of target object access rights for different customers
(5) Cache Proxy provides temporary storage for overhead computational results. It allows multiple customers to share results to reduce computation or network latency.
(6) Firewall Proxy controls access to network resources and protects the subject from malicious customers.
(7) SynchronizationProxy provides secure access to the topic in the case of multi-threading.
(8) Smart Reference Proxy - When an object is referenced, it provides some additional operations, such as recording the number of calls to this object, etc.
(9) Complexity Hiding Proxy is used to hide the complexity of a complex collection of a class and perform access control. Sometimes it is also called Façade Proxy, which is not difficult to understand. Complex hidden proxy and appearance mode are different because the proxy controls access, while the appearance mode is different because the proxy controls access, while the appearance mode only provides another set of interfaces.