# ICommand.java
public interface ICommand {
public abstract void exeute();
}
# CommandManager.java
import java.util.ArrayList;
public class CommandManager implements ICommand {
private ArrayList<ICommand> commands = new ArrayList<>();
@Override
public void exeute() {
for (ICommand command : commands)
command.exeute();
}
public boolean add(ICommand command) {
if (command != this) {
commands.add(command);
return true;
}
return false;
}
public void remove(ICommand command) {
if (commands.size() != 0)
commands.remove(command);
}
public void clear() {
commands.clear();
}
}
# Command01.java
public class Command01 implements ICommand {
@Override
public void exeute() {
System.out.println("This is a command 01.");
}
}
# Command02.java
public class Command02 implements ICommand {
@Override
public void exeute() {
System.out.println("This is a command 02.");
}
}
# Main.java
public class Main {
public static void main(String[] args) {
CommandManager commandManager = new CommandManager();
ICommand command01 = new Command01();
ICommand command02 = new Command02();
commandManager.add(command01);
commandManager.add(command02);
commandManager.exeute();
commandManager.remove(command02);
System.out.println("==============================");
commandManager.add(new ICommand() {
@Override
public void exeute() {
System.out.println("This is a new command that now added.");
}
});
commandManager.exeute();
}
}
# 결과
This is a command 01.
This is a command 02.
==============================
This is a command 01.
This is a new command that now added.
댓글 영역