mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 12:54:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -14,8 +14,11 @@
|
||||
* Target
|
||||
* defines specific interface that Client uses
|
||||
*/
|
||||
class Target {
|
||||
class Target
|
||||
{
|
||||
public:
|
||||
virtual ~Target() {}
|
||||
|
||||
virtual void request() = 0;
|
||||
// ...
|
||||
};
|
||||
@@ -25,11 +28,14 @@ public:
|
||||
* all requests get delegated to the Adaptee which defines
|
||||
* an existing interface that needs adapting
|
||||
*/
|
||||
class Adaptee {
|
||||
class Adaptee
|
||||
{
|
||||
public:
|
||||
void specificRequest() {
|
||||
~Adaptee() {}
|
||||
|
||||
void specificRequest()
|
||||
{
|
||||
std::cout << "specific request" << std::endl;
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
@@ -40,11 +46,12 @@ public:
|
||||
* to request on a Target by extending both classes
|
||||
* ie adapts the interface of Adaptee to the Target interface
|
||||
*/
|
||||
class Adapter : public Target, private Adaptee {
|
||||
class Adapter : public Target, private Adaptee
|
||||
{
|
||||
public:
|
||||
virtual void request() {
|
||||
virtual void request()
|
||||
{
|
||||
specificRequest();
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
@@ -54,6 +61,7 @@ int main()
|
||||
{
|
||||
Target *t = new Adapter();
|
||||
t->request();
|
||||
|
||||
delete t;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -14,8 +14,11 @@
|
||||
* Target
|
||||
* defines specific interface that Client uses
|
||||
*/
|
||||
class Target {
|
||||
class Target
|
||||
{
|
||||
public:
|
||||
virtual ~Target() {}
|
||||
|
||||
virtual void request() = 0;
|
||||
// ...
|
||||
};
|
||||
@@ -26,11 +29,12 @@ public:
|
||||
* to Adapter it will get calls that client makes on the Target
|
||||
*
|
||||
*/
|
||||
class Adaptee {
|
||||
class Adaptee
|
||||
{
|
||||
public:
|
||||
void specificRequest() {
|
||||
void specificRequest()
|
||||
{
|
||||
std::cout << "specific request" << std::endl;
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
@@ -40,17 +44,20 @@ public:
|
||||
* implements the Target interface and when it gets a method call it
|
||||
* delegates the call to a Adaptee
|
||||
*/
|
||||
class Adapter : public Target {
|
||||
class Adapter : public Target
|
||||
{
|
||||
public:
|
||||
Adapter() : adaptee() {}
|
||||
|
||||
~Adapter() {
|
||||
|
||||
~Adapter()
|
||||
{
|
||||
delete adaptee;
|
||||
}
|
||||
|
||||
void request() {
|
||||
|
||||
void request()
|
||||
{
|
||||
adaptee->specificRequest();
|
||||
// ...
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
|
||||
@@ -64,6 +71,7 @@ int main()
|
||||
{
|
||||
Target *t = new Adapter();
|
||||
t->request();
|
||||
|
||||
delete t;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user