当前位置:网站首页>Optimize if code with policy mode [policy mode]
Optimize if code with policy mode [policy mode]
2022-07-04 20:04:00 【qq_ twenty-two million eight hundred and forty-one thousand thr】
Problem description
At present, there are many in our code if,else To control the flow of the program , But this leads to a problem —— The extensibility of the code is not high .
Here, the policy mode is used for if-else Code optimization
Implementation process
1. Define policy pattern types
/** Generic type */
export namespace Common {
/** * The strategy pattern * - state * - true Function executed at */
type StrategyAction = [boolean, () => void];
}
Parameters in , On the left is boolean type , On the right is the corresponding response function
2. Execute the policy mode function
import {
Common } from "@/types/common/common"
export function exeStrategyActions(actions: Common.StrategyAction[]) {
actions.some((item) => {
const [flag, action] = item
if (flag) {
action()
}
return flag
})
}
adopt
some
To execute strategy mode , When corresponding toflag
by true when , Execute the corresponding method
some Official definition
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.
explain :
This some Method tests whether at least one function in the array passes the provided function . Turn it into ture, If it finds an element that matches the provided function ; Otherwise it becomes false. It will not change the original array
in other words , When some After finding the first satisfied condition , Then the subsequent methods will not be executed
3. Case use
const action :Common.StrategyAction[] = [
[
value === ' home page ',
() => {
useRouterPush().routerPush('home')
},
],
[
value === ' Submit list ',
() => {
submitListModal.openModal()
},
],
]
exeStrategyActions(action)
- When
value
When it is the first page , Execute jump routing method - When
value
When submitting a list for , Open the pseudo box
边栏推荐
- 1005 Spell It Right(20 分)(PAT甲级)
- C# 使用StopWatch测量程序运行时间
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
- HDU 6440 2018中国大学生程序设计网络选拔赛
- Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
- Explicit random number
- TCP两次挥手,你见过吗?那四次握手呢?
- C语言-入门-基础-语法-流程控制(七)
- socket编程demo二
- Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
猜你喜欢
随机推荐
HDU 1097 A hard puzzle
C语言-入门-基础-语法-流程控制(七)
Introduction to polyfit software
Educational Codeforces Round 22 E. Army Creation
Free soldier
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
kotlin 继承
Kotlin condition control
Euler function
HMM隐马尔可夫模型最详细讲解与代码实现
1005 Spell It Right(20 分)(PAT甲级)
Reflection (I)
FPGA timing constraint sharing 01_ Brief description of the four steps
牛客小白月赛7 E Applese的超能力
kotlin 循环控制
多表操作-外连接查询
实战模拟│JWT 登录认证
C language - Introduction - Foundation - grammar - process control (VII)
Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,