์์ ํฌ์ธํธ : ํด๋์ค ๋ด์ ์คํํฑํ ํฉํ ๋ฆฌ ๋ฉ์๋ or ์ต์์ function, ์์-๋ถ๋ชจ ์ฐ๊ฒฐ์ ์ ๊ฑฐ ํ ํ ๊ฐ๊ฐ ์ธ์คํด์ค ์์ฑ
2. ์์2: ์๋ธ ํด๋์ค๊ฐ ์ฌ๋ฌ ๊ฐ์ผ ๋
์๋์ ๊ฐ์ ์ฝ๋๊ฐ ์๋ค๊ณ ๊ฐ์ ํ๋ค.
์ด ์ฝ๋๋ ๊ณง ์ผ์ ์กฐ๋ฅ์ ์ฌ์ก ์กฐ๋ฅ๋ฅผ ๊ตฌ๋ถ ์ง๊ธฐ ์ํด ํฌ๊ฒ ์์ ํ ์์ ์ด๋ค. (์ด ์ฐจ์ด๋ฅผ WildBird์ CaptiveBird๋ผ๋ ๋ ์๋ธํด๋์ค๋ก ๋ชจ๋ธ๋งํ๋ ๋ฐฉ๋ฒ๋ ์๋ค)
์์์ ํ๋ฒ๋ง ์ธ ์ ์์ผ๋ ์ผ์๊ณผ ์ฌ์ก์ ๊ธฐ์ค์ผ๋ก ๋๋๋ ค๋ฉด ์ข ์ ๋ฐ๋ฅธ ๋ถ๋ฅ๋ฅผ ํฌ๊ธฐํด์ผ ํ๋ค.
function createBird(data) {
switch (data.type) {
case '์ ๋ฝ ์ ๋น':
return new EuropeanSwallow(data);
break;
case '์ํ๋ฆฌ์นด ์ ๋น':
return new AfricanSwallow(data);
break;
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return new NorwegianBlueSwallow(data);
break;
default:
return new Bird(data);
}
}
class Bird {
constructor(data) {
this._name = data.name;
this._plumage = data.plumage;
}
get name() {
return this._name;
}
get plumage() {
return this._plumage || '๋ณดํต์ด๋ค';
}
get airSpeedVelocity() {
return null;
}
}
class EuropeanSwallow extends Bird {
get airSpeedVelocity() {
return 35;
}
}
class AfricanSwallow extends Bird {
constructor(data) {
super(data);
this._numberOfCoconuts = data.numberOfCoconuts;
}
get airSpeedVelocity() {
return 40 - 2 * this._numberOfCoconuts;
}
}
class NorwegianBlueSwallow extends Bird {
constructor(data) {
super(data);
this._voltage = data.voltage;
this._isNailed = data.isNailed;
}
get plumage() {
if (this._voltage > 100) return '๊ทธ์๋ ธ๋ค';
else return this._plumage || '์์๋ค';
}
get airSpeedVelocity() {
return (this._isNailde) ? 0 : 10 + this._voltage / 10;
}
}
์์2 : ์์์ ์์์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ์ํ ์ฝ๋ ๋ฆฌํฉํ ๋ง
*์์๋ ์ฑ ๊ณผ ๋ค๋ฅด๊ฒ ๊ธฐ์ฌํ์์ต๋๋ค
(1) ์ญ์, ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ํ์ฉํด๋ณด์
function createBird(data) {
return new Bird(data);
}
(2) Bird ํด๋์ค์ ์์ฑ์๋ฅผ ์๋์ ๊ฐ์ด ๋ณ๊ฒฝ
constructor(data) {
this._name = data.name;
this._plumage = data.plumage;
this._speciesPlumge = this.selectSpeciesDelegate(data);
}
(3) Bird ํด๋์ค ๋ด ๊นํธ ์ํ๋ ๊ณต์ ๊ฒํฐ๋ ์คํ์ ๋ธ๋ฆฌ๊ฒ์ดํธ์์ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ก ๋ณ๊ฒฝ
get plumage() {
return this._speciesDelegate.plumage;
}
get airSpeedVelocity() {
return this._speciesDelegate.airSpeedVelocity;
}
(4) Bird ํด๋์ค ๋ด selectSpecialDelegate๋ฅผ ์์ฑํด์ค๋ค.
selectSpecialDelegate๋ฅผ ํธ์ถํด์ผ์ง ํด๋น data์ ํด๋นํ๋ ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ๋ง๋ค ์ ์๋๋ก
selectSpecialDelegate(data) {
switch (data.type) {
case '์ ๋ฝ ์ ๋น':
return new EuropeanSwallowDelegate(data, this);
break;
case '์ํ๋ฆฌ์นด ์ ๋น':
return new AfricanSwallowDelegate(data, this);
break;
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return new NorwegianBlueSwallowDelegate(data, this);
break;
default:
return new SpeciesDelegate(data, this);
}
}
(6) ํด๋์ค SpeciesDelegate ์์ฑ
class SpeciesDelegate {
constructor(data, bird) {
this._bird = bird;
}
get plumge() {
return this._bird._plumage || '๋ณดํต์ด๋ค';
}
get airSpeedVelocity() {
return null;
}
}
(7) ์๋ธ ํด๋์ค๋ค์ด SpeciesDelegate๋ฅผ ํ์ฅ๋ฐ๋๋ก ํ๊ณ , ํด๋์ค๋ช ์ ์ ํ๊ฒ ๋ณ๊ฒฝ
class EuropeanSwallowDelegate extends SpeciesDelegate {
get airSpeedVelocity() {
return 35;
}
}
(8) ์ญ์ฐธ์กฐ ์ถ๊ฐ
NorwegianBlueSwallowDelegate๋ ๋ค๋ฅธ ์๋ธ ํด๋์ค์๋ ๋ค๋ฅด๊ฒ ๊นํธ ์ํ, ์ฆ plumage()๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํ๋ค. ์์ฑ์์ Bird๋ก์ ์ญ์ฐธ์กฐ๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค.
class AfricanSwallowDelegate extends SpeciesDelegate {
constructor(data, bird) {
super(data, bird);
this._numberOfCoconuts = data.numberOfCoconuts;
}
get airSpeedVelocity() {
return 40 - 2 * this._numberOfCoconuts;
}
}
class NorwegianBlueSwallowDelegate extends SpeciesDelegate {
constructor(data, bird) {
super(data, bird);
this._voltage = data.voltage;
this._isNailed = data.isNailed;
}
get airSpeedVelocity() {
return (this._isNailde) ? 0 : 10 + this._voltage / 10;
}
get plumge() {
if (this._voltage > 100) return '๊ทธ์๋ ธ๋ค';
else return thisthis._bird._plumage || '์์๋ค';
}
}
์์2 ๋ฆฌํฉํ ๋ง ํ ์ ์ฒด ์ฝ๋
function createBird(data) {
return new Bird(data);
}
class Bird {
constructor(data) {
this._name = data.name;
this._plumage = data.plumage;
this._speciesPlumge = this.selectSpeciesDelegate(data);
}
get name() { return this._name; }
get plumage() {
return this._speciesDelegate.plumage;
}
get airSpeedVelocity() {
return this._speciesDelegate.airSpeedVelocity;
}
selectSpeciesDelegate(data) {
switch (data.type) {
case '์ ๋ฝ ์ ๋น':
return new EuropeanSwallowDelegate(data, this);
break;
case '์ํ๋ฆฌ์นด ์ ๋น':
return new AfricanSwallowDelegate(data, this);
break;
case '๋
ธ๋ฅด์จ์ด ํ๋ ์ต๋ฌด':
return new NorwegianBlueSwallowDelegate(data, this);
break;
default:
return new SpeciesDelegate(data, this);
}
}
}
class SpeciesDelegate {
constructor(data, bird) {
this._bird = bird;
}
get plumge() {
return this._bird._plumage || '๋ณดํต์ด๋ค';
}
get airSpeedVelocity() {
return null;
}
}
class EuropeanSwallowDelegate extends SpeciesDelegate {
get airSpeedVelocity() {
return 35;
}
}
class AfricanSwallowDelegate extends SpeciesDelegate {
constructor(data, bird) {
super(data, bird);
this._numberOfCoconuts = data.numberOfCoconuts;
}
get airSpeedVelocity() {
return 40 - 2 * this._numberOfCoconuts;
}
}
class NorwegianBlueSwallowDelegate extends SpeciesDelegate {
constructor(data, bird) {
super(data, bird);
this._voltage = data.voltage;
this._isNailed = data.isNailed;
}
get airSpeedVelocity() {
return (this._isNailde) ? 0 : 10 + this._voltage / 10;
}
get plumge() {
if (this._voltage > 100) return '๊ทธ์๋ ธ๋ค';
else return thisthis._bird._plumage || '์์๋ค';
}
}
์์2 ๋ฆฌํฉํ ๋ง : Bird๋ฅผ ์์์ผ๋ก๋ถํฐ ๊ตฌ์ ํ ๊ฒ ์ธ์ ์ด ๋ฆฌํฉํฐ๋ง์์ ์ป์ ๊ฒ
์์2 ๋ฆฌํฉํ ๋ง์ ์๋์ ์๋ธํด๋์ค๋ค์ ์์์ผ๋ก ๊ต์ฒดํ์ง๋ง SpeciesDelegate์๋ ์ฌ์ ํ ์ฒ์ ๊ตฌ์กฐ์ ๋งค์ฐ ๋น์ทํ ๊ณ์ธต ๊ตฌ์กฐ๊ฐ ์กด์ฌํ๋ค. ์์์ผ๋ก ์ฎ๊ฒจ์ง ์ข ๊ณ์ธต๊ตฌ์กฐ๋ ๋ ์๊ฒฉํ๊ฒ ์ข ๊ณผ ๊ด๋ จํ ๋ด์ฉ๋ง์ ๋ค๋ฃจ๊ฒ ๋์๋ค. ์์ ํด๋์ค๋ค์ ์ข ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ ๋ฐ์ดํฐ์ ๋ฉ์๋๋ง์ ๋ด๊ฒ ๋๊ณ ์ข ๊ณผ ์๊ด์๋ ๊ณตํต ์ฝ๋๋ Bird ์์ฒด์ ๋ฏธ๋์ ์๋ธํด๋์ค๋ค์ ๋จ๋๋ค.$
์ฑ ๊ณผ ์์๋๋ก ๋ฆฌํฉํ ๋ง์ ์ฐธ๊ณ ํ๊ณ ์ถ๋ค๋ฉด! : https://eunjin3786.tistory.com/405
๋ฆฌํฉํฐ๋ง (7) - ํ์ ์ฝ๋๋ฅผ ์๋ธํด๋์ค๋ก / ์๋ธํด๋์ค๋ฅผ ์์์ผ๋ก
๋งํด ํ์ธ๋ฌ - ๋ฆฌํฉํฐ๋ง (2ํ) ์ 12์ฅ - ์์ ๋ค๋ฃจ๊ธฐ ์ค ์ข์๋ ๊ฒ๋ค ๊ธฐ๋ก โ๏ธโ๏ธ 12.6 ํ์ ์ฝ๋๋ฅผ ์๋ธํด๋์ค๋ก ๋ฐ๊พธ๊ธฐ (Replace Type Code with Subclasses) ๋ณดํต ์ด๊ฑฐํ, ๋ฌธ์์ด, ์ซ์ ๋ฑ์ ํ์ ์ฝ๋๋ฅผ
eunjin3786.tistory.com
'์ฑ ๋ฆฌ๋ทฐ > ๋ฆฌํฉํ ๋ง(์๋ฐ์คํฌ๋ฆฝํธํ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฆฌํฉํฐ๋ง 2ํ] 12.11 ์ํผํด๋์ค๋ฅผ ์์์ผ๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.12.06 |
---|---|
[๋ฆฌํฉํฐ๋ง 2ํ] 12.10 ์๋ธํด๋์ค๋ฅผ ์์์ผ๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.11.30 |
[๋ฆฌํฉํฐ๋ง 2ํ] 12.10์ ์ฝ๊ธฐ์ ์์ ์์์ด๋? (0) | 2022.11.28 |
[๋ฆฌํฉํฐ๋ง 2ํ] 12.7 ์๋ธํด๋์ค ์ ๊ฑฐํ๊ธฐ (0) | 2022.11.24 |
[๋ฆฌํฉํฐ๋ง 2ํ] 12.6 ํ์ ์ฝ๋๋ฅผ ์๋ธํด๋์ค๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.11.24 |