Skip to content

Commit 0903572

Browse files
author
samuel.casanova
committedJun 23, 2023
add command design pattern real world example
1 parent 76bf911 commit 0903572

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
 

‎src/Command/RealWorld/Output.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Kelsey Grammar sings and plays the piano for the theme song of Fraiser.
2+
The cruise liner, Queen Elizabeth II, moves only six inches for each gallon of diesel that it burns.
3+
American Airlines saved $40,000 in 1987 by eliminating one olive from each salad served in first-class.
4+
The the U.S. you dial `911`. In Stockholm, Sweden you dial 90000
5+
Facetious and abstemious contain all the vowels in the correct order, as does arsenious, meaning "containing arsenic."  
6+
There`s a systematic lull in conversation every 7 minutes.
7+
Walt Disney was afraid of mice.

‎src/Command/RealWorld/index.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const https = require('node:https');
2+
3+
/**
4+
* EN: Real World Example for the Command Design Pattern
5+
*
6+
* Need: Execute a command to retrieve random information every X seconds
7+
*
8+
* Solution: Create an object that has all the information to execute the query
9+
*/
10+
11+
/**
12+
* EN: The Command interface declares a method for executing a command.
13+
*/
14+
interface Command {
15+
execute(): void;
16+
}
17+
18+
/**
19+
* EN: We will use a receiver object to run the business logic
20+
*/
21+
class PrintRandomFactCommand implements Command {
22+
constructor(protected randomFactDomainServiceReceiver: RandomFactDomainServiceReceiver) {
23+
}
24+
25+
public async execute(): Promise<void> {
26+
const fact = await this.randomFactDomainServiceReceiver.getRandomFact();
27+
console.info(fact);
28+
}
29+
}
30+
31+
/**
32+
* EN: The Receiver class contains all the business logic to retrieve the
33+
* information
34+
*/
35+
class RandomFactDomainServiceReceiver {
36+
public getRandomFact(): Promise<string> {
37+
return new Promise((resolve, reject) => {
38+
https.get('https://uselessfacts.jsph.pl/api/v2/facts/random', (res) => {
39+
res.on('data', (d) => {
40+
const data = JSON.parse(d);
41+
const fact = data.text;
42+
resolve(fact);
43+
});
44+
}).on('error', (error) => {
45+
reject(error);
46+
});
47+
});
48+
}
49+
}
50+
51+
/**
52+
* EN: The Invoker will execute any command every X seconds.
53+
*/
54+
class CommandInvoker {
55+
constructor(protected command: Command, protected seconds: number = 5) {
56+
}
57+
58+
start(): void {
59+
setInterval(() => {
60+
this.command.execute();
61+
}, this.seconds * 1000);
62+
}
63+
}
64+
65+
/**
66+
* EN: The client code invokes the command
67+
*/
68+
const randomFactDomainServiceReceiver = new RandomFactDomainServiceReceiver();
69+
const command = new PrintRandomFactCommand(randomFactDomainServiceReceiver);
70+
const commandInvoker = new CommandInvoker(command, 3);
71+
72+
commandInvoker.start();

0 commit comments

Comments
 (0)
Please sign in to comment.