์ฑ… ๋ฆฌ๋ทฐ/๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ)

[๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.10 ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ์œ„์ž„์œผ๋กœ ๋ฐ”๊พธ๊ธฐ(์˜ˆ์ œ 2)

Rainbow๐ŸŒˆCoder 2022. 12. 1. 19:35
728x90

์œ„์ž„ ํฌ์ธํŠธ : ํด๋ž˜์Šค ๋‚ด์˜ ์Šคํƒœํ‹ฑํ•œ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ 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

 

728x90