Proxy
-o0O0o-
How to control access to an object through a representative or surrogate?
-o0O0o-
UML Model File:
Model Format
Visual Paradigm
Advantages
- It manages rhe system resources by instantiating objects on-demand, reducing memory and processing overhead.
- Access control by enabling managing the permissions.
- Extra layer of Security where sensitive information are involved.
Disadvantages
- Complex in case of adding a lot of new classes.
- The response from the service could get delayed.
Code Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/** * Normally, we adopt a “lazy instantiation” strategy for the RealSubject, * creating it only when it is actually required. */ class RealSubject { ... // RealSubject attribute declaration public RealSubject(...) { ... // RealSubject attribute initialization } public void request() { ... // Real implementation of the request method } } /** * The Proxy class contains a reference that lets the proxy access the real subject. * Proxy also implements the same interface as the real subject and delegates all the work to the real subject. */ class Proxy { private static RealSubject realSubject; ... // Proxy specific attribute declaration public Proxy(...) { ... // Proxy specific initialization } /** * The “request” method is used to access the RealSubject’s request method. * This could involve additional logic such as lazy instantiation, access control, or logging. */ public void request() { if (realSubject == null) { realSubject = new RealSubject(...); } ... // Additional logic before delegating the request realSubject.request(); ... // Additional logic after delegating the request } } /** * If we are concerned with thread safety when using multi-threading, we need to ensure * that our Proxy class handles simultaneous calls properly, especially during instantiation of the RealSubject. */ class Proxy { private static volatile RealSubject realSubject; ... // Proxy specific attribute declaration public Proxy(...) { ... // Proxy specific initialization } /** * We use synchronized blocks inside the request method to ensure that RealSubject is * instantiated only once, even with concurrent access. */ public static void request() { if (realSubject == null) { synchronized (Proxy.class) { if (realSubject == null) { realSubject = new RealSubject(...); } } } realSubject.request(); } } /** * Alternatively, we may use an “early instantiation” strategy for RealSubject, creating it * at the time of Proxy class loading. This simplifies the request method as it no longer needs * to check if RealSubject exists. */ class Proxy { private static final RealSubject realSubject = new RealSubject(...); ... // Proxy specific attribute declaration public Proxy(...) { ... // Proxy specific initialization } /** * The “request” method simply delegates the request to the RealSubject. */ public void request() { realSubject.request(); } } |