Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Go Premium
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Builder Pattern | C++ Design Patterns
Next article icon

Proxy Pattern | C++ Design Patterns

Last Updated : 31 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Design Patterns are an essential part of software engineering, offering proven solutions to common problems encountered during software development. One such pattern is the Proxy Pattern. The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object, allowing you to control access to it. This pattern can be particularly useful in situations where you need to add an extra layer of control, lazy loading, or remote access to objects.


proxy_pattern_150

Important Topics for the Proxy Pattern in C++ Design Patterns

  • What is a Proxy Pattern?
  • Components of the Proxy Pattern
  • Implementation of the Proxy Pattern in C++
  • Use Cases of the Proxy Pattern
  • Advantages of the Proxy Pattern
  • Disadvantages of the Proxy Pattern
  • Conclusion

What is a Proxy Pattern?

The Proxy Pattern, also known as the Surrogate Pattern that falls under the Gang of Four Design Patterns, is a structural design pattern, meaning it deals with how classes and objects are composed to form larger structures. The pattern involves creating a new class (the proxy) that acts as an intermediary or placeholder for an object (the subject) to control access to it. The proxy can be a local representative of the subject, which helps in various tasks such as lazy initialization, access control, logging, monitoring, or remote communication.

Components of the Proxy Pattern

Imagine you have a friend who's an expert at solving puzzles, but you don't want to bother them all the time. You'd like someone to help you access their puzzle-solving skills when needed.

  • Subject: Your friend's ability to solve puzzles is like the Subject. This is the core thing you want to use, which is solving puzzles.
  • Real Object: Your friend is the Real Object in this case. They can solve puzzles really well, but it might take some time and effort to engage them every time you need help.
  • Proxy: Now let's introduce a Proxy. In this scenario, the proxy could be your other friend, who acts as an intermediary. When you have a puzzle to solve, you talk to this proxy friend. The proxy friend decides if the puzzle is simple enough to handle themselves. If it's a tough puzzle, they'll ask your puzzle-solving expert friend for help.

In simple terms, the Proxy Pattern is like having a middle person (proxy) who decides when to get help from the real expert (real object) and when to handle things themselves. It's a way to manage and control access to someone's skills without bothering them unnecessarily.

Implementation of the Proxy Pattern in C++

We'll create a simplified example where an Image class is used to represent images, and we'll implement a proxy to control access and display these images.

Step 1: Define the Subject Interface

The Subject is the common interface that both the RealObject and Proxy will implement. In our example, it will be the Image interface:

C++
// Step 1: Define the Subject interface
class Image {
public:
    virtual void display() = 0;
};

Step 2: Implement the Real Object

The Real Object is the actual class that performs the real work. In our example, we create an RealImage class:

C++
// Step 2: Implement the Real Object
class RealImage : public Image {
private:
    std::string filename;

public:
    RealImage(const std::string& filename) : filename(filename) {
        // Simulate loading the image (this can be a resource-intensive operation)
        std::cout << "Loading image: " << filename << std::endl;
    }

    void display() override {
        std::cout << "Displaying image: " << filename << std::endl;
    }
};

Step 3: Create the Proxy

The Proxy class implements the same interface as the Real Object, and it maintains a reference to the Real Object. The proxy controls access to the Real Object. We'll call this ImageProxy:

C++
// Step 3: Create the Proxy
class ImageProxy : public Image {
private:
    RealImage* realImage; // Reference to the Real Object
    std::string filename;

public:
    ImageProxy(const std::string& filename) : filename(filename), realImage(nullptr) {}

    void display() override {
        // The Proxy checks if the Real Object is created and loads it if necessary
        if (realImage == nullptr) {
            realImage = new RealImage(filename);
        }
        realImage->display();
    }
};

Step 4: Use the Proxy Pattern

Now, let's use the Proxy Pattern:

C++
int main() {
    // Create a proxy to an image
    Image* image = new ImageProxy("example.jpg");

    // Display the image (the Proxy will load the Real Object if necessary)
    image->display();

    // Displaying the image again (the Proxy won't reload it)
    image->display();

    delete image; // Clean up

    return 0;
}

This code sets up an image proxy, and when you request to display the image, the proxy checks whether the real image needs to be loaded. If it's already loaded, the proxy just displays it. This demonstrates the lazy-loading aspect of the Proxy Pattern.

The Proxy Pattern allows you to control access to a resource (in this case, an image) and manage when and how the real object is created and accessed without changing the client's code.

Below is the complete working code of the above example:

C++
#include <bits/stdc++.h>

using namespace std;

class Image {
public:
    virtual void display() = 0;
};

class RealImage : public Image {
private:
    std::string filename;

public:
    RealImage(const std::string& filename) : filename(filename) {
        // Simulate loading the image (this can be a resource-intensive operation)
        std::cout << "Loading image: " << filename << std::endl;
    }

