Robert Larson

  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Proxy Pattern using C++ (Zoo Example)

Proxy Pattern using C++ (Zoo Example)

May 26, 2017 by Robert Larson

Table of Contents

  • What is the Proxy Pattern?
  • Proxy Pattern Class Diagram Explained
  • Types of Proxies
  • Proxy Pattern Example
  • Example written in C++
  • Benefits of the Proxy Pattern
  • Recommended Resources
  • In Closing
  • Like this:
  • Related

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

Proxy Pattern Class Diagram

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.

  1. Remote Proxy – our RealSubject is a remote object whose access is controlled by a Proxy object
  2. 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.
  3. Protection Proxy – our RealSubject may require access protection of some kind. The Proxy would provide that protection based on access rights.
  4. 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

Proxy Pattern (Zoo 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.

  1. Remote Proxy‘s primary benefit is that it hides the fact that the RealSubject lives within another process.
  2. Virtual Proxy‘s primary benefit is that it defer the creation of an object to only when it is absolutely needed.
  3. 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!

Share this:

  • Share on X (Opens in new window) X
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Posted in: c++, Design Patterns Tagged: #cplusplus, #cpp, #designpatterns #cplusplus, c++, designpatterns
← State Pattern using C++ (Music Player Example)
Bridge Pattern using C++ (Cell Phone Text App Example) →
  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Recent Posts

  • March Madness 2019 – Weighted Random Bracket Generator
  • Gifts Ideas for Software Developers 2018-2019
  • March Madness 2018 – Second Chance Weighted Random Bracket Generator
  • March Madness 2018 – Weighted Random Bracket Generator
  • Builder Pattern using C++ (NFL Scheduler Example)

Archives

  • March 2019
  • November 2018
  • March 2018
  • August 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017

Follow me on Twitter

My Tweets

Copyright © 2026 Robert Larson.

Grace WordPress Theme by SiteChurch

%d