Created
October 12, 2017 13:56
-
-
Save rikuTanide/5ed8a3b13b046be9562c1ef6df7822a6 to your computer and use it in GitHub Desktop.
状態変数の読み込みと書き込みを局所化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class State { | |
bool aisatu = false; | |
int onigiriPrice = 100; | |
} | |
var state = new State(); | |
void onMessage(String message) { | |
state = updateState(state, message); | |
} | |
State updateState(State _state, String message) { | |
switch (message) { | |
case "こんにちは": | |
return onAisatu(_state); | |
case "おにぎり3個ください": | |
return onOnigiriRequest(_state); | |
} | |
} | |
State onAisatu(State _state) { | |
print("こんにちは"); | |
return new State() | |
..onigiriPrice = _state.onigiriPrice | |
..aisatu = true; | |
} | |
State onOnigiriRequest(State _state) { | |
if (_state.aisatu) { | |
var sum = _state.onigiriPrice * 3; | |
print("$sum 円です"); | |
return new State() | |
..onigiriPrice = _state.onigiriPrice | |
..aisatu = false; | |
} | |
return _state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment