Replace Conditional With Polymorphism, ์กฐ๊ฑด๋ถ ๋ก์ง์ ๋คํ์ฑ์ผ๋ก ๋ฐ๊พธ๊ธฐ๋ฅผ ์์๋ณด์.
์์ฝ
์ฝ๋
switch (bird.type) {
case '์ ๋ฝ ์ ๋น':
return "๋ณดํต์ด๋ค";
case '์ํ๋ฆฌ์นด ์ ๋น':
return (bird.numberOfCoconuts > 2) ? "์ง์ณค๋ค" : "๋ณดํต์ด๋ค";
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return (bird.voltage > 100) ? "๊ทธ์๋ ธ๋ค" : "์์๋ค";
default:
return "์ ์ ์๋ค";
}
class EuropeanSwallow {
get plumage() {
return "๋ณดํต์ด๋ค";
}
}
class AfricanSwallow {
get plumage() {
return (this.numberOfCoconuts > 2) ? "์ง์ณค๋ค" : "๋ณดํต์ด๋ค";
}
}
class NorwegianBlueSwallow {
get plumage() {
return (this.voltage > 100) ? "๊ทธ์๋ ธ๋ค" : "์์๋ค";
}
}
๋ฐฐ๊ฒฝ
- ์กฐ๊ฑด๋ถ ๋ก์ง์ ํ๋ก๊ทธ๋จ์ ์ฝ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ ๋ค.
- ์ด๋ป๊ฒ ํ๋ฉด ์ง๊ด์ ์ผ๋ก ๊ตฌ์กฐํํ ์ ์์๊น?
- ๋ ๋์ ์์ค์ ๊ฐ๋ ์ ๋์ ํ๋ฉด ์ด๋ฅผ ๋ถ๋ฆฌ์ํฌ ์ ์๋ค.
- ํด๋์ค์ ๋คํ์ฑ์ด๋ค.
- ๊ฐ ํ์ ์ด ์์ ๋ง์ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋๋ก ๊ตฌํํ๋ฉด ์ข๊ฒ ๋ค.
์ ์ฐจ
- ๋คํ์ ๋์์ ํํํ๋ ํด๋์ค๋ค์ด ์๋ค๋ฉด ํ๋ ๋ง๋ ๋ค. ๊ธฐ์์ด๋ฉด ์ธ์คํด์ค๋ฅผ ์์์ ๋ง๋ค์ด ๋ฐํํ๋ ํฉํ ๋ฆฌ ํจ์๋ ํจ๊ป ๋ง๋ ๋ค. (์ฌ์ฉํ๋์ชฝ์์ ์ด๊ฒ ๋ ํธํ๋ค.)
- ํธ์ถํ๋ ์ชฝ์์ ํฉํ ๋ฆฌ ํจ์๋ฅผ ์ฌ์ฉํ๋๋ก ์์ ํ๋ค.
- ์กฐ๊ฑด๋ถ ๋ก์ง์ ์ผ๋จ ์ํผ ํด๋์ค๋ก ์ฎ๊ธด๋ค.
- ์๋ธ ํด๋์ค ์ค ํ๋๋ฅผ ์ ํํ๋ค. ์กฐ๊ฑด๋ถ ๋ก์ง ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํ๋ค.
- ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ค๋ฅธ ์๋ธ ํด๋์ค์๋ ์ ์ฉํ๋ค.
- ์ํผ ํด๋์ค์๋ ๊ธฐ๋ณธ ๋์๋ง ๋จ๊ธด๋ค.
์์
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;
}
}
- ์์ ์ข ์ ๋ฐ๋ฅธ ๋นํ ์๋์ ๊นํธ ์ํ๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
- ์ ์ข ๋ฅ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์ํ๋ ํจ์๊ฐ ๋ช ๊ฐ ๋ณด์ธ๋ค.
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) {
new Bird(bird).plumage;
}
function airSpeedVelocity(bird) {
new Bird(bird).airSpeedVelocity;
}
class Bird {
constructor(birdObject) {
Object.assign(this, birdObject);
}
get plumage() {
switch (this.type) {
case '์ ๋ฝ ์ ๋น':
return "๋ณดํต์ด๋ค";
case '์ํ๋ฆฌ์นด ์ ๋น':
return (this.numberOfCoconuts > 2) ? "์ง์ณค๋ค" : "๋ณดํต์ด๋ค";
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return (this.voltage > 100) ? "๊ทธ์๋ ธ๋ค" : "์์๋ค";
default:
return "์ ์ ์๋ค";
}
}
get airSpeedVelocity() {
switch (this.type) {
case '์ ๋ฝ ์ ๋น':
return 35;
case '์ํ๋ฆฌ์นด ์ ๋น':
return 40 - 2 * this.numberOfCoconuts;
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return (this.isNailed) ? 0 : 10 + this.voltage / 10;
default:
return null;
}
}
}
- ์ผ๋จ ์ํผ ํด๋์ค๋ฅผ ๋ง๋ค์๊ณ , ๊ทธ ์์ ์๋ธํด๋์ค๋ฅผ ๋ง๋ค์.
- ์ ํฉํ ๋ฆฌ ํจ์๋ ์ถ๊ฐํด์ฃผ์.
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) {
createBird(bird).plumage;
}
function airSpeedVelocity(bird) {
createBird(bird).airSpeedVelocity;
}
function createBird(bird) {
switch (bird.type) {
case '์ ๋ฝ ์ ๋น':
return new EuropeanSwallow(bird);
case '์ํ๋ฆฌ์นด ์ ๋น':
return new AfricanSwallow(bird);
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return new NorwegianBlueSwallow(bird);
default:
return new Bird(bird);
}
}
class Bird {
constructor(birdObject) {
Object.assign(this, birdObject);
}
get plumage() {
switch (this.type) {
case '์ ๋ฝ ์ ๋น':
return "๋ณดํต์ด๋ค";
case '์ํ๋ฆฌ์นด ์ ๋น':
return (this.numberOfCoconuts > 2) ? "์ง์ณค๋ค" : "๋ณดํต์ด๋ค";
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return (this.voltage > 100) ? "๊ทธ์๋ ธ๋ค" : "์์๋ค";
default:
return "์ ์ ์๋ค";
}
}
get airSpeedVelocity() {
switch (this.type) {
case '์ ๋ฝ ์ ๋น':
return 35;
case '์ํ๋ฆฌ์นด ์ ๋น':
return 40 - 2 * this.numberOfCoconuts;
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return (this.isNailed) ? 0 : 10 + this.voltage / 10;
default:
return null;
}
}
}
class EuropeanSwallow extends Bird {
}
class AfricanSwallow extends Bird {
}
class NorwegianBlueSwallow extends Bird {
}
- ํฉํ ๋ฆฌ ํจ์๋ฅผ ํ๋ ๋ง๋ค์๊ณ , ์ด๊ฑธ ํธ์ถํ๋๋ก ๋ณ๊ฒฝํ๋ค.
- ๊ทธ๋ฆฌ๊ณ ์๋ธ ํด๋์ค๋ค์ ์ผ๋จ ๋ง๋ค์ด๋ดค๋ค.
- ๋ก์ง์ด ๋ถ๋ฆฌ๋์ง๋ ์์์ผ๋ ์ด๊ฑธ ์ฒ๋ฆฌํด๋ณด์.
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) {
createBird(bird).plumage;
}
function airSpeedVelocity(bird) {
createBird(bird).airSpeedVelocity;
}
function createBird(bird) {
switch (bird.type) {
case '์ ๋ฝ ์ ๋น':
return new EuropeanSwallow(bird);
case '์ํ๋ฆฌ์นด ์ ๋น':
return new AfricanSwallow(bird);
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return new NorwegianBlueSwallow(bird);
default:
return new Bird(bird);
}
}
class Bird {
constructor(birdObject) {
Object.assign(this, birdObject);
}
get plumage() {
return "์ ์ ์๋ค";
}
get airSpeedVelocity() {
return null;
}
}
class EuropeanSwallow extends Bird {
get plumage() {
return "๋ณดํต์ด๋ค";
}
}
class AfricanSwallow extends Bird {
get plumage() {
return (this.numberOfCoconuts > 2) ? "์ง์ณค๋ค" : "๋ณดํต์ด๋ค";
}
}
class NorwegianBlueSwallow extends Bird {
get plumage() {
return (this.voltage > 100) ? "๊ทธ์๋ ธ๋ค" : "์์๋ค";
}
}