Javascript๋Š” ๋ณธ์งˆ์ ์œผ๋กœ ๊ฐ์ฒด ๊ธฐ๋ฐ˜ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

 

OOP(Object-Oriented Programming)์˜ ํ•ต์‹ฌ์€ ๋‹ค์Œ ๋„ค ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค

  1. ์ถ”์ƒํ™” (Abstraction)
  2. ์บก์Аํ™” (Encapsulation)
  3. ์ƒ์† (Inheritance)
  4. ๋‹คํ˜•์„ฑ (Polymorphism)

 


1๏ธโƒฃ ์ถ”์ƒํ™” (Abstraction)

๋ถˆํ•„์š”ํ•œ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ˆจ๊ธฐ๊ณ , ์ค‘์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋…ธ์ถœํ•˜๋Š” ๊ฒƒ

์˜ˆ์‹œ:

class CoffeeMachine {
  constructor(brand) {
    this.brand = brand;
  }

  makeEspresso() {
    this.#boilWater();
    console.log(`${this.brand} ๋จธ์‹ : ์—์Šคํ”„๋ ˆ์†Œ ์™„์„ฑ!`);
  }

  #boilWater() {
    console.log('๋ฌผ์„ ๋“์ด๋Š” ์ค‘...');
  }
}

const cm = new CoffeeMachine('Nespresso');
cm.makeEspresso(); // ์—์Šคํ”„๋ ˆ์†Œ ์™„์„ฑ!
  • ๋‚ด๋ถ€ ๋™์ž‘(boilWater())์€ ์ˆจ๊ฒจ์ง
  • ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„  ๋‹จ์ˆœํžˆ makeEspresso()๋งŒ ์•Œ๋ฉด ๋จ

2๏ธโƒฃ ์บก์Аํ™” (Encapsulation)

๊ฐ์ฒด ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์ˆจ๊ธฐ๊ณ , ํ•„์š”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ๊ณต๊ฐœํ•˜๋Š” ๊ฒƒ

โœ… ์˜ˆ์‹œ (Private ํ•„๋“œ):

class BankAccount {
  #balance = 0;

  deposit(amount) {
    if (amount > 0) this.#balance += amount;
  }

  getBalance() {
    return this.#balance;
  }
}

const myAccount = new BankAccount();
myAccount.deposit(1000);
console.log(myAccount.getBalance()); // 1000
// myAccount.#balance → โŒ ์—๋Ÿฌ
  • #balance๋Š” ์™ธ๋ถ€์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€ (์บก์Аํ™”)

3๏ธโƒฃ ์ƒ์† (Inheritance)

๊ธฐ์กด ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ

โœ… ์˜ˆ์‹œ:

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a sound`);
  }
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks`);
  }
}

const dog = new Dog('Buddy');
dog.speak(); // Buddy barks
  • Dog๋Š” Animal์˜ ์†์„ฑ ๋ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๊ณ , speak()๋Š” ์˜ค๋ฒ„๋ผ์ด๋”ฉ

4๏ธโƒฃ ๋‹คํ˜•์„ฑ (Polymorphism)

๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ช…์ด ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ

โœ… ์˜ˆ์‹œ (์˜ค๋ฒ„๋ผ์ด๋”ฉ):

const animals = [
  new Dog('Max'),
  new Animal('Generic')
];

animals.forEach(animal => animal.speak());

 

๊ฒฐ๊ณผ:

Max barks
Generic makes a sound
  • speak()๋Š” ๊ฐ ํด๋ž˜์Šค์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„๋จ
  • ํ•˜๋‚˜์˜ animal.speak() ํ˜ธ์ถœ์ด ์—ฌ๋Ÿฌ ํ˜•ํƒœ๋กœ ๋™์ž‘ → ๋‹คํ˜•์„ฑ

๐Ÿ”ท React ์ปดํฌ๋„ŒํŠธ์™€ OOP ๊ฐœ๋…

React๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์ด์ง€๋งŒ, OOP์˜ ์‚ฌ๊ณ ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๋Œ€๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

OOP ํŠน์„ฑ React์—์„œ์˜ ์˜ˆ

์ถ”์ƒํ™” ์ปดํฌ๋„ŒํŠธ๋กœ UI ๋‹จ์œ„ ์ถ”์ƒํ™” (<Button />)
์บก์Аํ™” props/state๋Š” ์™ธ๋ถ€์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€
์ƒ์† HOC, ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ extends React.Component
๋‹คํ˜•์„ฑ ๋™์ผํ•œ ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„์ด๋ผ๋„ props์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋ Œ๋”๋ง
const Button = ({ type }) => {
  return <button>{type === 'submit' ? '์ œ์ถœ' : '์ทจ์†Œ'}</button>;
};
  • ๊ฐ™์€ <Button /> ์ปดํฌ๋„ŒํŠธ๋„ props์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ → ๋‹คํ˜•์„ฑ ๊ฐœ๋… ์ ์šฉ

๊ฒฐ๋ก 

ํŠน์„ฑ ์„ค๋ช… JS ์˜ˆ์‹œ ํ‚ค์›Œ๋“œ

์ถ”์ƒํ™” ์ค‘์š”ํ•œ ๊ฒƒ๋งŒ ๋ณด์ด๊ฒŒ private ๋ฉ”์„œ๋“œ, interface ์—ญํ• 
์บก์Аํ™” ๋‚ด๋ถ€ ์ˆจ๊ธฐ๊ธฐ #ํ•„๋“œ, getter/setter
์ƒ์† ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ class A extends B
๋‹คํ˜•์„ฑ ๋™์ผ ์ธํ„ฐํŽ˜์ด์Šค, ๋‹ค๋ฅธ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

+ Recent posts