码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-22 21:42:51      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:operation   iostream   文件包含   类型   pen   not   isp   efi   default   

一、策略模式的概念

在策略模式(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

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

标签:operation   iostream   文件包含   类型   pen   not   isp   efi   default   

原文地址:https://www.cnblogs.com/madFish/p/12081158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!