Matlab策略模式
2024-09-03 21:49:37
策略模式的意图是定义一系列算法,把它们一个一个封装起来,并且使它们可以互相替换。通常每个策略算法不可抽象再分。本人仿照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));
最新文章
- 发现两个有趣的CSS3效果
- TP框架整合Swagger UI接口文档
- Redis安装及基本配置
- BC之Run
- 使用 RestEasy 和 Apache Tomcat 构建 RESTful Web 服务
- Restrict each user to a single session in window server 2008 R2 or 2012
- 磁盘性能指标--IOPS 理论
- Xml序列化自引用/循环引用问题1
- (摘自ItPub)物理standby中switchover时switchover pending的解决办法
- NodeJs技术
- linux libpcap的性能问题,请大家注意绕行。
- SDP(10):文本式大数据运算环境-MongoDB-Engine功能设计
- JavaScript数据结构与算法(七) 双向链表的实现
- Apache Commons Configuration读取xml配置
- 10位时间戳使用moment转化为日期
- android sdk 历史版本下载地址
- js控制easyui文本框例子及控制html例子
- python MD5加密方法
- 收藏点webservice接口
- HTML5将<;video>;视频设置为页面动态背景
热门文章
- mysql 导出查询结果
- wordpress 本地环境安装
- SQLServer len 函数, 查字符串长度函数
- pd.ExcelWriter(to_excel)保存结果到已存在的excel文件中
- API调试工具--------Postman
- 面试突击(八)——JVM的结构及内存模型,是怎么划分的?
- shell基础知识6-在不按回车键的情况下读入N个字符
- ubuntu下编译C++程序
- MySQL创建触发器的时候报1419错误( 1419 - You do not have the SUPER privilege and binary logging is enabled )
- 使用命令行操作vmware esxi -- powershell