Table of Contents
What is the Proxy Pattern?
The definition of the Proxy Pattern from both Design Patterns: Elements of Reusable Object-Oriented Software and Head First Design Patterns: A Brain-Friendly Guide is
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.
Proxy Pattern Class Diagram Explained
The above class diagram represents the Proxy Pattern in its simplest form. Our surrogate or place holder would be our Proxy object that controls the access to another object RealSubject. Each implement the same interface, Subject. This allows the client to implement to an interface and have no knowledge that it is interacting with a Proxy versus the RealSubject.
Types of Proxies
There are a few different types of proxies. Each control access to different types of resources.
- Remote Proxy – our RealSubject is a remote object whose access is controlled by a Proxy object
- Virtual Proxy – our RealSubject or a resource that is part of our RealSubject is expensive to create. The Proxy object, in this case, would defer the creation until it was absolutely needed.
- Protection Proxy – our RealSubject may require access protection of some kind. The Proxy would provide that protection based on access rights.
- Smart Reference – our Proxy would provide additional behavior such as reference counts of the RealSubject pointer. This is commonly known as a smart pointer. Additionally, the Proxy may provide a locking mechanism to protect the RealSubject from being changed by other objects while an access is already in progress. Lastly, the Proxy could be responsible for creating the RealSubject when it is first needed.
Proxy Pattern Example
I decided to show an example of a Protection Proxy pattern. Consider a zoo that allows two types of zoo patrons, members and guests. Members receive a few perks such as early access to the zoo over normal guests as well as being able to go to special animal shows. To start we will need a Zoo object. It will be protected by a ZooProxy object. Both objects will implement a ZooIF interface with just two methods, EnterZooEarly() and AttendAnimalShow(). Each of these methods have one argument, ZooPatron, which will be our interface to our two types of patrons, ZooMember and ZooGuest. Methods for those objects would be HasAnimalShowAccess() and HasEarlyAccess(). Each return true if access is granted, false, otherwise. Our ZooProxy will protect access to the Zoo object by determining if the ZooPatron making the request for early access or animal show access actually have permissions to do so. Thus illustrating the Protection Proxy pattern.
Example written in C++
So we have our design finished. Let’s take a look and see what our implementation will look like.
ZooMember.h
ZooMember.cpp
ZooGuest.h
ZooGuest.cpp
ZooPatron.h
ZooIF.h
Zoo.h
Zoo.cpp
ZooProxy.h
ZooProxy.cpp
ZooProxyTest.cpp
All the code for this post can be found here
Benefits of the Proxy Pattern
Each type of proxy a primary benefit.
- Remote Proxy‘s primary benefit is that it hides the fact that the RealSubject lives within another process.
- Virtual Proxy‘s primary benefit is that it defer the creation of an object to only when it is absolutely needed.
- Protection and Smart Reference Proxies have the same primary benefit of providing additional behavior pertaining to the RealSubject when access to the RealSubject is needed.
Recommended Resources
Here are some resources I recommend for more information on the Proxy Pattern
Head First Design Patterns: A Brain-Friendly Guide
Design Patterns: Elements of Reusable Object-Oriented Software
In Closing
There you have it. If you followed along, I hope you now have a better understanding of the Proxy Pattern. Please feel free to leave comments and feedback. Much appreciated!