/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank"></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
interface Command {
public void executeDraw();
public void executeErase();
}
public static class ShapeConcreteCommand implements Command {
final Receiver logic; // business logic
final String shape; // parametro metodo
public ShapeConcreteCommand(Receiver logic, String shape) {
this.logic = logic;
this.shape = shape;
}
public void executeDraw() {
logic.draw(shape); // trasforma una chiamata a una logica, come un oggetto autonomo
}
public void executeErase() {
logic.erase(shape);
}
}
public static class Receiver {
// business logic
public void draw(String shape) {
System.out.println("Drawing " + shape);
}
public void erase(String shape) {
System.out.println("Erasing " + shape);
}
}
public static class Invoker {
Command command;
public void setCommand(Command command) {
this.command = command;
}
// avvia la richiesta del client
public void draw() {
command.executeDraw();
}
public void erase() {
command.executeErase();
}
}
public static void main(String args[]) {
// Unit test - uso del design pattern: Command
// invocker: avvia una richieta [Command]
Invoker sketchBoard = new Invoker();
// receiver: business logic
Receiver drawShape = new Receiver();
// Command: esegue una richiesta [receiver, parametri]
ShapeConcreteCommand commad = new ShapeConcreteCommand(drawShape, "circle");
sketchBoard.setCommand(commad);
sketchBoard.draw();
sketchBoard.erase();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: