typesciprt: Command Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 *
 * 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 命令模式";

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!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>

  

 

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-12 CSharp: Simple Factory Pattern in donet core 3
2022-10-12 CSharp: Interpreter Pattern in donet core 3
2022-10-12 CSharp: Chain of Responsibility Pattern in donet core 3
2019-10-12 css: hide or dispaly div
2019-10-12 Python: simple code
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示