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,7 +14,8 @@
|
||||
* Strategy
|
||||
* declares an interface common to all supported algorithms
|
||||
*/
|
||||
class Strategy {
|
||||
class Strategy
|
||||
{
|
||||
public:
|
||||
virtual ~Strategy() { /* ... */ }
|
||||
virtual void algorithmInterface() = 0;
|
||||
@@ -25,31 +26,37 @@ public:
|
||||
* Concrete Strategies
|
||||
* implement the algorithm using the Strategy interface
|
||||
*/
|
||||
class ConcreteStrategyA : public Strategy {
|
||||
class ConcreteStrategyA : public Strategy
|
||||
{
|
||||
public:
|
||||
~ConcreteStrategyA() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
|
||||
void algorithmInterface()
|
||||
{
|
||||
std::cout << "Concrete Strategy A" << std::endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class ConcreteStrategyB : public Strategy {
|
||||
class ConcreteStrategyB : public Strategy
|
||||
{
|
||||
public:
|
||||
~ConcreteStrategyB() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
|
||||
void algorithmInterface()
|
||||
{
|
||||
std::cout << "Concrete Strategy B" << std::endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class ConcreteStrategyC : public Strategy {
|
||||
class ConcreteStrategyC : public Strategy
|
||||
{
|
||||
public:
|
||||
~ConcreteStrategyC() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
|
||||
void algorithmInterface()
|
||||
{
|
||||
std::cout << "Concrete Strategy C" << std::endl;
|
||||
}
|
||||
// ...
|
||||
@@ -59,16 +66,18 @@ public:
|
||||
* Context
|
||||
* maintains a reference to a Strategy object
|
||||
*/
|
||||
class Context {
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
Context(Strategy *s)
|
||||
: strategy(s) {}
|
||||
|
||||
~Context() {
|
||||
Context( Strategy* const s ) : strategy( s ) {}
|
||||
|
||||
~Context()
|
||||
{
|
||||
delete strategy;
|
||||
}
|
||||
|
||||
void contextInterface() {
|
||||
|
||||
void contextInterface()
|
||||
{
|
||||
strategy->algorithmInterface();
|
||||
}
|
||||
// ...
|
||||
@@ -81,12 +90,8 @@ private:
|
||||
|
||||
int main()
|
||||
{
|
||||
ConcreteStrategyA strategy;
|
||||
// ConcreteStrategyB strategy;
|
||||
// ConcreteStrategyC strategy;
|
||||
|
||||
Context context(&strategy);
|
||||
Context context( new ConcreteStrategyA() );
|
||||
context.contextInterface();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user