mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 12:54:36 +03:00
add Strategy pattern
This commit is contained in:
92
strategy/Strategy.cpp
Normal file
92
strategy/Strategy.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* C++ Design Patterns: Strategy
|
||||
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
|
||||
* 2016
|
||||
*
|
||||
* Source code is licensed under MIT License
|
||||
* (for more details see LICENSE)
|
||||
*
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
* Strategy
|
||||
* declares an interface common to all supported algorithms
|
||||
*/
|
||||
class Strategy {
|
||||
public:
|
||||
virtual ~Strategy() { /* ... */ }
|
||||
virtual void algorithmInterface() = 0;
|
||||
// ...
|
||||
};
|
||||
|
||||
/*
|
||||
* Concrete Strategies
|
||||
* implement the algorithm using the Strategy interface
|
||||
*/
|
||||
class ConcreteStrategyA : public Strategy {
|
||||
public:
|
||||
~ConcreteStrategyA() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
std::cout << "Concrete Strategy A" << std::endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class ConcreteStrategyB : public Strategy {
|
||||
public:
|
||||
~ConcreteStrategyB() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
std::cout << "Concrete Strategy B" << std::endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class ConcreteStrategyC : public Strategy {
|
||||
public:
|
||||
~ConcreteStrategyC() { /* ... */ }
|
||||
|
||||
void algorithmInterface() {
|
||||
std::cout << "Concrete Strategy C" << std::endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
/*
|
||||
* Context
|
||||
* maintains a reference to a Strategy object
|
||||
*/
|
||||
class Context {
|
||||
public:
|
||||
Context(Strategy *s)
|
||||
: strategy(s) {}
|
||||
|
||||
~Context() {
|
||||
delete strategy;
|
||||
}
|
||||
|
||||
void contextInterface() {
|
||||
strategy->algorithmInterface();
|
||||
}
|
||||
// ...
|
||||
|
||||
private:
|
||||
Strategy *strategy;
|
||||
// ...
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
ConcreteStrategyA strategy;
|
||||
// ConcreteStrategyB strategy;
|
||||
// ConcreteStrategyC strategy;
|
||||
|
||||
Context context(&strategy);
|
||||
context.contextInterface();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user