Skip to content

Instantly share code, notes, and snippets.

@VintageAppMaker
Last active August 25, 2022 06:00
Show Gist options
  • Save VintageAppMaker/f8d04bfa6e2ac4d05bfc2973e85acae6 to your computer and use it in GitHub Desktop.
Save VintageAppMaker/f8d04bfa6e2ac4d05bfc2973e85acae6 to your computer and use it in GitHub Desktop.
void main(List<String> arguments) {
ifElseExample(3);
fnTableExample(80);
fnTableExample2(100);
print('종료');
}
// 1. if else 처리
void ifElseExample(int n) {
if(n < 10){
print("10 이하");
} else if (n >= 10 && n < 50){
print("10이상 50이하");
} else if (n >= 50 && n < 80){
print("50이상 80이하");
} else if ( n >= 80 && n < 100){
print("80이상 100 이하");
} else if ( n == 100){
print("100");
}
}
// 2. 함수테이블을 이용한 조건처리
void fnTableExample(int n) {
// 코드가 커지면 분리하여 관리
var fnAction1 = (){ print("10 이하"); };
var fnAction2 = (){ print("10이상 50이하"); };
var fnAction3 = (){ print("50이상 80 이하"); };
var fnAction4 = (){ print("80이상 100 이하"); };
var fnAction5 = (){ print("100"); return;};
// 상태테이블
var fnProcess = [
[(){return (n < 10); }, fnAction1],
[(){return (n >= 10 && n < 50); }, fnAction2],
[(){return (n >= 50 && n < 80); }, fnAction3],
[(){return (n >= 80 && n < 100 ); },fnAction4],
[(){return (n == 100 ); }, fnAction5],
].forEach((element) {
if(element[0]() == true){
element[1]();
return;
}
});
}
// 3. 상태관리자를 통한 조건처리
void fnTableExample2(int n) {
// 코드가 커지면 분리하여 관리
var fnAction1 = (){ print("10 이하"); };
var fnAction2 = (){ print("10이상 50이하"); };
var fnAction3 = (){ print("50이상 80 이하"); };
var fnAction4 = (){ print("80이상 100 이하"); };
var fnAction5 = (){ print("100"); return;};
// 상태정의
var state = stateProcess();
state.addIf(condition : (){return (n < 10); }, onAction: fnAction1);
state.addIf(condition : (){return (n >= 10 && n < 50); }, onAction: fnAction2);
state.addIf(condition : (){return (n >= 50 && n < 80); }, onAction: fnAction3);
state.addIf(condition : (){return (n >= 80 && n < 100 ); },onAction: fnAction4);
state.addIf(condition : (){return (n == 100 ); }, onAction: fnAction5);
state.process();
}
// 상태관리 클래스
class stateProcess{
late List fnProcess;
stateProcess(){
// 비교문에 무시(false)되는 초기값
fnProcess =[[(){return false;}, (){}]];
}
void addIf({required Function condition, required Function onAction}){
fnProcess.add([condition, onAction]);
}
void process (){
fnProcess.forEach((element) {
if ( element[0]() == true) {
element[1]();
return;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment