typesciprt: Command Pattern
/** * * Command Pattern 命令是一种行为设计模式, 它可将请求或简单操作转换为一个对象。 * file: Commandts.ts * The Command interface declares a method for executing a command. * */ interface Command { execute(): string; //void } /** * Some commands can implement simple operations on their own. */ class SimpleCommand implements Command { private payload: string; /** * * @param payload */ constructor(payload: string) { this.payload = payload; } /** * * @returns */ public execute():string { //void console.log(`SimpleCommand: See, I can do simple things like printing (${this.payload})`); return "SimpleCommand:"+this.payload; } } /** * However, some commands can delegate more complex operations to other objects, * called "receivers." */ class ComplexCommand implements Command { private receiver: Receiver; /** * Context data, required for launching the receiver's methods. */ private a: string; private b: string; /** * Complex commands can accept one or several receiver objects along with * any context data via the constructor. */ constructor(receiver: Receiver, a: string, b: string) { this.receiver = receiver; this.a = a; this.b = b; } /** * Commands can delegate to any methods of a receiver. */ public execute():string { //void let getstr=""; console.log('ComplexCommand: Complex stuff should be done by a receiver object.'); getstr=getstr+this.receiver.doSomething(this.a); getstr=getstr+this.receiver.doSomethingElse(this.b); //getstr="ComplexCommand:"+this.a+","+this.b; return getstr; } } /** * The Receiver classes contain some important business logic. They know how to * perform all kinds of operations, associated with carrying out a request. In * fact, any class may serve as a Receiver. */ class Receiver { /** * * @param a * @returns */ public doSomething(a: string): string { //void console.log(`Receiver: Working on (${a}.)`); return "Receiver: Working on "+a+"."; } /** * * @param b * @returns */ public doSomethingElse(b: string):string { //void console.log(`Receiver: Also working on (${b}.)`); return "Receiver: Also working on"+b+"."; } } /** * The Invoker is associated with one or several commands. It sends a request to * the command. */ class Invoker { private onStart: Command; private onFinish: Command; /** * Initialize commands. * @param command * @param message * @returns */ public setOnStart(command: Command,message:string): string { //void let getstr=""; this.onStart = command; getstr="开始运行:"+command.execute()+","+message; return getstr; } /** * * @param command * @param message * @returns */ public setOnFinish(command: Command,message:string): string { //void let getstr=""; this.onFinish = command; getstr="已完成:"+command.execute()+","+message; return getstr; } /** * The Invoker does not depend on concrete command or receiver classes. The * Invoker passes a request to a receiver indirectly, by executing a * command. */ public doSomethingImportant():string { //void let getstr=""; console.log('Invoker: Does anybody want something done before I begin?'); if (this.isCommand(this.onStart)) { getstr=getstr+this.onStart.execute(); } console.log('Invoker: ...doing something really important...'); console.log('Invoker: Does anybody want something done after I finish?'); if (this.isCommand(this.onFinish)) { getstr=getstr+ this.onFinish.execute(); } return getstr; } private isCommand(object): object is Command { return object.execute !== undefined; } } let pubCommand1=""; let pubCommand2=""; let pubCommand3="Geovin Du"; let pubCommand4="geovindu"; /** * The client code can parameterize an invoker with any commands. */ const invoker = new Invoker(); pubCommand1=invoker.setOnStart(new SimpleCommand('Say Hi!'),"geovindu"); const receiver = new Receiver(); pubCommand2=invoker.setOnFinish(new ComplexCommand(receiver, 'Send email', 'Save report'),"geovindu"); pubCommand3=invoker.doSomethingImportant(); let messageCommand: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du.Web'; document.body.innerHTML = messageCommand+",<br/>one="+pubCommand1+",<br/>two="+pubCommand2+",<br/>three="+pubCommand3+",<br/>four="+pubCommand4+",<br/>TypeScript Command Pattern 命令模式";
调用:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <head><title>TypeScript Hello Command Pattern 命令模式</title> <meta name="Description" content="geovindu,涂聚文,Geovin Du"/> <meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/> <meta name="author" content="geovindu,涂聚文,Geovin Du"/> </head> <body> <script src="dist/Commandts.js"></script> </body> </html>
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)