Proxy | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJul 12, 2021

--

Often a Proxy controls the access to another object. Intermediate layers that control access to a database are a well-known example. The pattern distinguishes between Remote Proxy, Virtual Proxy, and Protection Proxy. Also known is Smart Reference: This acts as replacement for a simple pointer — by accessing an object additional operations must be performed.

Real-life examples

  • Layers for accessing a database
  • Internet access
  • Guard or security service to protect a person or to pay attention on how the person is treated

Meaning

  • Provision of an upstream proxy object or a placeholder for the purpose of access control of an object

Applicability (suitable if …)

  • There is a need for a more adaptable and intelligent referencing of an object than a simple pointer
  • A local proxy is needed for an object in another address range (Remote Proxy)
  • The creation of resource-heavy objects on demand (e.g. transaction) is required (Virtual Proxy)
  • Control over access to original object is needed (Protection Proxy)
  • A replacement for a simple pointer is required and additional operations must be performed when accessing an object, e.g. reference counting, transaction handling (Smart Reference).

Assets and Drawbacks

  • Adds differentiation level for access to object
  • Remote Proxy can hide the fact that an object is in another address range
  • Virtual Proxy can perform optimizations such as creating an object on demand
  • Protection Proxy and smart reference enable additional management tasks as soon as an object is accessed
  • Example: Objects can be checked for internal state and thus their “permission” to use other instances arises

Example

When you hear Proxy, everyone immediately thinks of the internet. So for the example below I use this adaptation.

First, you define an interface for the internet that you can connect to and then visit websites:

Then follows the concrete class, which implements the functions accordingly:

After that, you define the interface of the Protection Proxy, which protects the actual Internet class and allows its use only if the user can authenticate himself. It also has a black list of websites that must not be visited.

The concrete class implements the given functions:

The client can easily initiate the instances via Dependency Inversion as part of Object Composition and can define websites for the white list:

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using compositions of classes and objects. This provides flexibility due to a variable composition structure depending on runtime factors. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links