added command pattern example
This commit is contained in:
parent
7c3d0b2cbd
commit
3a2484a123
5
src/learn/simple_command/Command.java
Normal file
5
src/learn/simple_command/Command.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package learn.simple_command;
|
||||||
|
|
||||||
|
public interface Command {
|
||||||
|
void execute();
|
||||||
|
}
|
14
src/learn/simple_command/ConcreteCommand.java
Normal file
14
src/learn/simple_command/ConcreteCommand.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
13
src/learn/simple_command/Invoker.java
Normal file
13
src/learn/simple_command/Invoker.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
12
src/learn/simple_command/Main.java
Normal file
12
src/learn/simple_command/Main.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
7
src/learn/simple_command/Receiver.java
Normal file
7
src/learn/simple_command/Receiver.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package learn.simple_command;
|
||||||
|
|
||||||
|
public class Receiver {
|
||||||
|
public void action() {
|
||||||
|
System.out.println("Do my action");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user