Object-Oriented Programming (OOP in Js)?

filoscoder·2019년 11월 19일
0
post-thumbnail

OOP(객채지향 프로그래밍)?

Object-Oriented Programming은 한글로 직역한다면 객채 지향 프로그래밍이다. 이것은 프로그램을 작성하는데 함수나 로직 기반 보다는 객체 기반으로 한 패러다임 또는 패턴이다. 여기서 객체속성동작을 가진 데이터 필드로 정의된다. 이는 속성 및 절차의 형태로 된 데이터를 방법 형태로 포함하고 있다. 객체 절차는 객체에 존재하는 데이터에 접근하고 수정할 수 있다.
OOP는 크고 복잡한 문제들을 관리하는 데 가장 적합하다.

OOP는 쉽게 말해, 모든 것을 물체로 정의하려는 프로그래밍 패러다임이다.

OOP Basic concept

객체 지향 프로그래밍에는 기본적인 개념으로 Encapsulation, Inheritance, Abstraction, Polymorphism 을 이야기 할 수 있다.

1. Encapsulation (캡슐화)

어떠한 물체를 객체를 이해할때, 이는 여러가지 속성과 행동으로 이루어져 있는데, 이것을 하나의 객체로 묶어서 표현하는 것을 캡슐화이다.

예를 들어, 동물(animal)이라는 물체 중 강아지(dog)절차 지향적으로 표현하기 위해 아래와 같이 정의 할 수 있을것이다.

// ANIMAL > 'dog'
let name = "dog"
let color = "brown";
let speak = "woof woof!"
function getAnimal(name, color, speak) {
 return "This is a "+color+" "+name+" and sounds like: "+speak; 
}
// 인자로 여러 변수를 지정하여 getAnimal 함수 호출
getAnimal(name, color, speak); 
// output> "This is a brown dog and sounds like: woof woof!"

객체지향의 캡슐화 개념은 동물의 속성과 행동을 하나의 객체로 이해합니다.

// ANIMAL > 'x'
const animal = {
  name : "dog",
  color : "brown",
  speak : "woof woof!",
  getAnimal: function() {
    return "This is a "+this.color+" "+this.name+" and sounds like: "+this.speak; 
  }
}
// animal 객체 안에 내장된 getAnimal key 값으로 된 익명 함수 호출
animal.getAnimal(); 
// output> "This is a brown dog and sounds like: woof woof!"

2. Inheritance (상속)

상속이라는 단어를 들으면 부모가 자식에게 물려주는 재산 같은 개념이 떠오를 수 있습니다. OOP에서 상속은 비슷한 개념이라고 이야기 할 수 있습니다. Parent 또는 Super라는 부모 Class가 있고 그 상속을 받을 Child 라는 자식 Class는 상속 받은 재산에 접근하여 사용할 수 있다. 여기서 재산이란 부모 Class가 가지고 있는 변수함수를 비유적으로 말한 것이다.

이해를 돕기 위해 예제를 보자.
Person이라는 부모 Class를 정의 하고 Programmer라는 자식 Class에서 상속을 받고 "재산"을 활용해보자.

  • 먼저 부모 클래스 Person을 정의하고 "재산"에 접근 해보자
// Parent/Super Class
class Person {
 // 재산1 > 생성자 함수 변수들
 constructor (name, age) {
   this.name = name;
   this.age = age;
 }
  // 재산2 > displayPerson 함수
 displayPerson() {
 	console.log("Hi! My name is", this.name,", I'm", this.age)
 }
}

let john = new Person("John Doe", 41); // Person 클래스 인스턴스
john.displayPerson();	// Person 클래스가 가지고 있는 함수

// output> "Hi! My name is John Doe, I'm 41"
  • 자식 클래스 Programmer를 정의하고 Person 클래스를 상속 받고 "부모재산"에 "나의 재산"을 더하여 활용해보자
// Child Class: Programer [inherit] Parent Class: Person
class Programmer extends Person{
 
 constructor (name, age, language, position) {
   super(name, age); // 부모재산1 > 생성자 함수 변수들
   this.language = language;
   this.position = position;
 }
  
 displayPerson() { // 부모재산2 > displayPerson 함수
 	console.log("Hi! My name is", this.name,", I'm", this.age)
    console.log("I'm also a", this.language, this.position, "developer");
 }
}

let daniel = new Programmer("Daniel Son", 32, "javascript", "front-end"); // Person 클래스 인스턴스
daniel.displayPerson();	// Person 클래스가 가지고 있는 함수

// output> "Hi! My name is Daniel Son, I'm  32"

3. Abstraction (추상)

abstract는 한글로 추상이라는 개념이다. 어려운 단어이며, 단어를 들었을때 어떤 것인지 유추하기 쉽지않다. 쉽게 말해서, 상속과 관련된 개념인데, 어떠한 클래스를 상속받을때 그 안에 있는 함수를 모두 구현해야 하는 개념이다.

4. Polymorphism (다형성)

polymorphism의 어원은 헬라어에서 많은, 형상에서 오는데, 한글로는 다형성이라는 어려운 말로 번역되어있다.
다형성이라는 개념 또한 상속 개념과 연관이 있는데, 상속받은 함수를 제사용하여 다른 모양으로 구현하는 것이다.
위에 있는 Programmer extends Person 예제에서 사용되었는데, displayPerson() 함수의 경우이다.

profile
Keep thinking code should be altruistic

0개의 댓글