diff --git a/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/ManagerInvoker.js b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/ManagerInvoker.js new file mode 100644 index 0000000..10ef81f --- /dev/null +++ b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/ManagerInvoker.js @@ -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; diff --git a/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/main.js b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/main.js new file mode 100644 index 0000000..bba80f4 --- /dev/null +++ b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/main.js @@ -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 diff --git a/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/orderCommand.js b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/orderCommand.js new file mode 100644 index 0000000..bc25e70 --- /dev/null +++ b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/orderCommand.js @@ -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, +}; diff --git a/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/receiver.js b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/receiver.js new file mode 100644 index 0000000..a1571ca --- /dev/null +++ b/Learning_2.0/Solutions/Behavioural-pattern-solutions/online-ordering-food-system/receiver.js @@ -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;