    void display() override {
        std::cout << "Displaying image: " << filename << std::endl;
    }
};

class ImageProxy : public Image {
private:
    RealImage* realImage; // Reference to the Real Object
    std::string filename;

public:
    ImageProxy(const std::string& filename) : filename(filename), realImage(nullptr) {}

    void display() override {
        // The Proxy checks if the Real Object is created and loads it if necessary
        if (realImage == nullptr) {
            realImage = new RealImage(filename);
        }
        realImage->display();
    }
};

int main() {
    // Create a proxy to an image
    Image* image = new ImageProxy("example.jpg");

    // Display the image (the Proxy will load the Real Object if necessary)
    image->display();

    // Displaying the image again (the Proxy won't reload it)
    image->display();

    delete image; // Clean up

    return 0;
}

Output
Loading image: example.jpg
Displaying image: example.jpg
Displaying image: example.jpg

Use Cases of the Proxy Pattern

The Proxy Pattern is particularly useful in various scenarios:

  1. Lazy Loading: When you have a resource-intensive object that should be loaded only when it is needed, a proxy can handle its creation. For instance, loading large images or database connections on-demand.
  2. Access Control: Proxies can control access to the Real Subject by adding authentication and authorization checks. This is especially useful in securing sensitive resources.
  3. Caching: Proxies can cache the results of expensive operations, reducing the need to invoke the Real Subject repeatedly.
  4. Logging and Monitoring: Proxies can be used to log or monitor the actions performed on the Real Subject without modifying its code.

Advantages of the Proxy Pattern

  1. Control: The Proxy Pattern allows you to control access to the real subject, enabling you to add additional features or security checks without modifying the subject itself.
  2. Lazy Loading: With virtual proxies, you can delay the creation of resource-intensive objects until they are actually needed, improving performance.
  3. Remote Access: In distributed systems, the Proxy Pattern allows you to access objects in different address spaces transparently.
  4. Security: Protection proxies can enforce access control and security checks, preventing unauthorized access to the real subject.

Disadvantages of the Proxy Pattern

  1. Complexity: Introducing proxy classes can add complexity to the codebase, making it harder to maintain and understand.
  2. Overhead: In some cases, the use of proxies can introduce additional method calls and indirection, potentially impacting performance.
  3. Maintenance: Keeping proxies in sync with changes in the real subject can be challenging.
  4. Misuse: If not used judiciously, the Proxy Pattern can lead to excessive code and an overly complex system.

Conclusion

The Proxy Pattern in C++ is a valuable tool for enhancing the control and efficiency of your applications. It allows you to add an additional layer of functionality to your classes without altering their structure. By using proxies, you can achieve lazy initialization, access control, logging and caching, improving the performance and maintainability of your code. When applied appropriately, the Proxy Pattern can be a powerful asses in your design patterns toolbox.


Next Article
Builder Pattern | C++ Design Patterns

T

thesunpandey
Improve
Article Tags :
  • Design Pattern
  • Geeks Premier League
  • System Design
  • Geeks Premier League 2023
  • C++ Design Pattern

Similar Reads

    Modern C++ Design Patterns Tutorial
    Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
    7 min read

    Creational Software Design Patterns in C++

    Factory Method Pattern | C++ Design Patterns
    Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the client code and the concrete products.Factory Method Pattern | C++ Design PatternsTa
    8 min read
    Abstract Factory Pattern | C++ Design Patterns
    Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are
    6 min read
    Builder Pattern | C++ Design Patterns
    The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
    6 min read
    Prototype Pattern | C++ Design Patterns
    When designing software, it's crucial to make it efficient, easy to reuse, and simple to maintain. One way to achieve these goals is by using design patterns, and one such pattern is the Prototype Pattern. In this article, we'll explore the Prototype Design Pattern in the context of C++. Important T
    5 min read
    Singleton Pattern | C++ Design Patterns
    A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier. The Singleton pattern is useful when we need to have
    11 min read

    Structural Software Design Patterns in C++

    Adapter Pattern | C++ Design Patterns
    Adapter Pattern is a structural design pattern used to make two incompatible interfaces work together. It acts as a bridge between two incompatible interfaces, allowing them to collaborate without modifying their source code. This pattern is particularly useful when integrating legacy code or third-
    6 min read
    Bridge Method | C++ Design Patterns
    Bridge Pattern is basically a structural design pattern in software engineering or in C++ programming that is used to separate an object's abstraction from its implementation. It is part of the Gang of Four (GoF) design patterns and is particularly useful when we need to avoid a permanent binding be
    9 min read
`; $(commentSectionTemplate).insertBefore(".article--recommended"); } loadComments(); }); }); function loadComments() { if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences