当前位置:网站首页>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
someTo execute strategy mode , When corresponding toflagby 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
valueWhen it is the first page , Execute jump routing method - When
valueWhen submitting a list for , Open the pseudo box
边栏推荐
- 西门子HMI下载时提示缺少面板映像解决方案
- Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
- Several methods of online database migration
- 1002. A+b for Polynomials (25) (PAT class a)
- BCG 使用之新建向导效果
- BCG 使用之CBCGPProgressDlg进度条使用
- 2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
- kotlin 条件控制
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
猜你喜欢

Development and construction of DFI ecological NFT mobile mining system

c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

ACM组合计数入门

In operation (i.e. included in) usage of SSRs filter

Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall

多表操作-内连接查询

Abc229 summary (connected component count of the longest continuous character graph in the interval)

On communication bus arbitration mechanism and network flow control from the perspective of real-time application

西门子HMI下载时提示缺少面板映像解决方案
随机推荐
Socket programming demo II
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
Find the nth power of 2
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
Master the use of auto analyze in data warehouse
kotlin 类和对象
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Reflection (I)
Kotlin inheritance
双冒号作用运算符以及命名空间详解
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
Dark horse programmer - software testing - stage 07 2-linux and database -09-24-linux command learning steps, wildcards, absolute paths, relative paths, common commands for files and directories, file
Actual combat simulation │ JWT login authentication
矩阵翻转(数组模拟)
Chrome开发工具:VMxxx文件是什么鬼
Educational Codeforces Round 22 E. Army Creation
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
HDU 1372 & POJ 2243 Knight moves (breadth first search)