-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.ts
More file actions
59 lines (47 loc) · 1.17 KB
/
clipboard.ts
File metadata and controls
59 lines (47 loc) · 1.17 KB
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
// The Command interface
interface Command {
execute(text: string): void;
}
// Concrete command to copy text
class Copy implements Command {
private clipboard: TextClipboard;
constructor(clipboard: TextClipboard) {
this.clipboard = clipboard;
}
public execute(text: string): void {
this.clipboard.copy(text)
}
}
// Concrete command to paste text
class Paste implements Command {
private clipboard: TextClipboard;
constructor(clipboard: TextClipboard) {
this.clipboard = clipboard;
}
public execute(): void {
this.clipboard.paste()
}
}
class TextClipboard {
private data: string;
public copy(text: string): void {
console.log(`Clipboard: Copying ...`);
this.data = text
}
public paste(): void {
console.log(`Clipboard: Pasting ... \n${this.data}`);
}
}
// Client code
const clipboard = new TextClipboard()
let copy = new Copy(clipboard)
let paste = new Paste(clipboard)
console.log('Enter text to copy: Hello World!')
copy.execute('Hello World!')
paste.execute()
/* OUTPUT
Enter text to copy: Hello World!
Clipboard: Copying ...
Clipboard: Pasting ...
Hello World!
*/