Command
by Artem
JavaScript
'use strict';
class GraphicsEditor {
constructor() {
this.canvas = [];
}
show() {
if (this.canvas.length > 0) {
console.log(`There are ${this.canvas} on the canvas.`);
} else if (this.canvas.length === 1) {
console.log(`There is ${this.canvas} on the canvas.`);
} else {
console.log(`There is nothing on the canvas.`);
}
}
operation(type, options) {
if (type === undefined) {
this.show();
return;
}
console.log(`Drawing... ${type} with params: ${options.weight}`);
this.canvas.push(type);
}
}
class DrawCommand {
execute(editor, type, options) {
editor.operation(type, options)
}
}
class ShowCommand {
execute(editor, type) {
editor.operation(type)
}
}
class User {
constructor() {
this.commands = [];
this.editor = new GraphicsEditor();
}
draw(type, options) {
const command = new DrawCommand();
this.commands.push(command);
command.execute(this.editor, type, options);
}
showPainting() {
const command = new ShowCommand();
this.commands.push(command);
command.execute(this.editor);
}
}
const user = new User();
user.draw('line', {
weight: 'bold'
});
user.draw('line', {
weight: 'normal'
});
user.showPainting();