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

[๋ฆฌํŒฉํ„ฐ๋ง 2ํŒ] 12.6 ํƒ€์ž… ์ฝ”๋“œ๋ฅผ ์„œ๋ธŒํด๋ž˜์Šค๋กœ ๋ฐ”๊พธ๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 11. 24. 14:06
728x90

0.  ๋ฆฌํŒฉํ† ๋ง ์ „ ์ „์ฒด ์ฝ”๋“œ

class Employee {
	#name;
	#type;
	constructor(name, type) {
		this.validateType(type);
		this.#name = name;
		this.#type = type;
	}

	validateType(arg) {
		if (!['engineer', 'manager', 'salesperson'].includes(arg)) {
			throw new Error(`${arg}๋ผ๋Š” ์ง์› ์œ ํ˜•์€ ์—†์Šต๋‹ˆ๋‹ค.`);
		}
	}

	get type() {
		return this.#type;
	}

	toString() {
		return `${this.#name} (${this.#type})`;
	}
}


const mandoo = new Employee('๊ธฐํšํ•˜๋Š”๋งŒ๋‘', 'engineer');
const gom = new Employee('์ฝ”๋”ฉ๊ณฐ', 'manager');

 

1.  ๋ฌธ์ œ์ 

constructor(name, type)๋ผ๊ณ  ํ•ด๋‘๋ฉด ์™ธ๋ถ€์—์„œ ์–ด๋–ค type์ด ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๋‹ˆ๊นŒ ์–ด๋–ค type์„ ์ œ๊ณตํ•ด์ค˜์•ผ ํ•  ์ง€ ํŒŒ์•…ํ•˜๋Š” ๋น„์šฉ์ด ๋ฐœ์ƒํ•œ๋‹ค. ํŠนํžˆ๋‚˜ js ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ํƒ€์ž…์ด ๋ช…์‹œ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ํƒ€์ž…์ด ๊ฐ€๋Šฅํ•œ์ง€ ์œ ์ถ”ํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” constructor์—์„œ validateType(type)ํ•ด์ฃผ๋ฉด์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด์ด

['engineer', 'manager', 'salesperson']์— ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ๋˜์ง€๋Š” ์ฝ”๋“œ์ด๋‹ค.

์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ž์—์„œ ์—๋Ÿฌ๋ฅผ ๋˜์ง€๋Š” ๊ฒƒ์€ ์ •๋ง ๋‚˜์œ ๊ด€๋ก€์ด๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ, validateType๋ฅผ ์ƒ์„ฑ์ž์—์„œ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ฒŒ๋” ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค. + ํƒ€์ž… ํ•„๋“œ ์ œ๊ฑฐ

 

2.  ๋ฆฌํŒฉํ„ฐ๋ง ์ „๋žต

(1) ํƒ€์ž… ์ฝ”๋“œ ํ•„๋“œ๋ฅผ ์ž๊ฐ€ ์บก์Аํ™”ํ•œ๋‹ค.

(2) ํƒ€์ž… ์ฝ”๋“œ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. ํƒ€์ž… ์ฝ”๋“œ ๊ฒŒํ„ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ํ•ด๋‹น ํƒ€์ž… ์ฝ”๋“œ์˜ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•œ๋‹ค.

(3) ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํƒ€์ž… ์ฝ”๋“œ์™€ ๋ฐฉ๊ธˆ ๋งŒ๋“  ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋งคํ•‘ํ•˜๋Š” ์„ ํƒ ๋กœ์ง์„ ๋งŒ๋“ ๋‹ค.

(4) ํƒ€์ž… ์ฝ”๋“œ ํ•„๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

(5) ํƒ€์ž… ์ฝ”๋“œ ์ ‘๊ทผ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ ๋ชจ๋‘์— ๋ฉ”์„œ๋“œ ๋‚ด๋ฆฌ๊ธฐ์™€ ์กฐ๊ฑด๋ถ€ ๋กœ์ง์„ ๋‹คํ˜•์„ฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ๋ฅผ ์ ์šฉํ•œ๋‹ค.

 

