Skip to content

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
classDiagram
    Client --> Subject
    Subject <|-- RealSubject
    Subject <|-- Proxy
    Proxy --> RealSubject
    class Client {

    }
    class Subject {
        <<interface>>
        +request1()
        +request2()
    }
    class RealSubject {
        +request1()
        +request2()
    }
    class Proxy {
        +request1() "realSubject.request1();"
        +request2() "reqlSubject.request2();"
    }

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
classDiagram
    Client --> Subject
    Subject <|-- RealSubject
    Subject <|-- Proxy
    InvocationHandler <|-- RealInvoicationHandler
    Proxy --> RealInvoicationHandler
    RealInvoicationHandler --> RealSubject
    class Client {
    }
    class Subject {
        <<interface>>
        +request1()
        +request2()
    }
    class RealSubject {
        +request1()
        +request2()
    }
    class Proxy {
        +request1() "handler.invoke(Object proxy, Method method, Object[] args);"
        +request2()
    }
    class InvocationHandler {
        <<interface>>
        +invoke() Object
    }
    class RealInvoicationHandler {
        +invoke() Object "realSubject.request?()"
    }    

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