بررسی الگوی Command در جاوا اسکریپت
نویسنده: ابوالفضل روشن ضمیر
تاریخ: ۱۴۰۰/۰۷/۲۰ ۷:۱۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
class Command {
execute() {};
}
//TurnOnPrinter command
class TurnOnPrinter extends Command {
constructor(printingMachine) {
super();
this.printingMachine = printingMachine;
this.commandName = "turn on"
}
execute() {
this.printingMachine.turnOn();
}
}
//TurnOffPrinter command
class TurnOffPrinter extends Command {
constructor(printingMachine) {
super();
this.printingMachine = printingMachine;
this.commandName = "turn off"
}
execute() {
this.printingMachine.turnOff();
}
}
//Print command
class Print extends Command {
constructor(printingMachine) {
super();
this.printingMachine = printingMachine;
this.commandName = "print"
}
execute() {
this.printingMachine.print();
}
}
//Invoker
class PrinterControlPanel {
pressButton(command) {
console.log(`Pressing ${command.commandName} button`);
command.execute();
}
}
//Reciever:
class PrintingMachine {
turnOn() {
console.log('Printing machine has been turned on');
}
turnOff() {
console.log('Printing machine has been turned off');
}
print(){
console.log('The printer is printing your document')
}
}
const printingMachine = new PrintingMachine();
const turnOnCommand = new TurnOnPrinter(printingMachine);
const turnOffCommand = new TurnOffPrinter(printingMachine);
const printCommand = new Print(printingMachine)
const controlPanel = new PrinterControlPanel();
controlPanel.pressButton(turnOnCommand);
controlPanel.pressButton(turnOffCommand);
controlPanel.pressButton(printCommand); class PrintingMachine {
turnOn() {
console.log('Printing machine has been turned on');
}
turnOff() {
console.log('Printing machine has been turned off');
}
print(){
console.log('The printer is printing your document')
}
} class TurnOnPrinter extends Command {/*code*/}
class TurnOffPrinter extends Command {/*code*/}
class Print extends Command {/*code*/} class Command {
execute() {};
} class TurnOnPrinter extends Command {
constructor(printingMachine) {
super();
this.printingMachine = printingMachine;
this.commandName = "turn on"
}
execute() {
this.printingMachine.turnOn();
}
} class TurnOffPrinter extends Command {
//code...
this.commandName = "turn off"
//code..
}
class Print extends Command {
//code...
this.commandName = "print"
//code..
} class TurnOffPrinter extends Command {
//code...
execute() {
this.printingMachine.turnOff();
}
}
class Print extends Command {
//code...
execute() {
this.printingMachine.print();
}
} class PrinterControlPanel {
pressButton(command) {
console.log(`Pressing ${command.commandName} button`);
command.execute();
}
} controlPanel.pressButton(turnOnCommand);
Printing machine has been turned on