Content deleted Content added
→PHP example: PHP-FIG PSR |
AndreAdrian (talk | contribs) →Examples: use C++14 version of Design pattern book Sample Code implementation |
||
Line 107:
==C++ example==
This C++14 implementation is based on the pre C++98 implementation in the book.
<syntaxhighlight lang="cpp">▼
#include<iostream>▼
▲#include <iostream>
#include <memory>
virtual void someMethod() = 0;▼
class View { // AbstractClass
public:
// defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
// implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
void display() {
setFocus();
doDisplay();
resetFocus();
}
virtual ~View() = default;
private:
void setFocus() {
std::cout << "View::setFocus\n";
}
void resetFocus() {
std::cout << "View::resetFocus\n";
}
};
class
// implements the primitive operations to carry out subclass-specific steps of the algorithm.
// render the view's contents
std::cout << "MyView::doDisplay\n";
}▼
}
};
int main() {
// The smart pointers prevent memory leaks
std::unique_ptr<View> myview = std::make_unique<MyView>();
myview->display();
</syntaxhighlight>
The program output is
<syntaxhighlight lang="c++">
View::setFocus
MyView::doDisplay
View::resetFocus
</syntaxhighlight>
|