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);
์ถ๋ ฅ ๊ฒฐ๊ณผ
'์ฑ ๋ฆฌ๋ทฐ > ๋ฆฌํฉํ ๋ง(์๋ฐ์คํฌ๋ฆฝํธํ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฆฌํฉํฐ๋ง 2ํ] 12.10์ ์ฝ๊ธฐ์ ์์ ์์์ด๋? (0) | 2022.11.28 |
---|---|
[๋ฆฌํฉํฐ๋ง 2ํ] 12.7 ์๋ธํด๋์ค ์ ๊ฑฐํ๊ธฐ (0) | 2022.11.24 |
[๋ฆฌํฉํฐ๋ง 2ํ] 11.13 ์์ธ๋ฅผ ์ฌ์ ํ์ธ์ผ๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.11.24 |
[๋ฆฌํฉํฐ๋ง 2ํ] 11.12 ์ค๋ฅ ์ฝ๋๋ฅผ ์์ธ๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.11.23 |
[๋ฆฌํฉํฐ๋ง 2ํ] 11.8 ์์ฑ์๋ฅผ ํฉํฐ๋ฆฌ ํจ์๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.11.23 |