当前位置:网站首页>利用策略模式优化if代码【策略模式】
利用策略模式优化if代码【策略模式】
2022-07-04 17:16:00 【qq_22841387】
问题描述
当前我们代码中存在很多的if,else去控制程序的流程,但是这样就会导致一个问题——代码的可扩展性不高。
这里以策略模式进行对if-else代码的优化
实现过程
1.定义策略模式类型
/** 通用类型 */
export namespace Common {
/** * 策略模式 * - 状态 * - true时执行的函数 */
type StrategyAction = [boolean, () => void];
}
参数中,左侧是boolean类型,右侧是对应的响应函数
2.执行策略模式函数
import {
Common } from "@/types/common/common"
export function exeStrategyActions(actions: Common.StrategyAction[]) {
actions.some((item) => {
const [flag, action] = item
if (flag) {
action()
}
return flag
})
}
通过
some去执行策略模式,当对应策略模式类型中的flag为true时,执行对应的方法
some的官方定义
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.
解释:
这个some方法测试是否数组中至少有一个通过提供的函数。将它变为ture,如果它找到了一个元素符合提供的函数;否则变为false。它不会改变原本数组
也就是说,当some找到了第一个满足的条件后,就不再执行后续方法
3.实例使用
const action :Common.StrategyAction[] = [
[
value === '首页',
() => {
useRouterPush().routerPush('home')
},
],
[
value === '提交列表',
() => {
submitListModal.openModal()
},
],
]
exeStrategyActions(action)
- 当
value为首页时,执行跳转路由方法 - 当
value为提交列表时,打开拟态框
边栏推荐
- 一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
- 激进技术派 vs 项目保守派的微服务架构之争
- Scala基础教程--20--Akka
- 【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
- 【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
- My colleagues quietly told me that flying Book notification can still play like this
- Digital "new" operation and maintenance of energy industry
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
- 1、 Introduction to C language
- PB的扩展DLL开发(超级篇)(七)
猜你喜欢

爬虫初级学习

2022 ByteDance daily practice experience (Tiktok)

1、 Introduction to C language

vbs或vbe如何修改图标

Principle and application of ThreadLocal

How to modify icons in VBS or VBE

基于unity的愤怒的小鸟设计

MXNet对GoogLeNet的实现(并行连结网络)

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
随机推荐
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
使用SSH
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
输入的查询SQL语句,是如何执行的?
力扣刷题日记/day8/7.1
发送和接收IBM WebSphere MQ消息
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Scala basic tutorial -- 15 -- recursion
C language printing exercise
Imitation of numpy 2
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
How is the entered query SQL statement executed?
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
如何提高开发质量
机器学习概念漂移检测方法(Aporia)
Grain Mall (I)
[cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
ThreadLocal原理与使用