add Proxy pattern

This commit is contained in:
Jakub Vojvoda
2016-09-17 18:46:34 +02:00
parent 1c9457115c
commit a3f6b2a2d2
2 changed files with 70 additions and 1 deletions

68
proxy/Proxy.cpp Normal file
View File

@@ -0,0 +1,68 @@
/*
* C++ Design Patterns: Proxy
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT licence
* (for more details see LICENCE)
*
*/
#include <iostream>
/*
* Subject
* defines the common interface for RealSubject and Proxy
* so that a Proxy can be used anywhere a RealSubject is expected
*/
class Subject {
public:
virtual ~Subject() { /* ... */ }
virtual void request() = 0;
// ...
};
/*
* Real Subject
* defines the real object that the proxy represents
*/
class RealSubject : public Subject {
public:
void request() {
std::cout << "Real Subject request" << std::endl;
}
// ...
};
/*
* Proxy
* maintains a reference that lets the proxy access the real subject
*/
class Proxy : public Subject {
public:
Proxy() {
subject = new RealSubject();
}
~Proxy() {
delete subject;
}
void request() {
subject->request();
}
// ...
private:
RealSubject *subject;
};
int main()
{
Proxy *proxy = new Proxy;
proxy->request();
return 0;
}

View File

@@ -1,6 +1,7 @@
## Proxy
Provide a surrogate or placeholder for another object to control access to it.
Proxy pattern provides a surrogate or placeholder for another object to control access to it.
The pattern has structural purpose and applies to objects.
### When to use