Skip to content

Commit 888f6bc

Browse files
authored
Added Strategy pattern
1 parent 8e39f35 commit 888f6bc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

strategyPattern/main.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Strategy {
5+
public:
6+
virtual void Action() = 0;
7+
virtual ~Strategy() { }
8+
};
9+
10+
class ConcreteStrategyBus :public Strategy {
11+
public:
12+
void Action() {
13+
cout << "Bus Vehicle" << endl;
14+
}
15+
};
16+
17+
class ConcreteStrategyCar :public Strategy {
18+
public:
19+
void Action() {
20+
cout << "car vehicle" << endl;
21+
}
22+
};
23+
24+
class ContextTransportation {
25+
public:
26+
void setStratedy(Strategy *stg) {
27+
_stg = stg;
28+
}
29+
30+
void DoActionSelectVehicle() {
31+
_stg->Action();
32+
}
33+
34+
private:
35+
Strategy *_stg;
36+
};
37+
38+
int main() {
39+
Strategy *bus = new ConcreteStrategyBus();
40+
Strategy *car = new ConcreteStrategyCar();
41+
ContextTransportation *context = new ContextTransportation();
42+
context->setStratedy(bus);
43+
context->DoActionSelectVehicle();
44+
context->setStratedy(car);
45+
context->DoActionSelectVehicle();
46+
delete bus;
47+
delete car;
48+
49+
return 0;
50+
}

0 commit comments

Comments
 (0)