약 3분

ADT and Polymorphism

ADT and Polymorphism
Photo by Merve Sehirli Nasir / Unsplash
추상화: Hiding the details. 관심 있는 것에 집중!

Abstraction

  • Making it Simpler

Encapsulation

  • Making them into one Unit

Information Hiding

  • Hiding the details
Encapsulation is a language facility,
whereas information hiding is a design principle.

Encapsulation refers to the bundling of data with the methods
that operate on that data.

The primary criteria for system modularization should concern
the hiding of critical design decisions.

ADT

: 추상 자료형(Abstract Data Types)

  • Type: Set of Values + Set of Operations
  • ADT도 타입이다! 즉, Value Set + Operation Set
    • 하지만 Operation의 구현이 드러나지 않으며,
    • Value의 구현(저장 형태 등)도 드러나지 않는다 (abstract)
  • 중요! 데이터 타입을 캡슐화:
    정의된 Operation Set을 제외한 다른 방법으로는 객체를 다룰 수 없도록!!!!!
  • ADT 예시:
    • SetName: name * student -> student
    • GetName: student -> name
    • SetCourse: course * student -> student
    • GetCourse: student -> course
    • GetCredits: student -> integer
    • ...
    • 여기서 name, course는 정의되지 않은 또 다른 ADT다!

Information Hiding

  • 인터페이스와 무관한 부분은 숨기는 설계 기법
  • 위 ADT의 예시에서, student 객체가 어떻게 저장되어야 하는지는 알 수도 없으며, 알 필요도 없다
    • 중요한 것은 연산(Operation)들의 동작 형태(Behavior)다!!
  • 정보 은닉은 어떤 언어에서든 구현 가능하다
    • C언어 예시:
typedef struct {
  char *name,
  int salary,
  ...
} Employee;

char *getName(Employee e);
void promotion(Employee e);
...

Algebraic Data Type?

  • 해당 데이터타입에 적용 가능한 연산자들 사이에, 특정 대수적 법칙이 만족됨!
  • POP(newstack) = newstack
  • TOP(newstack) = undefined
  • POP( PUSH(S, I) ) = S
    • 원래 상태가 그대로 유지됨 (push 직후 pop)
  • TOP( PUSH(S, I) ) = I
    • push 직후 top의 결과는 반드시 방금 push한 element
  • ...

Polymorphism

Ad-hoc Polymorphism

: 실제로는 같은 코드지만, 다른 이름으로 사용

  • Overloading

Parametric Polymorphism

: 여러 타입에 동일하게 적용될 코드를 타입 인수를 통해 처리

  • Generic Programming

Subtype Polymorphism

: Is - A 관계에서, Supertype에 적용 가능한 코드는 Subtype에도 적용 가능하도록

  • Inheritance
    • Is - A
    • Has - A

Generic Data Type

  • Generic Type?
    • "타입"을 인수로 받는 "타입"
    • 뒤쪽의 타입은, 앞쪽의 타입을 통해 생성된 타입
    • 타입 인수(argument, parameter)를 통해 Parametric Polymorphism을 형성
  • Instantiation of Generic Type
    • Generic Type에 타입 인수를 전달함으로써 새로운 타입을 만드는 것
  • 구현?
    • 타입 인수(파라미터)는 컴파일 타임에 결정된다
    • 따라서, 컴파일 타임에 일반 타입과 동일하게 구현할 수 있다
      • ex: Instantiation of a class template

Inheritance

  • 일반적인 의미의 Inheritance?
    • 한 프로그램 요소의 특징이 다른 프로그램 요소로 전달되는 것
    • nested block에서, 감싸고 있는 블럭의 변수를 내부 블럭에서 볼 수 있는것도 inheritance의 일종이라고 보는 시각도 있다
  • ADT 관점의 Inheritance
    • 한 ADT를 보다 구체화하여 새로운 ADT를 만드는 것
    • Superclass - Subclass 관계를 형성함 (Subtype 관계)
    • Subclass는 Superclass의 모든 속성을 상속받음!!
  • 구현 예시
    • Ada의 tagged class, C++의 derived class, Java의 subclass

Inheritance Implementation Issues

  • 다중 상속(Multiple Inheritance)
    • 여러 Superclass를 가질 수 있나?
    • 다이아몬드 상속(Diamond Inheritance) 문제
      • 다이아몬드 상속 구조에서는, 동일한 Base Class를 여럿 가지게 되는 문제가 발생할 수 있다
      • -> virtual class를 사용(Pure Interface)
  • 런타임 메서드 바인딩 (Dynamic Invocation of Methods)
    • 메서드는 객체에 따라 호출되어야 한다!
    • 메시지를 수신하는 실제 객체의 종류에 따라 메서드가 달라져야 함
    • -> C++의 경우, virtual function을 활용 (vtable, vptr)