描述
让javascript使用设计模式更简单
安装
npm i wj-design-art -S
使用
责任链模式的API
- ResChain类
类 | 说明 | 入参 |
---|---|---|
ResChain | constructor(handleFn: HandleFn, transaction?: any, next?: ResChain) | HandleFn:处理事务的函数,transaction:事务, next:下一个处理对象 |
- 实例方法或属性
名称 | 说明 | 入参 | 出参 |
---|---|---|---|
setNext | 设置下一个处理对象,setNext(next: ResChain) | next: 下一个处理对象 | void |
handle | 开始处理事务,handle(transaction?: any) | transaction:要处理的事务 | void |
责任链模式的使用
; // 要处理的事务const count = 1100; // 部门处理事务的函数 { return { ; };} // 单位处理事务的函数 { return { ; };} // 集团处理事务的函数 { return { ; };} const dept = deptHandleFn count;const company = companyHandleFn count;const group = groupHandleFn count; dept;company; // 或者不用setNext函数,直接给定下一处理对象// const group = new designArt.ResChain(groupHandleFn, count);// const company = new designArt.ResChain(companyHandleFn, count, group);// const dept = new designArt.ResChain(deptHandleFn, count, company); dept; // 输出结果为// 1100部门不能解决,给单位// 1100单位不能解决,给集团// 1100集团解决了
单例模式API
名称 | 说明 | 入参 | 出参 |
---|---|---|---|
singles | 一个属性,所有的单例 | none | none |
add | 增加单例,add(_class: any, classname: string = _class.name, ...args: any[]) | _class:类,classname:类名,...args:实例化类时的参数 | void |
del | 删除单例,del(key: string) | key:要删除的键 | void |
clear | 清空,clear() | none | void |
单例模式的使用
;const singleCase = designArt; { return 'wj'; } { return 'jack'; } public myname: string; { thismyname = name; } { return thismyname; }singleCase; // {A: instance}singleCase; // {A: instance, singleB: instance}singleCase; // {A: instance, singleB: instance, singleC: instance}const singles = singleCasesingles; // {A: instance, singleB: instance, singleC: instance}console; // wjconsole; // jackconsole; // cnamesingleCase; // {singleB: instance, singleC: instance}singleCase; // {singleC: instance}singleCaseclear; // {}
命令模式
命令模式API
- Command 类
类 | 说明 | 入参 |
---|---|---|
Command | 命令模式类 | none |
- 实例方法
名称 | 说明 | 入参 | 出参 |
---|---|---|---|
add | 添加命令, add(command: CommandItem) | command:函数或者是实现了ICommand接口的实例 | Command |
remove | 删除命令,remove(command: CommandItem) | command:函数或者是实现了ICommand接口的实例 | Command |
undo | 撤销命令,删除最后一次添加的命令,undo() | none | Command |
reset | 重置命令,清空所有命令,reset() | none | Command |
execute | 执行所有的命令,execute() | none | void |
命令模式的使用
;; const Command = designArt; const command = ; implements ICommand { console; } const one = ; { console;} command; command; // one,command two,command three command; // command two,command three command; // command two command; // command two