2022-05-11-커맨드패턴-2부-패턴적용하기
본문 바로가기
CS Study/디자인패턴

2022-05-11-커맨드패턴-2부-패턴적용하기

by KyeongMin 2022. 5. 12.
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()));

https://github.com/3DPIT/3dpit.github.io/blob/main/content/blog/DesignPattern/2022-05-11-%EC%BB%A4%EB%A7%A8%EB%93%9C%ED%8C%A8%ED%84%B4-2%EB%B6%80-%ED%8C%A8%ED%84%B4%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0.md

 

GitHub - 3DPIT/3dpit.github.io

Contribute to 3DPIT/3dpit.github.io development by creating an account on GitHub.

github.com

 

728x90
반응형

댓글