Upload
sheila-hodge
View
246
Download
0
Embed Size (px)
DESCRIPTION
추상화 (Abstraction) The concept of abstraction is fundamental in programming Control abstraction control structures Procedures Data abstraction data types
Citation preview
9 장 추상 데이터 타입 및 모듈(Abstract Data Type & Module)
9.1 캡슐화 및 추상 데이터 타입(Encapsulation and Abstract Data Types)
추상화 (Abstraction) The concept of abstraction is fundamental in
programming
Control abstraction control structures Procedures
Data abstraction data types
프로시져 사용 예제 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);
프로시져 사용의 문제점 Any procedures(operations) can use COMPLEX data type
Direct access of fields of COMPLEX variables ?
No separation of interface and implementation
캡슐화 (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
추상 데이터 타입 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
추상 데이터 타입 연산 (메쏘드 )
데이터
클라이언트
추상 데이터 타입의 장점 수정 용이성
구현에 독립적인 인터페이스
재사용가능성 표준 인터페이스로 다른 프로그램에서도 재사용
보안성 구현의 세부사항을 프로그램 다른 부분에서 접근 금지
추상 데이터 타입의 예 Module in Modula-2
Package in Ada
Class in Java, and C++
추상 데이터 타입의 수학적 의미 (S, Op) where
S is a set of data Op is a set of operations.
프로그래밍 단위 Procedural languages
Procedures
Languages with ADT Modules in Modula-2, Package in Ada
Object-oriented languages Class
모듈을 이용한 프로그래밍 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
정보 은닉 (Information Hiding) 구현의 세부 사항을 정의에서 분리 구현의 세부 사항은 프로그램 다른 부분에서 접근 금지
모듈 (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
클래스를 타입 이름으로 In C++Stack s, t;s.push(7);s.push(8);
In JavaStack 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)
비교 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
비교 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
9.2 프로그램 설계
프로그램 설계 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
모듈 var s, t : stack;begin
s := newstack;t := newstack;push(10,s);push(10,s);. . .
end.
type stack; function pop(s:stack) : integer; procedure push(a: integer; s:stack); function newstack : stack; type stack = ^ rep
rep = recordelements: array[0..100] of integer;top : integer;
end; (type stack)
function newstack : stack; var s: stack;begin new(s); s^.top :=0; newstack :=send; procedure bodies (pop, push)
Module StackManager
Ada 의 패키지 Ada 에서 제공하는 ADT
Package
Package 구성 Package specification
the interface Package Body
implementation of the entities named in the specification
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;
privatetype COMPLEX is record re, im : float := 0.0; end recordI : contant COMPLEX := (0.0, 1.0);
end COMPLEX_TYPE;
Ada 의 패키지 예제package body COMPLEX_TYPE is
function +(X,Y:COMPLEX) return COMPLEX isbegin
return(X.RE+Y.RE, X.IM+Y.IM);end;function RE(X:COMPLEX) return float isbegin return X.RE; end;function +(X:float; Y:COMPLEX) return COMPLEX isbeginreturn(X+Y.RE, Y.IM);end; ... others ...
end COMPLEX_TYPE;
Ada 의 패키지 사용 declare
use COMPLEX_TYPE; X,Y: COMPLEX;Z: COMPLEX := 1.5 + 2.5*I;
beginX := 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
9.3 매개변수를 갖는 추상 데이터 타입
매개변수를 갖는 추상 데이터 타입 Procedure 혹은 function 이 적용될 수 있는 데이터
타입의 확장
타입을 parameter 로 받는 메커니즘 이용 Ada: Generic C++: Template
예제template <class T> 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<char> sc(100);Stack<int> si(100);Stack<point> sp(100);