# ICommand.java
public interface ICommand {
public abstract void execute();
}
# Invoker.java
public class Invoker {
public void execute(ICommand command) {
command.execute();
}
}
# ToggleCommand.java
public class ToggleCommand implements ICommand {
private boolean isOn;
public ToggleCommand() {
this.isOn = false;
}
@Override
public void execute() {
if (isOn) {
isOn = false;
System.out.println("boolean: " + this.isOn);
} else {
isOn = true;
System.out.println("boolean: " + this.isOn);
}
}
}
# Main.java
public class Main {
public static void main(String[] args) {
ToggleCommand toggleCommand = new ToggleCommand();
Invoker invoker = new Invoker();
invoker.execute(toggleCommand);
invoker.execute(toggleCommand);
invoker.execute(toggleCommand);
}
}
# 결과
boolean: true
boolean: false
boolean: true
댓글 영역