@binart/jx-patterns
v1.0.2
Published
Design patterns for javascript.
Downloads
13
Readme
编程范型
设计模式
责任链
// 错误定义
export class ExampleError extends Error {
constructor(message) {
super(message);
this.name = 'ExampleError';
}
}
// 规则定义
export class ExampleRule extends ChainNode {
canHandle(...args: any[]): boolean {
// 获取配置项目,是否支持该规则检查
}
handle(msg): any {
// 执行规则检查
if (msg.type === 1) {
let timeInterval = 100;
if (timeInterval > 0) {
throw new ExampleRule();
}
}
}
}
// 规则检查、错误处理
const _chainChecker = new ChainContainer()
_chainChecker.add(new ExampleRule());
try {
_chainChecker.handle(msg);
} catch (err) {
if (err instanceof ValidationError) {
throw new ReadError("Validation Error", err);
} else {
throw err;
}
}
状态机
???