CS Study/디자인패턴
2022-05-11-커맨드패턴-2부-패턴적용하기
KyeongMin
2022. 5. 12. 23:57
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()));
GitHub - 3DPIT/3dpit.github.io
Contribute to 3DPIT/3dpit.github.io development by creating an account on GitHub.
github.com
728x90
반응형