-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_strategy.cpp
More file actions
68 lines (59 loc) · 1.4 KB
/
final_strategy.cpp
File metadata and controls
68 lines (59 loc) · 1.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*Strategy Pattern can also be used in the software design. When it is possible to have several different algorithms for performing a process, each of which is the best solution depending on the situation, then a Strategy Pattern can be used. This article is all about Strategy Pattern. It uses a programming example to explain what, when and why a Strategy Pattern is needed. Benefits and drawbacks of using Strategy Pattern in software design is discussed. Three different approaches for implementing the Pattern in C++ and known uses of Strategy Pattern are also presented in this article.*/
#include<iostream>
using namespace std;
class sorting
{
public:
virtual void sortdata() = 0;
};
class quicksort : public sorting
{
public:
void sortdata()
{
cout << "sorting using quick sort";
}
};
class mergesort : public sorting
{
public:
void sortdata()
{
cout << "sorting using merge sort";
}
};
class sample
{
private:
sorting* s;
public:
sample():s(NULL) {}
void addNumber(int n)
{
cout << "Logic for add number goes here";
}
void sortvalues()
{
s->sortdata();
}
void printvalues ()
{
cout << "Printing the sorted values";
}
void setStrategy (sorting* s)
{
sample::s = s;
}
};
int main()
{
sample sam;
sam.addNumber(10);
sam.addNumber(9);
sam.addNumber(13);
sorting *s = new mergesort;
sam.setStrategy(s);
sam.sortvalues();
delete(s);
return 0;
}