-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.js
147 lines (119 loc) · 2.97 KB
/
command.js
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
/*
Command Pattern bir isteği bir nesne şeklinde kapsüller ve isteği yapmak isteyen nesneye aktarır. Bu desen, istekleri nesne şeklinde temsil ederek istek gönderen ve isteği alan nesneleri birbirinden bağımsız hale getirir.
*/
// Command sınıfı
class AddTextCommand {
constructor(editor, text) {
this.editor = editor;
this.text = text;
}
execute() {
this.editor.addText(this.text);
}
undo() {
this.editor.deleteText(this.text);
}
}
// Receiver sınıfı
class Editor {
constructor() {
this.content = "";
}
addText(text) {
this.content += text;
}
deleteText(text) {
this.content = this.content.replace(text, "");
}
printContent() {
console.log("Content: " + this.content);
}
}
// Invoker sınıfı
class TextEditor {
setCommand(command) {
this.command = command;
}
executeCommand() {
this.command.execute();
}
undoCommand() {
this.command.undo();
}
}
// Kullanım örneği
const editor = new Editor();
const addCommand = new AddTextCommand(editor, "Hello, ");
const deleteCommand = new AddTextCommand(editor, "World!");
const textEditor = new TextEditor();
textEditor.setCommand(addCommand);
textEditor.executeCommand();
textEditor.setCommand(deleteCommand);
textEditor.executeCommand();
editor.printContent(); // Content: Hello,
textEditor.undoCommand();
editor.printContent(); // Content: Hello, World!
/*
command pattern bir komut fonksiyonunu nesneye donustur ve bu komut fonksiyonuna ait olan nesneyle olan ilişkisi olmadan bu komut dosyasını cagırabilir
bu da tek bir arayuzden sanki tum komutlar tek arayuzden turemis gibi cagırımını saglar
*/
// Command sınıfları
class AddItemCommand {
constructor(shoppingCart, item) {
this.shoppingCart = shoppingCart;
this.item = item;
}
execute() {
this.shoppingCart.addItem(this.item);
}
}
class RemoveItemCommand {
constructor(shoppingCart, item) {
this.shoppingCart = shoppingCart;
this.item = item;
}
execute() {
this.shoppingCart.removeItem(this.item);
}
}
// Receiver sınıfı
class ShoppingCart {
constructor() {
this.items = [];
}
addItem(item) {
this.items.push(item);
console.log("Added item: " + item);
}
removeItem(item) {
const index = this.items.indexOf(item);
if (index !== -1) {
this.items.splice(index, 1);
console.log("Removed item: " + item);
}
}
}
// Invoker sınıfı
class User {
constructor() {
this.commands = [];
}
addCommand(command) {
this.commands.push(command);
}
executeCommands() {
this.commands.forEach((command) => {
command.execute();
});
}
}
// Kullanım örneği
const shoppingCart = new ShoppingCart();
const addItemCommand = new AddItemCommand(shoppingCart, "Shirt");
const removeItemCommand = new RemoveItemCommand(shoppingCart, "Shirt");
const user = new User();
user.addCommand(addItemCommand);
user.addCommand(removeItemCommand);
user.executeCommands();
// Added item: Shirt
// Removed item: Shirt