Command pattern
This commit is contained in:
20
design-pattern/src/Command/Application.java
Normal file
20
design-pattern/src/Command/Application.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package Command;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
PriorityQueue<Command> queue = new PriorityQueue<>();
|
||||
|
||||
queue.add(new StringPrintCommand("A"));
|
||||
queue.add(new StringPrintCommand("AB"));
|
||||
queue.add(new StringPrintCommand("ABC"));
|
||||
queue.add(new StringPrintCommand("ABCD"));
|
||||
|
||||
for (Command command : queue) {
|
||||
command.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
design-pattern/src/Command/Command
Normal file
2
design-pattern/src/Command/Command
Normal file
@@ -0,0 +1,2 @@
|
||||
Command Pattern
|
||||
- 명령의 객체화
|
||||
6
design-pattern/src/Command/Command.java
Normal file
6
design-pattern/src/Command/Command.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package Command;
|
||||
|
||||
public interface Command extends Comparable<Command>{
|
||||
|
||||
void execute();
|
||||
}
|
||||
21
design-pattern/src/Command/StringPrintCommand.java
Normal file
21
design-pattern/src/Command/StringPrintCommand.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package Command;
|
||||
|
||||
public class StringPrintCommand implements Command{
|
||||
|
||||
private final String string;
|
||||
|
||||
public StringPrintCommand(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println(this.string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Command o) {
|
||||
return this.string.length() - ((StringPrintCommand) o).string.length();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user