728x90
반응형
01.커맨드 패턴 적용하기
01.1 Command
public interface Command{
void execute();
}
01.2 Button
public class Button{
private Command command;
pulic Button(command command){
this.command = command;
}
public void press(){
command,execute();
}
public static void main(String[] args){
Button button = new Button(new Command(){
@Override
public void execute(){
}
});
button.press();
button.press();
}
}
02.Concreate Command
02.1 LightOnCommand
public class LightOnCommand implements Command{
private Light light;
public lightOnCommand(Light light){
this.light = light;
}
@Override
public void execute(){
light.on();
}
}
02.2 LightOffCommand
public class LightOffCommand implements Command{
private Light light;
public lightOffCommand(Light light){
this.light = light;
}
@Override
public void execute(){
light.on();
}
}
02.3 GameStartCommand
public class GameStartCommand implements Command{
private Game game;
public GameStartCommand(Game game){
this.game = game;
}
@Override
public void execute(){
game.start();
}
}
02.4 GamaeEndCommand
public class GameEndCommand implements Command{
private Game game;
public GameEndCommand(Game game){
this.game = game;
}
@Override
public void execute(){
game.end();
}
}
03.Button으로 보는 사용
public class Button{
private Command command;
pulic Button(command command){
this.command = command;
}
public void press(){
command,execute();
}
public static void main(String[] args){
Button button = new Button(// 이곳에 넣기);
button.press();
button.press();
}
}
- 불을 켜고 싶은 경우
-
Button button = new Button(new LightOnCommand(new Light()));
- 게임 시작하고 싶은 경우
-
Button button = new Button(new GameStartCommand(new Game()));
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2022-05-12-인터프리터패턴-1부-패턴소개 (0) | 2022.05.12 |
---|---|
2022-05-11-커맨드패턴-3부-장점과단점 (0) | 2022.05.12 |
2022-05-11-커맨드패턴-1부-패턴소개 (0) | 2022.05.12 |
22-04-18-책임연쇄패턴-4부-장점과단점 (0) | 2022.04.18 |
22-04-18-책임연쇄패턴-3부-장점과단점 (0) | 2022.04.18 |
댓글