-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c71bd0e
commit ef73c9c
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/ManagerInvoker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class OrderManager { | ||
constructor() { | ||
this.history = []; | ||
this.undoStack = []; | ||
} | ||
|
||
executeCommand(command) { | ||
command.execute(); | ||
this.history.push(command); | ||
this.undoStack = []; // Reset the undo stack after a new command | ||
} | ||
|
||
undo() { | ||
const command = this.history.pop(); | ||
if (command) { | ||
command.undo(); | ||
this.undoStack.push(command); | ||
} else { | ||
console.log("No commands to undo."); | ||
} | ||
} | ||
|
||
redo() { | ||
const command = this.undoStack.pop(); | ||
if (command) { | ||
command.execute(); | ||
this.history.push(command); | ||
} else { | ||
console.log("No commands to redo."); | ||
} | ||
} | ||
} | ||
|
||
module.exports = OrderManager; |
40 changes: 40 additions & 0 deletions
40
Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const Order = require("./receiver"); | ||
const OrderManager = require("./ManagerInvoker"); | ||
const { | ||
AddItemCommand, | ||
RemoveItemCommand, | ||
ProcessOrderCommand, | ||
CancelOrderCommand, | ||
} = require("./orderCommand"); | ||
|
||
// Client code | ||
const myOrder = new Order(); | ||
const orderManager = new OrderManager(); | ||
|
||
// Add items | ||
const addItem1 = new AddItemCommand(myOrder, "Burger"); | ||
const addItem2 = new AddItemCommand(myOrder, "Fries"); | ||
|
||
orderManager.executeCommand(addItem1); // Adds "Burger" | ||
orderManager.executeCommand(addItem2); // Adds "Fries" | ||
|
||
// Show current items | ||
myOrder.showItems(); | ||
|
||
// Remove an item | ||
const removeItem = new RemoveItemCommand(myOrder, "Fries"); | ||
orderManager.executeCommand(removeItem); // Removes "Fries" | ||
|
||
// Undo remove | ||
orderManager.undo(); // Restores "Fries" | ||
|
||
// Show current items | ||
myOrder.showItems(); | ||
|
||
// Process order | ||
const processOrder = new ProcessOrderCommand(myOrder); | ||
orderManager.executeCommand(processOrder); // Processes the order | ||
|
||
// Cancel the order | ||
const cancelOrder = new CancelOrderCommand(myOrder); | ||
orderManager.executeCommand(cancelOrder); // Cancels the order |
79 changes: 79 additions & 0 deletions
79
...g_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/orderCommand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
class OrderCommand { | ||
execute() { | ||
throw new Error("execute() is not implemented"); | ||
} | ||
|
||
undo() { | ||
throw new Error("undo() is not implemented"); | ||
} | ||
} | ||
|
||
// Concrete class | ||
class AddItemCommand extends OrderCommand { | ||
constructor(order, item) { | ||
super(); | ||
this.order = order; | ||
this.item = item; | ||
} | ||
|
||
execute() { | ||
this.order.addItem(this.item); | ||
} | ||
|
||
undo() { | ||
this.order.removeItem(this.item); | ||
} | ||
} | ||
|
||
class RemoveItemCommand extends OrderCommand { | ||
constructor(order, item) { | ||
super(); | ||
this.order = order; | ||
this.item = item; | ||
} | ||
|
||
execute() { | ||
this.order.removeItem(this.item); | ||
} | ||
|
||
undo() { | ||
this.order.addItem(this.item); | ||
} | ||
} | ||
|
||
class ProcessOrderCommand extends OrderCommand { | ||
constructor(order) { | ||
super(); | ||
this.order = order; | ||
} | ||
|
||
execute() { | ||
this.order.processOrder(); | ||
} | ||
|
||
undo() { | ||
console.log("Cannot undo after order is processed."); | ||
} | ||
} | ||
|
||
class CancelOrderCommand extends OrderCommand { | ||
constructor(order) { | ||
super(); | ||
this.order = order; | ||
} | ||
|
||
execute() { | ||
this.order.cancelOrder(); | ||
} | ||
|
||
undo() { | ||
console.log("Cannot undo after order is cancelled."); | ||
} | ||
} | ||
|
||
module.exports = { | ||
AddItemCommand, | ||
RemoveItemCommand, | ||
ProcessOrderCommand, | ||
CancelOrderCommand, | ||
}; |
41 changes: 41 additions & 0 deletions
41
Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/receiver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Order { | ||
constructor() { | ||
this.items = []; | ||
} | ||
|
||
addItem(item) { | ||
this.items.push(item); | ||
} | ||
|
||
removeItem(item) { | ||
const index = this.items.indexOf(item); | ||
if (index > -1) { | ||
this.items.splice(index, 1); | ||
console.log(`Removed ${item} from the order.`); | ||
} else { | ||
console.log(`${item} is not in the order.`); | ||
} | ||
} | ||
|
||
processOrder() { | ||
if (this.items.length > 0) { | ||
console.log(`Order processed: ${this.items.join(", ")}`); | ||
this.items = []; | ||
} else { | ||
console.log("Add products to the order before processing it."); | ||
} | ||
} | ||
|
||
cancelOrder() { | ||
console.log("Order cancelled."); | ||
this.items = []; | ||
} | ||
|
||
showItems() { | ||
console.log( | ||
`Items in the order: ${this.items.join(", ") || "Empty, no items found."}` | ||
); | ||
} | ||
} | ||
|
||
module.exports = Order; |