티스토리 뷰

ooad

Implementation Strategies

dictee 2019. 5. 25. 12:23

※ tutorialspoint의 OOAD 요약 (https://www.tutorialspoints.com/object_oriented_analysis_design/)

 

1. Implementation using Programming Languages

C++, Java, Smalltalk, C#, Python 등의 object oriented programming language 존재. C++ 기준으로 설명.

아래 그림은 Circle class에 대한 C++ 표현.

2. Implementing Associations

대부분의 프로그래밍 언어는 직접적으로 associatoin을 구현하는 구조를 제공하지 않는다. association 구현은 상당한 고민이 필요하다.

association은 unidirectional 또는 bidirectional이다. 추가로, 각 association은 one-to-one, one-to-many, 또는 many-to-many이다.

Unidirectional Associations

unidirection이 유지되도록 구현하는 것이 필요하다. 여러 다양성의 구현은 다음과 같다,

  • Optional Associations: link는 object 사이에 있을 수도, 없을 수도 있다. 예를 들어, Customer와 Current Account 사이에서는, cusomter는 current account를 보유할 수도, 보유하지 않을 수도 있다.

구현 관점에서, Current Account object는 Customer에서 NULL일 수도 있는 attribute로 포함된다. C++ 코드는,

class Customer {
   private:
   // attributes
   Current_Account c; //an object of Current_Account as attribute
   
   public:  

   Customer() {
      c = NULL; 
   } // assign c as NULL

   Current_Account getCurrAc() {
      return c;
   }
   
   void setCurrAc( Current_Account myacc) {
      c = myacc;
   }

   void removeAcc() {  
      c = NULL;
   } 
};

 

  • One–to–one Associations: class instance는 정확하게 associated class의 instance와 관계가 있다. 예를 들어, Department와 Manager는 one-to-one association을 갖는다.

Department는 Manager object를 갖게 되지만, NULL이면 안되도록 구현한다. C++ 코드는,

class Department {
   private:
   // attributes
   Manager mgr; //an object of Manager as attribute
   
   public:  
   Department (/*parameters*/, Manager m) { //m is not NULL   
      // assign parameters to variables
      mgr = m;
   } 

   Manager getMgr() {  
      return mgr;  
   }    
};

 

  • One–to–many Associations − class instance는 associated class들의 여러 instance와 관계가 있다. 예를 들어, Employee와 Department의 관계.

C++ STL list를 이용한 코드,

class Employee {
   private:
   char * deptName;
   list <Dependent> dep; //a list of Dependents as attribute

   public:  
   void addDependent ( Dependent d) { 
      dep.push_back(d); 
   } // adds an employee to the department

   void removeDeoendent( Dependent d) { 
      int index = find ( d, dep );
      // find() function returns the index of d in list dep
      dep.erase(index);
   }               
};

 

Bi-directional Associations

양방향 유지를 함께 고민해야 한다.

  • Optional or one–to–one Associations: Project와 Project Manager의 관계.

Class Project {
   private:
   // attributes
   Project_Manager pmgr; 
   public:  
   void setManager ( Project_Manager pm);       
   Project_Manager changeManager();   
};

class Project_Manager {
   private:
   // attributes
   Project pj; 

   public:  
   void setProject(Project p);       
   Project removeProject();   
};

 

  • One–to–many Associations − Department와 Employee의 관계

C++ STL list를 이용한 코드,

class Department {
   private:
   char * deptName;
   list <Employee> emp; //a list of Employees as attribute

   public:  
   void addEmployee ( Employee e) { 
      emp.push_back(e); 
   } // adds an employee to the department

   void removeEmployee( Employee e) { 
      int index = find ( e, emp );
      // find function returns the index of e in list emp
      emp.erase(index);
   }               
};

class Employee {
   private:
   //attributes
   Department d;

   public:
   void addDept();
   void removeDept();
};

 

Implementing Associations as Classes

association과 관련한 attribute가 있는 경우, 별도의 class로 구현해야 한다. Employee와 Project의 one-to-one association은 다음과 같다.

WorksOn 구현

class WorksOn {
   private:
   Employee e; 
   Project p;
   Hours h;
   char * date;

   public:
   // class methods
};

 

3. Implementing Constraints

class의 constraint이란, attribute가 가질 수 있는 value의 범위와 type을 제한하는 것이다. 이것의 구현은, class의 object 초기화 시, 적절한 default value를 attribute에 할당하는 것으로부터 출발한다. runtime 때 value 변경 시, value의 유효성을 확인한다. 만약 invalid value인 경우는 exception handling 또는 별도의 method로 처리해야 한다.

 

Example

18-60의 value를 갖는 Employee class. 

class Employee {
   private: char * name;
   int age;
   // other attributes

   public:
   Employee() {                   // default constructor 
      strcpy(name, "");
      age = 18;                // default value
   }
 
   class AgeError {};          // Exception class
   void changeAge( int a) {   // method that changes age 
      if ( a < 18 || a > 60 )  // check for invalid condition
      throw AgeError();        // throw exception
      age = a;			
   }
};

 

4. Implementing State Charts

Enumerations within Class

state는 data member의 서로 다른 value로 표현. value는 class 내에서 enumeration을 통해 명시적으로 정의된다. transition은 data member의 value를 변경하는 member function으로 표현한다.

Arrangement of Classes in a Generalization Hierarchy

state는 common pointer variable에 의해 참조될 수 있는 일반화 hiterarchy로 배열된다. 다름 그림은, state chart diagram을 generalization hierarchy로의 전환을 보여준다.

 

5. Object Mapping to Database System

Persistency of Objects

data persistency. 이것을 통해 object는 object는 그것을 생성하는 프로그램보다 수명이 더 길다. persistent data는 필요 시 로딩할 수 있는, secondary storage에 저장된다.

Overview of RDBMS

관련 데이터의 정려된 collection을 DB라고 한다.

DBMS는 DB내의 data를 정의, 생성, 저장, 조작, 검색, 공유, 삭제하는 SW.

RDBMS에서 data는 relation 또는 table로서 저장되며, 각 column 또는 필드는 attribute를 의미하고, 각 row 또는 tuple은 instace record를 의미한다.

각 row는 primary key라고 불리는 minimal attribute 집합에 의해 구분된다.

foreign keyprimary key 중 다른 table 참조를 위한 key를 의미한다.

Representing Classes as Tables in RDBMS

class를 DB table로 매핑하는 것은, 각 attribute가 table 내의 field로 표현되는 것이다. 기존 attribute가 primary key 로 할당되거나  별도의 ID field가 primary key로서 추가된다. class는 요구사항에 따라 수평 또는 수직 방향으로 분할된다.

예를 들어, Circle class를 아래 그림과 같이 표현할 수 있다.

Schema for Circle Table: CIRCLE(CID, X_COORD, Y_COORD, RADIUS, COLOR)
Creating a Table Circle using SQL command:
CREATE TABLE CIRCLE (   
   CID	VARCHAR2(4) PRIMARY KEY,
   X_COORD INTEGER NOT NULL,
   Y_COORD INTEGER NOT NULL,
   Z_COORD INTEGER NOT NULL,
   COLOR 
);

 

6. Mapping Associations to Database Tables

One–to–One Associations

한 table의 primary key를 다른 table의 foreign key로 할당. 예를 들어, Department와 Manager의 association,

SQL commands to create the tables

CREATE TABLE DEPARTMENT ( 
   DEPT_ID INTEGER PRIMARY KEY,
   DNAME VARCHAR2(30) NOT NULL,
   LOCATION VARCHAR2(20),
   EMPID INTEGER REFERENCES MANAGER 
);

CREATE TABLE MANAGER ( 
   EMPID INTEGER PRIMARY KEY,
   ENAME VARCHAR2(50) NOT NULL,
   ADDRESS VARCHAR2(70),
);

 

One–to–Many Associations

1:N의 경우는 association의 1쪽 table의 primary key가 N쪽 table의 foreign key로 할당. Department와 Employee를,

SQL commands to create the tables

CREATE TABLE DEPARTMENT ( 
   DEPT_ID INTEGER PRIMARY KEY,
   DNAME VARCHAR2(30) NOT NULL,
   LOCATION VARCHAR2(20),
);

CREATE TABLE EMPLOYEE ( 
   EMPID INTEGER PRIMARY KEY,
   ENAME VARCHAR2(50) NOT NULL,
   ADDRESS VARCHAR2(70),
   D_ID INTEGER REFERENCES DEPARTMENT
);

 

Many–to–Many Associations

M:N association에서는 assocication 표현을 위한 새로운 relation을 생성. 아래와 같은, Employee와 Employee간의 association, 

Schema for Works_On Table − WORKS_ON (EMPID, PID, HOURS, START_DATE)

SQL command to create Works_On association − CREATE TABLE WORKS_ON

( 
   EMPID INTEGER,
   PID INTEGER, 
   HOURS INTEGER,
   START_DATE DATE,
   PRIMARY KEY (EMPID, PID),
   FOREIGN KEY (EMPID) REFERENCES EMPLOYEE,
   FOREIGN KEY (PID) REFERENCES PROJECT 
);

 

7. Mapping Inheritance to Tables

base table의 primary key는 deribed table의 primary key 뿐만 아니라 foreign key로 할당.

 

Example



댓글