Skip to content

Commit 995765e

Browse files
committed
Add command pattern
1 parent 1dde751 commit 995765e

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

command-pattern.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* What is the Command Design Pattern?
3+
*
4+
* - The command pattern is a behavioural design pattern in which an object is
5+
* used to represent and encapsulate all the information needed to call
6+
* a method at a later time
7+
* - This information includes the method name, the object that owns the method
8+
* and values for the method parameters
9+
* - Allows you to store lists of code that is used at a later time
10+
* or many times
11+
* - Client says "I want a specific Command to run when execute() is called on
12+
* one of these encapsulated(hidden) Objects"
13+
* - An Object called the Invoker transfers this Command to another Object
14+
* called a Receiver to execute the right code
15+
*
16+
* @see http://www.newthinktank.com/2012/09/command-design-pattern-tutorial/
17+
*/
18+
19+
interface ElectronicDevice {
20+
on(): void;
21+
off(): void;
22+
volumeUp(): void;
23+
volumeDown(): void;
24+
}
25+
26+
class Television implements ElectronicDevice {
27+
private volume: number = 0;
28+
on(): void {
29+
console.log("TV is ON")
30+
}
31+
off(): void {
32+
console.log("TV is OFF")
33+
}
34+
volumeUp(): void {
35+
this.volume++;
36+
this.displayVolume();
37+
}
38+
volumeDown(): void {
39+
this.volume--;
40+
this.displayVolume();
41+
}
42+
private displayVolume() {
43+
console.log(`TV Volume is at ${this.volume}`)
44+
}
45+
}
46+
47+
interface Command {
48+
execute(): void;
49+
}
50+
51+
class TurnTVOn implements Command {
52+
private device: ElectronicDevice;
53+
54+
public constructor(device: ElectronicDevice) {
55+
this.device = device;
56+
}
57+
58+
execute(): void {
59+
this.device.on();
60+
}
61+
}
62+
63+
class TurnTVOff implements Command {
64+
private device: ElectronicDevice;
65+
66+
public constructor(device: ElectronicDevice) {
67+
this.device = device;
68+
}
69+
70+
execute(): void {
71+
this.device.off();
72+
}
73+
}
74+
75+
class TurnTVUp implements Command {
76+
private device: ElectronicDevice;
77+
78+
public constructor(device: ElectronicDevice) {
79+
this.device = device;
80+
}
81+
82+
execute(): void {
83+
this.device.volumeUp();
84+
}
85+
}
86+
87+
class TurnTVDown implements Command {
88+
private device: ElectronicDevice;
89+
90+
public constructor(device: ElectronicDevice) {
91+
this.device = device;
92+
}
93+
94+
execute(): void {
95+
this.device.volumeDown();
96+
}
97+
}
98+
99+
class DeviceButton {
100+
private command: Command;
101+
102+
public constructor(command: Command) {
103+
this.command = command;
104+
}
105+
106+
public press(): void {
107+
this.command.execute();
108+
}
109+
}
110+
111+
class TVRemote {
112+
public static getDevice(): ElectronicDevice {
113+
return new Television();
114+
}
115+
}
116+
117+
//------------------------------------------------------------------------------
118+
119+
const device: ElectronicDevice = TVRemote.getDevice();
120+
121+
const commands: { [key: string]: Command } = {
122+
on: new TurnTVOn(device),
123+
off: new TurnTVOff(device),
124+
up: new TurnTVUp(device),
125+
down: new TurnTVDown(device)
126+
}
127+
const buttons: { [key: string]: DeviceButton } = {
128+
on: new DeviceButton(commands.on),
129+
off: new DeviceButton(commands.off),
130+
up: new DeviceButton(commands.up),
131+
down: new DeviceButton(commands.down),
132+
}
133+
134+
buttons.off.press();
135+
buttons.on.press();
136+
buttons.up.press();
137+
buttons.down.press();

0 commit comments

Comments
 (0)