当前位置:网站首页>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
边栏推荐
猜你喜欢
English grammar_ Noun - use
node_ Exporter deployment
Online text line fixed length fill tool
Key rendering paths for performance optimization
Cbcgpprogressdlg progress bar used by BCG
Actual combat simulation │ JWT login authentication
C # use stopwatch to measure the running time of the program
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Actual combat simulation │ JWT login authentication
随机推荐
Some thoughts on whether the judgment point is located in the contour
@transactional滥用导致数据源连接池耗尽问题
HDU 6440 2018中国大学生程序设计网络选拔赛
TCP waves twice, have you seen it? What about four handshakes?
黑马程序员-软件测试--08阶段2-linux和数据库-23-30-进程端口相关,修改文件权限,端口号信息的获取,程序和进程相关操作,linux命令案例
Double colon function operator and namespace explanation
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
Actual combat simulation │ JWT login authentication
Basic use of kotlin
How is the entered query SQL statement executed?
矩阵翻转(数组模拟)
Mysql database basic operation -ddl | dark horse programmer
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
1002. A+B for Polynomials (25)(PAT甲级)
Kotlin classes and objects
kotlin 循环控制
1007 maximum subsequence sum (25 points) (PAT class a)
Kotlin condition control
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
Online sql to excel (xls/xlsx) tool