GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.tutorialspoint.com — Proxy Design Pattern in C++
Selected Reading
Proxy Design Pattern in C++Proxy Design Pattern is a Structural Design Pattern that allows us to provide a substitute or we can say a placeholder for another object. In Proxy Design Pattern, a class represents the functionality of another class. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object (the original object). Proxy Design Pattern is used when we need to create a wrapper for an object to control access to it. It is also used to add additional functionality to an object without changing its code. Take the following example. In the image, the man at the gate talks to visitors for the queen because she’s busy. He passes messages and only lets the right ones in — kind of like a helper who speaks for her. That’s what a proxy does.
Types of Proxy Design PatternFollowing are the different types of Proxy Design Pattern −
Components of Proxy Design Pattern
Implementation of Proxy Design Pattern in C++We will implement a simple example of Proxy Design Pattern in C++. In this example, we will create a proxy for a Database class that simulates a connection to a database. The proxy will control access to the database and add caching functionality.
Steps to Implement Proxy Design PatternFollow the steps below to implement the Proxy Design Pattern in C++
C++ Code for Proxy Design PatternBelow is the C++ code that demonstrates the Proxy Design Pattern − Here, the DatabaseProxy class acts as a middleman for the RealDatabase class. It saves the result of the first query and gives back the saved result if you ask again, showing how a proxy can cache data. #include <iostream>
#include <string>
using namespace std;
// Subject Interface
class Database {
public:
virtual string query(const string& sql) = 0;
virtual ~Database() {}
};
// RealSubject Class
class RealDatabase : public Database {
public:
string query(const string& sql) override {
// Simulate a database query
return "Results for query: " + sql;
}
};
// Proxy Class
class DatabaseProxy : public Database {
private:
RealDatabase* realDatabase;
string cachedResult;
bool isCached;
public:
DatabaseProxy() : realDatabase(nullptr), isCached(false) {}
string query(const string& sql) override {
if (!isCached) {
if (realDatabase == nullptr) {
realDatabase = new RealDatabase();
}
cachedResult = realDatabase->query(sql);
isCached = true;
} else {
cout << "Returning cached result." << std::endl;
}
return cachedResult;
}
~DatabaseProxy() {
delete realDatabase;
}
};
// Client Code
int main() {
Database* db = new DatabaseProxy();
// First query - will access the real database
cout << db->query("SELECT * FROM users") << endl;
// Second query - will return cached result
cout << db->query("SELECT * FROM users") << endl;
delete db;
return 0;
}
Following is the output of the above code − Results for query: SELECT * FROM users Returning cached result. Results for query: SELECT * FROM users Pros and Cons of Proxy Design PatternFollowing are the advantages and disadvantages of using the Proxy Design Pattern −
When to Use Proxy Design Pattern?Following are some scenarios where you might want to use the Proxy Design Pattern −
Real-World Applications of Proxy Design PatternFollowing are some real-world applications of the Proxy Design Pattern −
ConclusionIn this chapter, you learned what the Proxy Design Pattern is, how it works, and where you can use it. Proxy helps you control access to something and lets you add new features without changing the main object. It’s a handy way to keep things safe and organized. Advertisements
| ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |