티스토리 뷰

Java

Command Pattern - object dispatching

techbard 2016.07.18 14:53



# ICommand.java


public interface ICommand {

    public abstract void execute();

}



# Invoker.java


public class Invoker {

    public void execute(ICommand command) {

command.execute();

    }

}



# ToggleCommand.java


public class ToggleCommand implements ICommand {

    private boolean isOn;

    

    public ToggleCommand() {

this.isOn = false;

    }


    @Override

    public void execute() {

if (isOn) {

   isOn = false;

   System.out.println("boolean: " + this.isOn);

} else {

   isOn = true;

   System.out.println("boolean: " + this.isOn);

}

    }

}



# Main.java


public class Main {

    public static void main(String[] args) {

ToggleCommand toggleCommand = new ToggleCommand();

Invoker invoker = new Invoker();


invoker.execute(toggleCommand);

invoker.execute(toggleCommand);

invoker.execute(toggleCommand);

    }

}



# 결과


boolean: true

boolean: false

boolean: true



댓글
댓글쓰기 폼
공지사항
Total
407,786
Today
15
Yesterday
38
«   2019/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
글 보관함