Design Pattern Examples
Overview of object-oriented design patterns
Proxy_Class.cpp
Go to the documentation of this file.
1
7
8#include <iostream>
9#include <memory>
10
11#include "helpers/formatstring.h"
12
13#include "Proxy_Class.h"
14
20{
21 // For the purposes of this example, this namespace encapsulates the real
22 // class and proxy class to hide them from the rest of the example
23 // program. In a real program, the real class would be in its own
24 // assembly and separate from the proxy class.
25
26
36 {
37 public:
39 // Implementation of the IWorkByProxy interface
41 std::string DoWork(std::string someArgument)
42 {
43 return Helpers::formatstring("Real class received '%s'", someArgument.c_str());
44 }
45 };
46
47 //====================================================================
48 //====================================================================
49
50
55 {
56 private:
58 std::unique_ptr<DesignPatternExamples_cpp::IWorkByProxy> _realClassInstance;
59
68 {
69 if (_realClassInstance == nullptr)
70 {
71 std::cout << " --> Creating instance of real class..." << std::endl;
72 _realClassInstance = std::make_unique<Real_Class>();
73 }
74 return _realClassInstance.get();
75 }
76
77
78 public:
80 // Implementation of the IWorkByProxy interface
82
95 std::string DoWork(std::string someArgument)
96 {
97 std::cout << " --> proxy class DoWork() in" << std::endl;
99 std::cout << " --> Forwarding DoWork() call to real class..." << std::endl;
100 return realClass->DoWork(someArgument);
101 }
102 };
103
104} // end Proxy_Class_Private namespace
105
106
107//============================================================================
108//============================================================================
109
110
112{
113 std::unique_ptr<IWorkByProxy> Proxy_Classes_Container::CreateProxy()
114 {
115 std::cout << " --> Creating instance of proxy class..." << std::endl;
116 return std::make_unique<Proxy_Class_Private::Proxy_Class>();
117 }
118
119} // end namespace
120
Declaration of the IWorkByProxy interface and the Proxy_Classes_Container class used in the Proxy Pat...
static std::unique_ptr< IWorkByProxy > CreateProxy()
Retrieve a new instance of the proxy class.
The proxy class that implements the IWorkByProxy.
Definition: Proxy_Class.cpp:55
std::string DoWork(std::string someArgument)
Do some work on a std::string.
Definition: Proxy_Class.cpp:95
std::unique_ptr< DesignPatternExamples_cpp::IWorkByProxy > _realClassInstance
The one and only instance of the real class associated with this proxy class instance.
Definition: Proxy_Class.cpp:58
DesignPatternExamples_cpp::IWorkByProxy * _GetRealClass()
Helper method to retrieve the one and only instance of the real class. This hides the details of inst...
Definition: Proxy_Class.cpp:67
The real class object that does all the work.
Definition: Proxy_Class.cpp:36
std::string DoWork(std::string someArgument)
Does some work on the given argument and returns a new std::string.
Definition: Proxy_Class.cpp:41
The namespace containing all Design Pattern Examples implemented in C++.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Hides the details of the proxy and real class in C++. Used by the Proxy pattern example.
Definition: Proxy_Class.cpp:20
Represents what can be done on the proxy object. This same interface is implemented on the real objec...
Definition: Proxy_Class.h:25
virtual std::string DoWork(std::string someArgument)=0
Does some work on the given argument and returns a new std::string.