added command pattern example

This commit is contained in:
Louis Heredero 2024-10-13 18:22:53 +02:00
parent 7c3d0b2cbd
commit 3a2484a123
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package learn.simple_command;
public interface Command {
void execute();
}

View File

@ -0,0 +1,14 @@
package learn.simple_command;
public class ConcreteCommand implements Command {
private Receiver receiver;
public ConcreteCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.action();
}
}

View File

@ -0,0 +1,13 @@
package learn.simple_command;
public class Invoker {
private Command slot;
public void setSlot(Command slot) {
this.slot = slot;
}
public void buttonWasPushed() {
slot.execute();
}
}

View File

@ -0,0 +1,12 @@
package learn.simple_command;
public class Main {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
invoker.setSlot(command);
invoker.buttonWasPushed();
}
}

View File

@ -0,0 +1,7 @@
package learn.simple_command;
public class Receiver {
public void action() {
System.out.println("Do my action");
}
}