Presentation is loading. Please wait.

Presentation is loading. Please wait.

9 장 추상 데이터 타입 및 모듈 (Abstract Data Type & Module).

Similar presentations


Presentation on theme: "9 장 추상 데이터 타입 및 모듈 (Abstract Data Type & Module)."— Presentation transcript:

1 9 장 추상 데이터 타입 및 모듈 (Abstract Data Type & Module)

2 9.1 캡슐화 및 추상 데이터 타입 (Encapsulation and Abstract Data Types)

3 추상화 (Abstraction) The concept of abstraction is fundamental in programming Control abstraction control structures Procedures Data abstraction data types

4 프로시져 사용 예제 typedef struct { float re,im; } COMPLEX; COMPLEX add( COMPLEX x, y) {... } COMPLEX subtract( COMPLEX x, y) {... } COMPLEX multiply( COMPLEX x, y) {... } COMPLEX divide( COMPLEX x, y) {... } COMPLEX a, b, c; c = add(a,b);

5 프로시져 사용의 문제점 Any procedures(operations) can use COMPLEX data type Direct access of fields of COMPLEX variables ? No separation of interface and implementation

6 캡슐화 (Encapsulation) Original motivation Large programs have two special needs: Some means of organization, other than simply division into subprograms Separate compilation units Obvious solution Unit abstraction or encapsulations a grouping of logically related subprograms with data types

7 추상 데이터 타입 Basic idea Collections of data(variables) and operations(procedures) Originated from Simula-67 Encapsulation define a data type with operations data + operations restrict the use of the type to the defined operations

8 추상 데이터 타입 연산 ( 메쏘드 ) 데이터 클라이언트

9 추상 데이터 타입의 장점 수정 용이성 구현에 독립적인 인터페이스 재사용가능성 표준 인터페이스로 다른 프로그램에서도 재사용 보안성 구현의 세부사항을 프로그램 다른 부분에서 접근 금지

10 추상 데이터 타입의 예 Module in Modula-2 Package in Ada Class in Java, and C++

11 추상 데이터 타입의 수학적 의미 (S, Op) where S is a set of data Op is a set of operations.

12 프로그래밍 단위 Procedural languages Procedures Languages with ADT Modules in Modula-2, Package in Ada Object-oriented languages Class

13 모듈을 이용한 프로그래밍 var s, t : stack; begin s := newstack; t := newstack; push(10,s);... end. type stack; function pop(s:stack) : integer; procedure push(a: integer; s:stack); function newstack : stack; representation of stacks (type stack)... initialization (newstack)... procedure bodies (pop, push) Module StackManager

14 정보 은닉 (Information Hiding) 구현의 세부 사항을 정의에서 분리 구현의 세부 사항은 프로그램 다른 부분에서 접근 금 지

15 모듈 (Module) Role A collection of data(variables) and operations(procedures) Interface(definition) part public part can be accessed outside of the package since it is public Implementation part private part an implementation detail cann't be accessed outside of the ADT

16 클래스를 타입 이름으로 In C++ Stack s, t; s.push(7); s.push(8); In Java Stack s, t; s = new Stack( ); t = new Stack( ); s.push(7); class Stack { public: Stack(); void push( int a); int pop(); private representation of stacks... initialization (Stack)... procedure bodies (pop, push)

17 비교 Procedures data type and operation are defined separately implementation details are accessible throughout program ADT (Modules, Packages) define data type with its operations interface and implementation implementation is hidden accss is checked

18 비교 Class class is a defined type as data and operations interface and implementation implementation is hidden accss is checked object an instance of a class inheritance

19 9.2 프로그램 설계

20 프로그램 설계 Program design discover the right object, right module Decide which types you want; Provide a full set of operations for each type Class and modules Formulate the structure of a program

21 모듈 var s, t : stack; begin s := newstack; t := newstack; push(10,s);... end. type stack; function pop(s:stack) : integer; procedure push(a: integer; s:stack); function newstack : stack; type stack = ^ rep rep = record elements: array[0..100] of integer; top : integer; end; (type stack) function newstack : stack; var s: stack; begin new(s); s^.top :=0; newstack :=s end; procedure bodies (pop, push) Module StackManager

22 Ada 의 패키지 Ada 에서 제공하는 ADT Package Package 구성 Package specification the interface Package Body implementation of the entities named in the specification

23 Ada 의 패키지 예제 package COMPLEX_TYPE is type COMPLEX is private; I: constant COMPLEX; function + (X, Y: COMPLEX) return COMPLEX; function - (X, Y: COMPLEX) return COMPLEX; function * (X, Y: COMPLEX) return COMPLEX; function / (X, Y: COMPLEX) return COMPLEX; function +(X:float; Y:COMPLEX) return COMPLEX; function - (X: COMPLEX) return COMPLEX;, function RE (X: COMPLEX) return float; function IM (X: COMPLEX) return float; private type COMPLEX is record re, im : float := 0.0; end record I : contant COMPLEX := (0.0, 1.0); end COMPLEX_TYPE;

24 Ada 의 패키지 예제 package body COMPLEX_TYPE is function +(X,Y:COMPLEX) return COMPLEX is begin return(X.RE+Y.RE, X.IM+Y.IM); end; function RE(X:COMPLEX) return float is begin return X.RE; end; function +(X:float; Y:COMPLEX) return COMPLEX is begin return(X+Y.RE, Y.IM); end;... others... end COMPLEX_TYPE;

25 Ada 의 패키지 사용 declare use COMPLEX_TYPE;X,Y: COMPLEX; Z: COMPLEX := 1.5 + 2.5*I; begin X := 2.5 + 3.5*I;Y := X + Z; Z := RE(Z) + IM(X)*I; if X = Y then X:= Y+Z; else X:= Y*Z; end if; end

26 9.3 매개변수를 갖는 추상 데이 터 타입

27 매개변수를 갖는 추상 데이터 타입 Procedure 혹은 function 이 적용될 수 있는 데이터 타입의 확장 타입을 parameter 로 받는 메커니즘 이용 Ada: Generic C++: Template

28 예제 template class Stack { T* v; T* p; int sz; public: Stack(int s) { v=p=new T[sz=s];} ~Stack( ) { delete[] v;} void push(T a) { p++ = a;} T pop( ) { return *--p;} Int size( ) { return p-v;} } Stack sc(100); Stack si(100); Stack sp(100);


Download ppt "9 장 추상 데이터 타입 및 모듈 (Abstract Data Type & Module)."

Similar presentations


Ads by Google