code refactoring: fix memory leaks, code style, etc.

This commit is contained in:
Jakub Vojvoda
2019-01-31 19:57:40 +01:00
parent a8681552c4
commit a0b0ea4f8e
24 changed files with 960 additions and 566 deletions

View File

@@ -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;
}