设计模式实现(二)——策略模式的C++实现

一、策略模式的概念

在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

二、例子

(1) Strategy抽象类的实现

 1 #ifndef STRATEGY_H
 2 #define STRATEGY_H
 3 
 4 class Strategy {
 5 public:
 6     Strategy() = default;
 7     virtual ~Strategy()
 8     {
 9     }
10     virtual int DoOperation(int x, int y) = 0;
11 };
12 #endif // !STRATEGY_H
View Code

(2) 加法策略类的实现

 1 #pragma once
 2 #include "Strategy .h"
 3 class OperationAdd :
 4     public Strategy
 5 {
 6 public:
 7     OperationAdd();
 8     ~OperationAdd();
 9     int DoOperation(int x, int y) override;
10 };
View Code
 1 #include "pch.h"
 2 #include "OperationAdd.h"
 3 
 4 
 5 OperationAdd::OperationAdd()
 6 {
 7 }
 8 
 9 
10 OperationAdd::~OperationAdd()
11 {
12 }
13 
14 int OperationAdd::DoOperation(int x, int y)
15 {
16     return x + y;
17 }
View Code

(3) 减法策略类的实现

 1 #pragma once
 2 #include "Strategy .h"
 3 class OperationSubstract :
 4     public Strategy
 5 {
 6 public:
 7     OperationSubstract();
 8     ~OperationSubstract();
 9     int DoOperation(int x, int y) override;
10 };
View Code
 1 #include "pch.h"
 2 #include "OperationSubstract.h"
 3 
 4 OperationSubstract::OperationSubstract()
 5 {
 6 }
 7 
 8 
 9 OperationSubstract::~OperationSubstract()
10 {
11 }
12 
13 int OperationSubstract::DoOperation(int x, int y)
14 {
15     return x - y;
16 }
View Code

(4) 乘法策略类的实现

 1 #pragma once
 2 #include "Strategy .h"
 3 class OperationMultiply :
 4     public Strategy
 5 {
 6 public:
 7     OperationMultiply();
 8     ~OperationMultiply();
 9     int DoOperation(int x, int y) override;
10 };
View Code
 1 #include "pch.h"
 2 #include "OperationMultiply.h"
 3 
 4 OperationMultiply::OperationMultiply()
 5 {
 6 }
 7 
 8 OperationMultiply::~OperationMultiply()
 9 {
10 }
11 
12 int OperationMultiply::DoOperation(int x, int y)
13 {
14     return x * y;
15 }
View Code

(5) context对象的实现

 1 #ifndef CONTEXT_H
 2 #define CONTEXT_H
 3 #include "Strategy .h"
 4 
 5 class Context {
 6 public:
 7     Context(Strategy *strategy);
 8     ~Context() = default;
 9     int ExecuteStrategy(int x, int y);
10 private:
11     Strategy *strategy;
12 };
13 #endif // !CONTEXT_H
View Code
 1 #include "pch.h"
 2 #include "Context .h"
 3 
 4 Context::Context(Strategy * strategy)
 5 {
 6     this->strategy = strategy;
 7 }
 8 
 9 int Context::ExecuteStrategy(int x, int y)
10 {
11     return this->strategy->DoOperation(x, y);
12 }
View Code

(6) 代码的测试实现

 1 // StrategyDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 2 //
 3 
 4 #include "pch.h"
 5 #include <iostream>
 6 #include <map>
 7 #include <string>
 8 #include "Context .h"
 9 #include "Strategy .h"
10 #include "OperationAdd.h"
11 #include "OperationMultiply.h"
12 #include "OperationSubstract.h"
13 using namespace std;
14  
15 std::map<string, Strategy*> strategyMap = 
16         { {"add", new (nothrow)OperationAdd},
17           {"sub", new (nothrow)OperationSubstract},
18           {"muti", new (nothrow)OperationMultiply}
19         };
20 
21 int main()
22 {
23     int num1, num2;
24     string operationName{};
25     cin >> operationName;
26     cin >> num1 >> num2;
27     Context context(strategyMap[operationName]);
28     cout << context.ExecuteStrategy(num1, num2) << endl;
29     return 0;
30 }
View Code

猜你喜欢

转载自www.cnblogs.com/madFish/p/12081158.html
今日推荐