3. ์˜ˆ์ œ ์ฝ”๋“œ 

(1) ๋ฆฌํŒฉํ„ฐ๋ง ์ „

class Employee {
	#name;
	#type;
	constructor(name, type) {
		this.validateType(type);
		this.#name = name;
		this.#type = type;
	}

	validateType(arg) {
		if (!['engineer', 'manager', 'salesperson'].includes(arg)) {
			throw new Error(`${arg}๋ผ๋Š” ์ง์› ์œ ํ˜•์€ ์—†์Šต๋‹ˆ๋‹ค.`);
		}
	}
}

(2) ๋ฆฌํŒฉํ„ฐ๋ง ํ›„

 

์ฃผ์•ˆ์ : validateType(type)์ด ํ•„์š”์—†๊ฒŒ๋” ๊ณ ์นœ๋‹ค.

๋‘๊ฐ€์ง€ ์Šคํƒ€์ผ๋กœ ๋ฆฌํŒฉํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

 

-1- ์ฒซ๋ฒˆ์งธ ์Šคํƒ€์ผ

ํƒ€์ž…๋ณ„๋กœ ์„œ๋ธŒ ํด๋ž˜์Šค๋“ค์„ ๋งŒ๋“ค์–ด์„œ

ํ•ด๋‹น ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ํƒ€์ž… ๊ฒŒํ„ฐ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์กฐ์น˜ํ•˜๊ณ ,

new๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ์Šˆํผํด๋ž˜์Šค๊ฐ€ ์•„๋‹ˆ๋ผ ์„œ๋ธŒํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.  

class Employee {
	#name;
	constructor(name) {
		this.#name = name;
	}

	get type() {
		return null;
	}
	toString() {
		return `${this.#name} (${this.type})`;
	}
}

class Engineer extends Employee {
	get type() {
		return 'engineer';
	}

}

class Salesperson extends Employee {
	get type() {
		return 'salesperson';
	}
}

class ProductManager extends Employee {
	get type() {
		return 'pm';
	}
}

const mandu = new ProductManager('PM๋งŒ๋‘');
const introduceMandu = mandu.toString();
const gom = new Engineer('์ฝ”๋”ฉ๊ณฐ');
const introduceGom = gom.toString();


console.log(introduceMandu);
console.log(introduceGom);

์ถœ๋ ฅ๊ฒฐ๊ณผ

 

 

-2- ๋‘๋ฒˆ์งธ ์Šคํƒ€์ผ

 

์ง์ ‘์ ์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ ๋•Œ,

 ์Šคํƒœํ‹ฑ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํ™œ์šฉ

class Employee {
	#name;
	constructor(name) {
		this.#name = name;
	}

	get type() {
		return null;
	}
	toString() {
		return `${this.#name} (${this.type})`;
	}
	static createEmployee(name, type) {
		switch (type) {
			case 'engineer':
				return new Engineer(name, type);
			case 'salesperson':
				return new Salesperson(name, type);
			case 'pm':
				return new ProductManager(name, type);
			default:
				throw new Error(`${type}๋ผ๋Š” ์ง์› ์œ ํ˜•์€ ์—†์Šต๋‹ˆ๋‹ค.`);
		}
	}
}

class Engineer extends Employee {
	get type() {
		return 'engineer';
	}

}

class Salesperson extends Employee {
	get type() {
		return 'salesperson';
	}
}

class ProductManager extends Employee {
	get type() {
		return 'pm';
	}
}

const mandu = Employee.createEmployee('PM๋งŒ๋‘', 'pm');
const introduceMandu = mandu.toString();
const gom = Employee.createEmployee('์ฝ”๋”ฉ๊ณฐ', 'engineer');
const introduceGom = gom.toString();
const navi = Employee.createEmployee('๋งˆ์ผ€ํ„ฐ๋‚˜๋น„', 'maketer');
const introduceNavi = navi.toString();

console.log(introduceMandu);
console.log(introduceGom);
console.log(introduceNavi);

์ถœ๋ ฅ ๊ฒฐ๊ณผ

 

 

728x90