Command pattern

This commit is contained in:
kim
2021-01-20 23:30:00 +09:00
parent 77b361ba96
commit aa29dc4df1
4 changed files with 49 additions and 0 deletions

View 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();
}
}
}

View File

@@ -0,0 +1,2 @@
Command Pattern
- 명령의 객체화

View File

@@ -0,0 +1,6 @@
package Command;
public interface Command extends Comparable<Command>{
void execute();
}

View 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();
}
}