Week 14 - Proxy Design Pattern
The Proxy Design Pattern¶
Classification¶
- Structural Design Pattern:
- WHY?: Even though it changes the behavior as well, it fits more into the structural category because its intent is to structurally modify how objects are accessed, not how they behave.
Pattern Definition¶
- Hard Definition: Provide a surrogate or placeholder for another object to control access to it.
- Easy Definition: Control access to an actual object where the real functionality lies.
Representations¶
Mermaid Graph - State Pattern¶
Basic Proxy Pattern¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Virtual Proxy Pattern / Java Dynamic Proxy¶
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 |
|
UML¶
Basic Proxy UML¶
Virtual Proxy / Java Dynamic Proxy¶
When to use¶
- When you want to treat remote objects as if they are local (Remote Proxy).
- RMI, stubs hide communication between remote objects.
- When you want to control access to a particular object or objects (Protection Proxy).
- Check access permissions before allowing access.
- When you want to delay or control how and when a large object gets created (Virtual Proxy).
- Lazy-load / Lazy-instantiation
- In ALL cases the proxy pattern serves as a placeholder for a real class/object.
Real World Usages¶
- Caching proxies like: HTML Cache, Squid, Silverpeak network cache.
- Remote proxies: Compute engines, Web Services built with GSON/Jackson, XML-RPC, etc.
- Virtual Proxy: Hibernate ORM with
@ManyToOne(fetch = FetchType.LAZY)
on it.
Java Code Example¶
Main Take-Aways from the textbook¶
- The Proxy Pattern provides a representative for another object in order to control the client’s access to it. There are a number of ways it can manage that access.
- A Remote Proxy manages interaction between a client and a remote object.
- A Virtual Proxy controls access to an object that is expensive to instantiate.
- A Protection Proxy controls access to the methods of an object based on the caller.
- Many other variants of the Proxy Pattern exist including caching proxies, synchronization proxies, firewall proxies, copy-on-write proxies, and so on.
- Proxy is structurally similar to Decorator, but the two patterns differ in their purpose.
- The Decorator Pattern adds behavior to an object, while Proxy controls access.
- Java’s built in support for Proxy can build a dynamic proxy class on demand and dispatch all calls on it to a handler of your choosing.
- Like any wrapper, proxies will increase the number of classes and objects in your designs.
Main OOP Principles of State Pattern¶
- Dependency Inversion Principle The stub classes in the RMI remote proxy are examples of abstractions for the real-world object.
- Program to interfaces, not Implementations The Subject and Proxy implement a shared interface, thus making it so the client doesn’t need to distinguish between them.
- Loosely Coupled: Client is oblivious to the fact that it is not interacting with the real object or how that communication takes place.