Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8
9#include "helpers/formatstring.h"
10
11#include "Decorator_Exercise.h"
12#include "Decorator_Classes.h"
13
14
16{
17
29 // ! [Using Decorator in C++]
31 {
32 std::cout << std::endl;
33 std::cout << "Decorator Exercise" << std::endl;
34
36 baseElement = std::make_shared<TextElement>("This is raw text");
37
38 // Wrap the base element in three decorators.
39 IRenderElement::shared_ptr_t wrappedElement =
40 std::make_shared<WhiteBackgroundDecorator>(
41 std::make_shared<UnderlineDecorator>(
42 std::make_shared<RedForegroundDecorator>(baseElement)));
43
44 // Now render the elements to the console.
45 std::cout
46 << Helpers::formatstring(" base Text element: \"%s\"", baseElement->Render().c_str())
47 << std::endl;
48 std::cout
49 << Helpers::formatstring(" Decorated element: \"%s\"", wrappedElement->Render().c_str())
50 << std::endl;
51
52 std::cout << " Done." << std::endl;
53 }
54 // ! [Using Decorator in C++]
55
56
57} // end namespace
Implementation of the IRenderElement interface, Decorator base class, TextElement class,...
Declaration of the Decorator_Exercise() function as used in the Decorator Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Decorator_Exercise()
Example of using the Decorator design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::shared_ptr< IRenderElement > shared_ptr_t
An alias to simplify syntax for this shared pointer.