๋ฐ์ํ
๐ ๋ชฉ์ฐจ
- ๋ฏน์ค์ธ ํจํด์ด๋?
- ๋ฏน์ค์ธ ํจํด
- ๋ฏน์ค์ธ ํจํด ์ฌํ
- ๊ฒฐ๋ก
๐ ๋ฏน์ค์ธ ํจํด์ด๋?
์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ํจ์๋ค์ ๊ฐ์ฒด๋ ํด๋์ค์ ์ถ๊ฐํ๋ ํจํด์ ๋๋ค. Vue์ React์์๋ ๋น์ทํ ์์ ๋ฅผ ์ฐพ์๋ณผ์ ์๋๋ฐ Vue์ ์์๋ ์๋์ ๊ฐ์ต๋๋ค.
// mixin.js
export default {
method: {
greet() {
console.log("Hello World!");
}
}
}
import mixin from './mixin'
export default {
mixins: [mixin],
created() {
console.log(this.greet());
}
}
๊ทธ๋ผ ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ด๊ฑธ ์ด๋ป๊ฒ ํํํ ์ ์๋์ง ํ๋ฒ ๋ณผ๊น์?
๐ ๋ฏน์ค์ธ ํจํด
class Dog {
constructor(name) {
this.name = name;
}
}
const dogFuntionality = {
__proto__: animalFunctionality,
bark(){ console.log(`${this.name} Bark!`); },
swim(){ console.log(`${this.name} Swim!`); },
walk() { super.walk(); },
sleep() { super.sleep(); }
}
Object.assign(Dog.prototype, dogFuntionality);
const dog = new Dog('Eddie');
dog.bark(); // Eddie Bark!
dog.swim(); // Eddie Swim!
๐ ๋ฏน์ค์ธ ํจํด ์ฌํ
class Dog {
constructor(name) {
this.name = name;
}
}
const animalFunctionality = {
walk() { console.log(`${this.name} walking...`); },
sleep() { console.log(`${this.name} sleeping...`); },
};
const dogFuntionality = {
__proto__: animalFunctionality,
bark(){ console.log(`${this.name} Bark!`); },
swim(){ console.log(`${this.name} Swim!`); },
walk() { super.walk(); },
sleep() { super.sleep(); }
}
// Object.assign(dogFuntionality, animalFunctionality); -> ์์ __proto__๋ฅผ ์ ๊ฑฐ ํ๊ณ ์ด๋ ๊ฒ ์ฌ์ฉํด๋ ๋ฉ๋๋ค.
Object.assign(Dog.prototype, dogFuntionality);
const dog = new Dog('Eddie');
dog.bark(); // Eddie Bark!
dog.swim(); // Eddie Swim!
dog.walk(); // Eddie walking...
dog.sleep(); // Eddie sleeping...
๐ ๊ฒฐ๋ก
๋ฏน์ค์ธ ํจํด์ ํจ์๋ค์ ๊บผ๋ด์ด์ ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด ์ค๋๋ค. ์ด๊ฒ์ ์ ๋ง ์ข์ ์ผ์ด์ง์. ๊ฐ๋ฐ์์๊ฒ ์ฝ๋ ์ค๋ณต์ ํผํด์ผํ ์ผ์ด๊ธฐ๋ ํ์ง๋ง ๋ณต์ก์ฑ์ด ์ฌ๋ผ๊ฐ๋๋ค. ๊ทธ๋์ ๋ฆฌ์กํธ์์๋ ๋ฏน์ค์ธ ํจํด๋ณด๋ค HOC๋ฅผ ์ด์ฉํ๋ผ๊ณ ๊ถ์ฅํฉ๋๋ค.
๋ฐ์ํ
'Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Render Props Pattern (with React) (0) | 2022.01.21 |
---|---|
Higher Order Component Pattern (with React) (0) | 2022.01.21 |
Mediator Pattern (0) | 2020.10.23 |
Observer Pattern (0) | 2020.10.19 |
Module Pattern (0) | 2020.10.07 |