Apart from many design patterns in C++ programming, Proxy pattern is one of the most influential design patterns in C++ which is used to provide a placeholder for accessing another object. RAII (Resource Acquisition Is Initialization) is used to represent the proxy pattern.
We get better control over the interaction with objects in programming with the help of proxy pattern. In this blog, we will understand the importance of proxy pattern in C++ programming along with its features, and more.
What is Proxy Pattern?
Proxy pattern is a structural design software pattern that could create a placeholder for anything like a network connection, object in memory, a file, and other resources which can be very complex and expensive to duplicate.
For example, the debit card or cheque we use from the bank is a proxy of the bank account. Despite using cash directly we have multiple options like net banking, UPI, cheque, credit card, and more for payments.
The proxy pattern uses placeholders to control access to objects and limit the interaction of the main object directly. It creates a duplicate copy of the object to work in the backend and manage the interaction. Proxy pattern is used to implement lazy initialization, logging, adding security checks, controlling access, and more.
Read More: Top Features of C++ Programming Language
Proxy Pattern: Key Takeaways
- Proxy pattern simply means standing for someone else in the line. For instance, marking your friend’s attendance in his place, a very common thing in college.
- Proxy pattern is a structural design pattern used to build a connecting bridge between client and actual object using the proxy object.
- Proxy is like a gatekeeper which keeps the system safe and implements more flexibility and efficiency.
What Problem Does Proxy Patterns Solve?
The proxy patterns control or limit the access of an object to another object by using a substitute or placeholder for it. It is very useful when we need complete control access for security reasons or we need to delay object creation.
This becomes most important when we need to manage expensive resources where we can manage changes without impacting the original object code. Proxy pattern in C++ program is useful when we need the following
- It can delay creation of heavy objects using lazy initialization which can be an image or file.
- It can limit the access control of other objects on a particular object using access based controls.
Proxy pattern can provide a security parameter and restrict everyone in directly accessing the object without any restrictions in access control.
Structure of Proxy Pattern In C++ Programming
This is a UML case style representation of Proxy pattern in C++ programming. Let us have a look at each component in the diagram.
1.Client
It represents the user or system that wants to perform an operation. To prevent it from interacting with the Real Object we have used a proxy function or pattern.
2. Proxy
Proxy acts as an intermediate between the real object and the client. It has a request () method that looks the same as the real object method.
This is where lazy loading, access control, caching, etc can be implemented using proxy patterns.
3. Real Object
Real object is the actual object that performs the real operation where the proxy forwards the request to this object if it is needed. This real object is kept safe from direct interactions with clients.
Chaining of Proxy in C++ Programming
Chain of proxies means connecting each component in sequence where each proxy undergoes checks before passing the request to next proxy for the real object.
Read more: Builder Pattern In C++ Design Patterns: Complete Explanation
Components of Proxy Design Pattern In C++ Programming
The proxy pattern consists of components which can be used to substitute placeholders to implement access controls. Let us get an overview of these important components.
1. Subject
The subject is defined as the common interface used for both the real object and the proxy so that the client can interact with any one of them in their preferred manner. This abstraction ensures that the client code does not know whether it is dealing with the real object or proxy.
2. Real Subject
The real Subject is the actual object which performs the real operations and implements the object reference. It contains the core functionality which clients want for their process. It is generally expensive to create due to the complex resources such as database, file I/O, and more.
3. Proxy
The proxy is a gatekeeper for the system making it more flexible and efficient. It is used to implement the subject interface and maintain a real object alongside. It controls the access of the real subject by performing operations such as lazy initialization, access control, catching, and more.
4. Client
The client is the initiator in the complete process where it interacts with the object using the reference and it is unaware that it is dealing with the proxy or the real object. This process allows for seamless substitution and more flexibility.
What Is the Use Of Proxy Design Pattern?
The proxy design pattern can be used to address various concerns in the software development process. Let us know its uses below.
-
- Lazy Loading: The Proxy design pattern is used for lazy loading when we want a resource intensive object to load slowly implementing delay for the time we exactly need it.
- Access Control: Proxy offers control policies where it acts as a gatekeeper protecting the object from interference by any unauthorised person or entity.
- Safety: While duplicating the real object proxy design pattern helps in promoting safety to the real object keeping it safe from any kind of direct interference.
- Caching: Proxies are able to implement caching to store resources or results. It is useful when there are repeated operations on real objects by catching results from previous results.
- Monitoring: Proxies provide methods by which we can log and monitor different functionalities in real time. It can easily intercept method calls by tracking usage, log information, and more.
How to Implement Proxy Design Pattern?
Check the complete process below to implement the proxy design pattern in C++ programming.
1. Define the Subject Interface
You can start by creating an interface or abstract class that declares the methods the client expects to use. This generally acts as a common contract that both the real object and the proxy will follow.
2. Implement the Real Object (RealSubject)
Now you have to create the class that performs the actual operations or business logic. This class implements the interface and provides the core functionality that the client needs.
3. Create the Proxy Class
You have to develop a proxy class that also implements the same interface. It holds a reference to the real object and adds additional behavior such as lazy instantiation, access control, logging, or caching. When a client calls a method, the proxy can process the request and then delegate it to the real object if needed.
4. Use the Proxy in the Client Code
Instead of directly accessing the real object, the client interacts with the proxy. The proxy determines how and when to pass the request to the actual object, offering a level of control and optimization without changing client logic.
When to Implement the Proxy Pattern?
There are certain conditions only where proxy patterns must be used. Let us check some of them below.
- You can use proxy when you want to postpone the creation of a resource intensive object until it is really needed.
- Proxy is summoned to control and manage access to an object making sure that the complete verification or conditions are met before allowing clients to interact with the real object.
- You can optimise the use of resources using this proxy pattern as it acts like a limiter which can also lead to performance improvement in the long run.
- Proxy can easily handle the communication details which makes it remote friendly and makes interaction more flexible and seamless.
- You can use a proxy to deal with the distributed system when you want to interact with objects in different systems.
- When you want to add cross cutting concerns such as monitoring, logging monitoring,
- When you need to manage access and protect shared resources to ensure safety or block harmful requests.
When to Avoid Using Proxy Patterns?
Proxies are not used at every place; you must be very careful before using them. You must avoid using proxy patterns in the following cases.
- Make sure that you do not use proxy for simple objects which do not involve any resource intensive object. This proxy can add unnecessary overhead.
- If your application does not need access control, lazy loading, or security then you can avoid using proxy as it can implement unnecessary complexity.
- When you do not need access control you can avoid using proxy patterns.
Also Read:
- Builder Pattern In C++ Design Patterns: Complete Explanation
- Command Pattern | C++ Design Patterns
- History Of C++ | Timeline (+Infographic), List Of Versions, & More
- Hanging the Exception Handling in C++: An Effective Guide
Learn C++ Programming with PW Skills
Become a master C++ programmer and learn Data Structures and Algorithms in C++ with PW Skills Self Paced program Decode DSA with C++ course. Get yourself prepared with job ready skills and competitive programming skills. Crack technical interview with ease by learning from our in-depth tutorials, e-learning materials, practice exercises, capstone projects and more.
Learn with dedicated mentors and in-depth interactive tutorials. Practice real world questions based on all modules in this course. Become a certified developer with this self paced lifetime program on pwskills.com.
Proxy Pattern FAQs
Q1. What is the Proxy pattern in C++ programming?
Ans: Proxy pattern is a structural design pattern used to build a connecting bridge between client and actual object using the proxy object. For example, consider a credit card as a proxy used for banking.
Q2. What are the components of the Proxy pattern?
Ans: Subject, real subject, client, and proxy are some of the components used in the complete process of proxy pattern in C++ programming.
Q3. When to implement a proxy pattern?
Ans: You can implement a proxy pattern when you want to delay the creation of complex or heavy resource intensive object until it is actually needed.
Q4. What is a real life example of a proxy pattern?
Ans: Let us take a real world example of a proxy pattern where we are using a bank cheque or credit card in place of our bank account. It can be used in place of cash and provides a method of accessing the cash indirectly.