-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.cpp
More file actions
44 lines (40 loc) · 1.19 KB
/
strategy.cpp
File metadata and controls
44 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* \file
* \brief
*
* \todo
*/
/*
Strategy is an algorithm that you pass into a function or class that uses the strategy.
The idea is that you can provide a different strategy to achieve something different.
*/
#include <iostream>
#include <string>
//-------------------------------------------------------------------------------------------------
class IStrategy
{
public:
virtual ~IStrategy() { }
virtual std::string format(const std::string &, const std::string &) const = 0;
};
//-------------------------------------------------------------------------------------------------
class Formatter : public IStrategy
{
public:
std::string format(const std::string & s1, const std::string & s2) const
{
return s1 + " " + s2 + "!";
}
};
//-------------------------------------------------------------------------------------------------
void hello_world(const IStrategy & strategy)
{
std::cout << strategy.format("Hello", "world") << std::endl;
}
//-------------------------------------------------------------------------------------------------
int main()
{
hello_world( Formatter() );
return 0;
}
//-------------------------------------------------------------------------------------------------