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 대상을 직접 만들면 호출 방법이 되지 않을까요???명령자와 청구자가 있는 이유는 명령자는 개폐원칙을 따르고 청구자는 명령의 기록, 취소 등을 실현하기 위한 것이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.