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




Proxy Pattern | C++ Design Patterns

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.

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

Similar Reads

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....

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....

Use Cases of the Proxy Pattern

...

Advantages of the Proxy Pattern

...

Disadvantages of the Proxy Pattern

...

Conclusion

...