23가지 디자인 모드 - 명령 모드(러시아 블록)
러시아 블록의 간단한 실현
/**
*
*/
public class TetrisMachine {
/**
* " "
*/
public void toLeft() {
Log.e(" ");
}
public void toRight() {
Log.e(" ");
}
public void fastToBottom() {
Log.e(" ");
}
public void transform() {
Log.e(" ");
}
}
/**
*
*/
public abstract class Command {
protected TetrisMachine machine;
public Command(TetrisMachine machine) {
this.machine = machine;
}
/**
*
*/
abstract void execute();
}
public class LeftCommand extends Command {
public LeftCommand(TetrisMachine machine) {
super(machine);
}
@Override
public void execute() {
machine.toLeft();
}
}
public class RightCommand extends Command {
public RightCommand(TetrisMachine machine) {
super(machine);
}
@Override
void execute() {
machine.toRight();
}
}
public class FallCommand extends Command {
public FallCommand(TetrisMachine machine) {
super(machine);
}
@Override
void execute() {
machine.fastToBottom();
}
}
public class TransformCommand extends Command {
public TransformCommand(TetrisMachine machine) {
super(machine);
}
@Override
void execute() {
machine.transform();
}
}
public class Buttons {
private LeftCommand leftCommand;
private RightCommand rightCommand;
private FallCommand fallCommand;
private TransformCommand transformCommand;
public void setLeftCommand(LeftCommand leftCommand) {
this.leftCommand = leftCommand;
}
public void setRightCommand(RightCommand rightCommand) {
this.rightCommand = rightCommand;
}
public void setFallCommand(FallCommand fallCommand) {
this.fallCommand = fallCommand;
}
public void setTransformCommand(TransformCommand transformCommand) {
this.transformCommand = transformCommand;
}
public void toLeft() {
leftCommand.execute();
}
public void toRight() {
rightCommand.execute();
}
public void fall() {
fallCommand.execute();
}
public void transform() {
transformCommand.execute();
}
}
public class Main {
public static void main(String[] args){
//
TetrisMachine machine=new TetrisMachine();
//4
Buttons buttons=new Buttons();
buttons.setLeftCommand(new LeftCommand(machine));
buttons.setRightCommand(new RightCommand(machine));
buttons.setFallCommand(new FallCommand(machine));
buttons.setTransformCommand(new TransformCommand(machine));
//
buttons.toLeft();
buttons.toRight();
buttons.fall();
buttons.transform();
}
}
복잡해진 것 같은데 Tetris Machine 대상을 직접 만들면 호출 방법이 되지 않을까요???명령자와 청구자가 있는 이유는 명령자는 개폐원칙을 따르고 청구자는 명령의 기록, 취소 등을 실현하기 위한 것이다.