让javascript使用设计模式更简单
npm i wj-design-art -S
| 类 |
说明 |
入参 |
| ResChain |
constructor(handleFn: HandleFn, transaction?: any, next?: ResChain) |
HandleFn:处理事务的函数,transaction:事务, next:下一个处理对象 |
| 名称 |
说明 |
入参 |
出参 |
| setNext |
设置下一个处理对象,setNext(next: ResChain) |
next: 下一个处理对象 |
void |
| handle |
开始处理事务,handle(transaction?: any) |
transaction:要处理的事务 |
void |
import designArt from 'wj-design-art';
const count = 1100;
function deptHandleFn(transaction: any) {
return new Promise((resolve) => {
setTimeout(function() {
if(transaction >= 500) {
console.log(transaction + '部门不能解决,给单位')
resolve({
isNext: true
});
} else {
resolve({
isNext: false
});
}
}, 1000);
});
}
function companyHandleFn(transaction: any) {
return new Promise((resolve) => {
setTimeout(function() {
if(transaction >= 1000) {
console.log(transaction + '单位不能解决,给集团');
resolve({
isNext: true
});
} else {
resolve({
isNext: false
});
}
}, 1000);
});
}
function groupHandleFn(transaction: any) {
return new Promise((resolve) => {
setTimeout(function() {
console.log(transaction + '集团解决了')
resolve({
isNext: false
});
}, 1000);
});
}
const dept = new designArt.ResChain(deptHandleFn, count);
const company = new designArt.ResChain(companyHandleFn, count);
const group = new designArt.ResChain(groupHandleFn, count);
dept.setNext(company);
company.setNext(group);
dept.handle();
| 名称 |
说明 |
入参 |
出参 |
| 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 |
import designArt from 'wj-design-art';
const { singleCase } = designArt;
class A {
getName() {
return 'wj';
}
}
class B {
getName() {
return 'jack';
}
}
class C {
public myname: string;
constructor(name: string) {
this.myname = name;
}
getName() {
return this.myname;
}
}
singleCase.add(A);
singleCase.add(B, 'singleB');
singleCase.add(C, 'singleC', 'cname');
const singles = singleCase.singles;
console.log(singles.A.getName());
console.log(singles.singleB.getName());
console.log(singles.singleC.getName());
singleCase.del('A');
singleCase.del('singleB');
singleCase.clear();
| 类 | 说明 | 入参 |
| --- | ---- | ---- | ---- |
| 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 |
import designArt from 'wj-design-art';
import { ICommand } from 'wj-design-art/lib/types/Command/types';
const { Command } = designArt;
const command = new Command();
class One implements ICommand {
execute() {
console.log('one');
}
}
const one = new One();
function two() {
console.log('command two');
}
command.add(one).add(two);
command.add(function() {
console.log('command three');
});
command.remove(one);
command.undo();
command.execute();
https://github.com/WJjack/wj-design-art.git