250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ใ…ฃใ„ท
  • ์œ„์ž„
  • MySQL
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

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

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

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

'์ฑ… ๋ฆฌ๋ทฐ > ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[๋ฆฌํŒฉํ„ฐ๋ง 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
    '์ฑ… ๋ฆฌ๋ทฐ/๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.11 ์Šˆํผํด๋ž˜์Šค๋ฅผ ์œ„์ž„์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
    • [๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.10 ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ์œ„์ž„์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
    • [๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.10์„ ์ฝ๊ธฐ์— ์•ž์„œ ์œ„์ž„์ด๋ž€?
    • [๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.7 ์„œ๋ธŒํด๋ž˜์Šค ์ œ๊ฑฐํ•˜๊ธฐ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”