Skip to content

Instantly share code, notes, and snippets.

@graynun
Created July 27, 2020 11:29
Show Gist options
  • Save graynun/933c211b348b83026fd15e917a388ede to your computer and use it in GitHub Desktop.
Save graynun/933c211b348b83026fd15e917a388ede to your computer and use it in GitHub Desktop.
Replace Conditional with Polymorphism
function plumages(birds) {
return new Map(birds.map(b => [b.name, plumage(b)]));
}
function speeds(birds) {
return new Map(birds.map(b => [b.name, airSpeedVelocity(b)]));
}
function plumage(bird) {
switch (bird.type) {
case '유럽 제비':
return "보통이다";
case '아프리카 제비':
return (bird.numberOfCoconuts > 2) ? "지쳤다" : "보통이다";
case '노르웨이 파랑 앵무':
return (bird.voltage > 100) ? "그을렸다" : "예쁘다";
default:
return "알 수 없다";
}
}
function airSpeedVelocity(bird) {
switch (bird.type) {
case '유럽 제비':
return 35;
case '아프리카 제비':
return 40 - 2 * bird.numberOfCoconuts;
case '노르웨이 파랑 앵무':
return (bird.isNailed) ? 0 : 10 + bird.voltage / 10;
default:
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment