Skip to content

Latest commit

ย 

History

History
206 lines (154 loc) ยท 5.99 KB

Object_Oriented_Programming.md

File metadata and controls

206 lines (154 loc) ยท 5.99 KB

Object-Oriented Programming

1. ๊ฐ์ฒด ์ง€ํ–ฅ์ด๋ž€

  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ ˆ์ฐจ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๊ณณ์— ๋ฌถ์–ด ์ฒ˜๋ฆฌํ•จ
  • ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ '๊ฐ์ฒด' ๋ผ๋Š” ๊ฐœ๋…์— ํฌํ•จ๋˜๋ฉฐ, ์ด๋Š” JavaScript ๋‚ด์žฅ ํƒ€์ž… object์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, class๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ถ€๋ฆ„
  • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ํ•˜๋‚˜์˜ ๋ชจ๋ธ์ด ๋˜๋Š” ์ฒญ์‚ฌ์ง„(blueprint)์„ ๋งŒ๋“ค๊ณ  โ†’ class
  • ์ด๋•Œ ๋ชจ๋ธ์˜ ์ฒญ์‚ฌ์ง„์„ ๋งŒ๋“ค ๋•Œ ์“ฐ๋Š” ์›ํ˜•๊ฐ์ฒด(original form) โ†’ prototype
  • ๊ทธ ์ฒญ์‚ฌ์ง„์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ฐ์ฒด(object)๋ฅผ ๋งŒ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด โ†’ instance

2. ํด๋ž˜์Šค ๋ชจ๋“ˆ ํŒจํ„ด

๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ

let counter1 = {
  value: 0;
  increase: function() {
    this.value++ // ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์„ ํ•  ๊ฒฝ์šฐ, this๋Š” counter1์„ ๊ฐ€๋ฆฌํ‚ด
  },
  decrease: function() {
    this.value--
  },
  getValue: function() {
    return this.value
  }
}

counter1.increase()
counter1.decrease()
counter1.increase()
counter1.decrease()
counter1.getValue() // 2

ํด๋กœ์ €๋ฅผ ์ด์šฉํ•ด ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ

function makeCounter() {
  let value = 0;
  return {
    increase: function () {
      value++;
    },
    decrease: function () {
      value--;
    },
    getValue: function () {
      return value;
    }
  }
}

let counter1 = makeCounter()
counter1.increase()
counter1.getValue() // 1

let counter2 = makeCounter()
counter2.decrease()
counter2.decrease()
counter2.getValue() // -2

3. ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค

ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฌธ๋ฒ•: class ํ‚ค์›Œ๋“œ

class Car {
constructor (brand, name, color) {
  // ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๋•Œ(= ์ดˆ๊ธฐํ™”๋  ๋•Œ) ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ: constructor(์ƒ์„ฑ์ž)
  // ์ฐธ๊ณ ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” return ๊ฐ’์„ ๋งŒ๋“ค์ง€ ์•Š์Œ
  }
}

ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋งŒ๋“ค๊ธฐ

let avante = new Car('hyundai', 'avante', 'black');
// ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” new ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
// ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋Š” Car ๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ณ ์œ ํ•œ ์†์„ฑ๊ณผ, ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง

this: ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด

  • parameter๋กœ ๋„˜์–ด์˜จ ๋ธŒ๋žœ๋“œ, ์ด๋ฆ„, ์ƒ‰์ƒ ๋“ฑ์€ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ ์ง€์ •ํ•˜๋Š” ๊ฐ’
  • this์— ํ• ๋‹นํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค์— ํ•ด๋‹น ํ•ด๋‹น ๋ธŒ๋žœ๋“œ, ์ด๋ฆ„, ์ƒ‰์ƒ์„ '๋ถ€์—ฌ'ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ
  • ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ ํ•ด๋‹น scope๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋Š” ๊ณ ์œ ํ•œ ์‹คํ–‰ context
  • new ํ‚ค์›Œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ–ˆ์„ ๋•Œ๋Š” ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”๋กœ this ๊ฐ’์ด ๋จ

๋ฉ”์†Œ๋“œ ์ •์˜

์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ class ํ‚ค์›Œ๋“œ ์•ˆ์ชฝ์— ๋ฌถ์–ด์„œ ์ •์˜

class Car {
  constructor(brand, name, color) {/* ์ƒ๋žต */}
  refuel() {
  }
  drive() {
  }
}

์ธ์Šคํ„ด์Šค์—์„œ์˜ ์‚ฌ์šฉ: ์œ„์—์„œ ์„ค์ •ํ•œ ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ์ธ์Šคํ„ด์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

let avante = new Car('hyundai', 'avnante', 'black');
avante.color; // 'black'
avante.drive(); // ์•„๋ฐ˜๋–ผ๊ฐ€ ์šด์ „์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค

let mini = new Car('bmw', 'mini', 'white');
mini.brand; // 'bmw'
mini.refuel(); // ๋ฏธ๋‹ˆ์— ์—ฐ๋ฃŒ๋ฅผ ๊ณต๊ธ‰ํ•ฉ๋‹ˆ๋‹ค

์ •๋ฆฌ

// constructor(์ƒ์„ฑ์ž) ํ•จ์ˆ˜
function Car (brand, name, color) { // Car๋Š” class
  this.brand = brand; // ์—ฌ๊ธฐ์„œ avante === this
  this.name = name;
  this.color = color;
}

// Car.prototype ๊ฐ์ฒด: ์—ฌ๊ธฐ์— ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ
Car.prototype.drive = function() {
  console.log(this.name + '๊ฐ€ ์šด์ „์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค');
}

let avante = new Car('hyundai', 'avante', 'black'); // avante: instance
avante.color; // 'black'
avante.drive(); // 'avante๊ฐ€ ์šด์ „์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค'

4. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

์ ˆ์ฐจ์  ์–ธ์–ด > ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด

  • ์ ˆ์ฐจ์  ์–ธ์–ด: ์ˆœ์ฐจ์ ์ธ ๋ช…๋ น์˜ ์กฐํ•ฉ
  • ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด: class ๋ผ๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์˜ ์ฒญ์‚ฌ์ง„์„ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์ด ํ•œ๋ฒˆ์— ๋ฌถ์—ฌ์„œ ์ฒ˜๋ฆฌ๋จ.
  • JavaScript๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๊ฐ์ฒด ์ง€ํ–ฅ ํŒจํ„ด์œผ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ

OOP์˜ ๋„ค ๊ฐ€์ง€ ๊ธฐ๋ณธ ๊ฐœ๋…

(1) Encapsulation(์บก์Šํ™”)

  • ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์„ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ๋Š” ๊ฒƒ
  • ์€๋‹‰(Hiding): ๊ตฌํ˜„์€ ์ˆจ๊ธฐ๊ณ , ๋™์ž‘์€ ๋…ธ์ถœ
  • ๋Š์Šจํ•œ ๊ฒฐํ•ฉ(Loose Coupling)์— ์œ ๋ฆฌ: ์ฝ”๋“œ ์‹คํ–‰ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ ˆ์ฐจ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ฝ”๋“œ๊ฐ€ ์ƒ์ง•ํ•˜๋Š” ์‹ค์ œ ๋ชจ์Šต๊ณผ ๋‹ฎ๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„ ๊ฒฐํ•ฉ, ์–ธ์ œ๋“  ๊ตฌํ˜„ ์ˆ˜์ • ๊ฐ€๋Šฅ
  • ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜์ง€ ์•Š๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ž„

(2) Inheritance(์ƒ์†)

  • ๋ถ€๋ชจ, ์ฆ‰ ๊ธฐ๋ณธ ํด๋ž˜์Šค(base class)์˜ ํŠน์ง•์„ ์ž์‹, ์ฆ‰ ํŒŒ์ƒ ํด๋ž˜์Šค(derived class)๊ฐ€ ์ƒ์†๋ฐ›๋Š” ๊ฒƒ
  • ์ฝ”๋“œ๊ฐ€ ๋‹จ์ˆœํ•˜๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ž„

(3) Abstraction(์ถ”์ƒํ™”)

  • ๋‚ด๋ถ€ ๊ตฌํ˜„์€ ๋ณต์žก but ์‹ค์ œ ๋…ธ์ถœ๋˜๋Š” ๋ถ€๋ถ„์€ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ค์–ด, ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์‚ฌ์šฉ์ƒ์˜ ๋ณ€ํ™” ๋ฐฉ์ง€
  • ์บก์Šํ™”๋Š” ๋ฐ์ดํ„ฐ์˜ ์€๋‹‰์— ์ค‘์‹ฌ์„ ๋งž์ถ”๊ณ , ์ถ”์ƒํ™”๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋ถˆํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ , ๋‹จ์ˆœํ•œ ์ด๋ฆ„์œผ๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์— ์ค‘์‹ฌ.
  • ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜์ง€ ์•Š๊ณ  ๋ณ€ํ™”์— ๋Œ€ํ•œ ์˜ํ–ฅ ์ตœ์†Œํ™”

(4) Polymorphism(๋‹คํ˜•์„ฑ)

  • ex) ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ฉ”์†Œ๋“œ๋ผ๋„ ๊ฐ ์š”์†Œ์— ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋จ
  • ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด if/else์™€ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ ๋Œ€์‹ , ๊ฐ ๊ฐ์ฒด์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ๋‹ค๋ฅด๊ฒŒ ์ž‘์„ฑ ๊ฐ€๋Šฅ

5. ํ”„๋กœํ† ํƒ€์ž…

ํ”„๋กœํ† ํƒ€์ž…๊ณผ ํด๋ž˜์Šค

JavaScript๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ์–ธ์–ด๋กœ, ํ”„๋กœ๋กœํƒ€์ž…์€ ์›ํ˜• ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ

class Human {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sleep() {
    console.log(`${this.name}์€ ์ž ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค`);
  }
}

let kimcoding = new Human('๊น€์ฝ”๋”ฉ', 30);

Human.prototype.constructor === Human; // true
Human.prototype === kimcoding.__proto__; // true
Human.prototype.sleep === kimcoding.sleep; // true

ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํŠน์„ฑ ์ค‘, ์ƒ์†์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ ์‚ฌ์šฉ