策略模式的意图是定义一系列算法,把它们一个一个封装起来,并且使它们可以互相替换。通常每个策略算法不可抽象再分。本人仿照https://www.runoob.com/design-pattern/strategy-pattern.html所给的例子,用Matlab代码对其进行实现

Strategy.m

classdef Strategy < handle
methods(Abstract)
doOperation(~,~,~);
end
end

OperationAdd.m

classdef OperationAdd < Strategy
methods
function res = doOperation(~,num1,num2)
res = num1 + num2;
end
end
end

OperationMultiply.m

classdef OperationMultiply < Strategy
methods
function res = doOperation(~,num1,num2)
res = num1 * num2;
end
end
end

Context.m

classdef Context < handle
properties
strategy
end
methods
function obj = Context(strategy)
obj.strategy = strategy;
end
function res = executeStrategy(obj,num1,num2)
res = obj.strategy.doOperation(num1,num2);
end
end
end

测试代码:

context = Context(OperationAdd());
disp("2 + 3 = " + context.executeStrategy(2, 3)); context =Context(OperationMultiply());
disp("2 * 3 = " + context.executeStrategy(2, 3));

最新文章

  1. 发现两个有趣的CSS3效果
  2. TP框架整合Swagger UI接口文档
  3. Redis安装及基本配置
  4. BC之Run
  5. 使用 RestEasy 和 Apache Tomcat 构建 RESTful Web 服务
  6. Restrict each user to a single session in window server 2008 R2 or 2012
  7. 磁盘性能指标--IOPS 理论
  8. Xml序列化自引用/循环引用问题1
  9. (摘自ItPub)物理standby中switchover时switchover pending的解决办法
  10. NodeJs技术
  11. linux libpcap的性能问题,请大家注意绕行。
  12. SDP(10):文本式大数据运算环境-MongoDB-Engine功能设计
  13. JavaScript数据结构与算法(七) 双向链表的实现
  14. Apache Commons Configuration读取xml配置
  15. 10位时间戳使用moment转化为日期
  16. android sdk 历史版本下载地址
  17. js控制easyui文本框例子及控制html例子
  18. python MD5加密方法
  19. 收藏点webservice接口
  20. HTML5将&lt;video&gt;视频设置为页面动态背景

热门文章

  1. mysql 导出查询结果
  2. wordpress 本地环境安装
  3. SQLServer len 函数, 查字符串长度函数
  4. pd.ExcelWriter(to_excel)保存结果到已存在的excel文件中
  5. API调试工具--------Postman
  6. 面试突击(八)——JVM的结构及内存模型,是怎么划分的?
  7. shell基础知识6-在不按回车键的情况下读入N个字符
  8. ubuntu下编译C++程序
  9. MySQL创建触发器的时候报1419错误( 1419 - You do not have the SUPER privilege and binary logging is enabled )
  10. 使用命令行操作vmware esxi -- powershell