///
Search
🎲

9주차 (중요)시퀀스 다이어그램(3/3)

마지막으로 학교 E-class나 포탈에 적용할 수 있는 수강 과목 등록 프로그램을 만들어 봅시다.
연관 관계와 의존관계를 모두 사용하는 복잡한 시퀀스 다이어그램 입니다.
Student 객체는 문자열 객체 즉 학생의 이름과 Schedule 객체를 포함합니다.
Course 객체는 문자열 객체 즉 과목 이름과 Roster 객체 즉 명단을 포함합니다.
Schedule 객체와 Roster 객체는 문자열 객체를 포함합니다.
최종적으로는 Register(등록)객체에서는 Student객체와 Course 객체를 사용합니다.

대학교 수강 과목 프로그램 만들기

소스코드

/courseRoster.h

// // courseRoster.h // // // Created by Glory on 2021/04/28. // //CourseRoster 클래스의 인터페이스 파일 #ifndef COURSEROSTER_H #define COURSEROSTER_H #include <string> #include <iostream> #include <cassert> using namespace std; // 클래스 정의 class CourseRoster { private: int size; string* stdNames; public: CourseRoster(); ~CourseRoster(); void addStudent(string studentName); void print() const; }; #endif
C++
복사
위의 코드는 CourseRoster 클래스의 인터페이스 파일이며 밑의 코드는 구현 파일

/courseRoster.cpp

// // courseRoster.cpp // // // Created by Glory on 2021/04/28. // //CourseRoster 클래스의 구현 파일 #include "courseRoster.h" // 생성자 CourseRoster::CourseRoster() :size(0) { stdNames = new string[20]; } // 소멸자 CourseRoster::~CourseRoster() { delete[] stdNames; } // addStudent 함수의 정의 void CourseRoster::addStudent(string studentName) { stdNames[size] = studentName; size++; } // print 함수의 정의 void CourseRoster::print() const { cout << "수강하는 학생 목록" << endl; for(int i = 0; i < size; i++) { cout << stdNames[i] << endl; } cout << endl; }
C++
복사

/course.h

// // course.h // // // Created by Glory on 2021/04/28. // //Course 클래스의 인터페이스 파일 #ifndef COURSE_H #define COURSE_H #include <cassert> #include <string> #include <iostream> #include "courseRoster.h" using namespace std; // 클래스 정의 class Course { private: string name; int units; CourseRoster* roster; public: Course(string name, int units); ~Course(); string getName() const; CourseRoster* getRoster() const; void addStudent(string name); void print() const; }; #endif
C++
복사
위의 코드는 Course 클래스의 인터페이스 파일이며 밑에는 구현 파일

/course.c

// // course.cpp // // // Created by Glory on 2021/04/28. // //Course 클래스의 구현 파일 #include "course.h" // 생성자 Course::Course(string nm, int ut) : name(nm), units(ut) { roster = new CourseRoster; } // 소멸자 Course::~Course() { } // getName 함수의 정의 string Course::getName() const { return name; } // addStudent 함수의 정의 void Course::addStudent(string name) { roster->addStudent(name); } // getRoster 함수의 정의 CourseRoster* Course::getRoster() const { return roster; } // print 함수의 정의 void Course::print() const { cout << "코스 이름: " << name << endl; cout << "과목의 학점: " << units << endl; roster->print(); }
C++
복사

/studentSchedule.h

// // studentSchedule.h // // // Created by Glory on 2021/04/28. // // StudentSchedule 클래스의 인터페이스 파일 #ifndef STUDENTSCHEDULE_H #define STUDENTSCHEDULE_H #include <string> #include <iostream> #include <cassert> using namespace std; // 클래스 정의 class StudentSchedule { private: int size; string* courseNames; public: StudentSchedule(); ~StudentSchedule(); void addCourse(string course); void print() const; }; #endif
C++
복사

/studentSchedule.cpp

// // studentSchedule.cpp // // // Created by Glory on 2021/04/28. // //StudentSchedule 클래스의 구현 파일 #include "studentSchedule.h" // 생성자 StudentSchedule::StudentSchedule() :size(0) { courseNames = new string[5]; } // 소멸자 StudentSchedule::~StudentSchedule() { delete[] courseNames; } // addCourse 함수의 정의 void StudentSchedule::addCourse(string name) { courseNames[size] = name; size++; } // print 함수의 정의 void StudentSchedule::print() const { cout << "수강 과목 목록" << endl; for(int i = 0; i < size; i++) { cout << courseNames[i] << endl; } cout << endl; }
C++
복사

/student.h

// // student.hpp // // // Created by Glory on 2021/04/28. // //Student 클래스의 인터페이스 파일 #ifndef STUDENT_H #define STUDENT_H #include <cassert> #include <string> #include <iostream> #include "studentSchedule.h" using namespace std; // 클래스 정의 class Student { private: string name; StudentSchedule* schedule; public: Student(string name); ~Student(); string getName() const; StudentSchedule* getSchedule() const; void addCourse(string name); void print() const; }; #endif
C++
복사

/student.cpp

// // student.cpp // // // Created by Glory on 2021/04/28. // //Student 클래스의 구현 파일 #include "student.h" // 생성자 Student::Student(string nm) :name(nm) { schedule = new StudentSchedule; } // 소멸자 Student::~Student() { } // getName 함수의 정의 string Student::getName() const { return name; } // getSchedule 함수의 정의 StudentSchedule* Student::getSchedule() const { return schedule; } // addCourse 함수의 정의 void Student::addCourse(string name) { schedule->addCourse(name); } // print 함수의 정의 void Student::print() const { cout << "학생 이름: " << name << endl; schedule->print(); }
C++
복사

/registrar.h

// // registrar.hpp // // // Created by Glory on 2021/04/28. //Registrar 클래스의 인터페이스 파일 #ifndef REGISTRAR_H #define REGISTRAR_H #include "course.h" #include "student.h" // 클래스 정의 class Registrar { public: Registrar(); ~Registrar(); void enroll(Student student, Course course); }; #endif
C++
복사

/registrar.cpp

// // registrar.cpp // // // Created by Glory on 2021/04/28. //Registrar 클래스의 구현 파일 #include "registrar.h" // 생성자 Registrar::Registrar() { } // 소멸자 Registrar::~Registrar() { } // enroll 함수 void Registrar::enroll(Student student, Course course) { (course.getRoster())->addStudent(student.getName()); (student.getSchedule())->addCourse(course.getName()); }
C++
복사
아래의 코드는 애플리케이션 파일이다.
애플리케이션 내부에서 registrar 객체는 딱 한개만 만들어 지는게 특징이다.
이걸 싱글 톤 패턴이라고 한다. (시험에는 안나와요!)
싱글 톤 패턴 소프트웨어 디자인 패턴에서 싱글턴 패턴(Singleton pattern)을 따르는 클래스는, 생성자가 여러 차례 호출되더라도 실제로 생성되는 객체는 하나이고 최초 생성 이후에 호출된 생성자는 최초의 생성자가 생성한 객체를 리턴한다. 이와 같은 디자인 유형을 싱글턴 패턴이라고 한다. 주로 공통된 객체를 여러개 생성해서 사용하는 DBCP(DataBase Connection Pool)와 같은 상황에서 많이 사용된다. 파이썬이 대표적으로 그 자체가 싱글톤 패턴이다. 자바는 생성자를 private으로 선언하여 상속이 불가능함을 지정하기도 한다.
C++
복사
아래의 코드 이어서 설명하면 student 객체를 3개 course 객체를 3개를 만들고 있다.
그리고 registrar 객체를 사용해서 학생을 특정 코스에 등록한다.

/app.cpp

// // app.cpp // // // Created by Glory on 2021/04/28. // 모든 클래스를 사용하는 애플리케이션 파일 #include "registrar.h" int main() { // Registrar 객체 인스턴스화 Registrar registrar; // Student 객체 인스턴스화 Student student1("John"); Student student2("Mary"); Student student3("Ann"); // Course 객체 3개 인스턴스화 Course course1("CIS101", 4); Course course2("CIS102", 3); Course course3("CIS103", 3); // Registrar 객체를 기반으로 학생이 과목 수강 등록 registrar.enroll(student1, course1); registrar.enroll(student1, course2); registrar.enroll(student2, course1); registrar.enroll(student2, course3); registrar.enroll(student3, course1); // Student 객체의 정보 출력 student1.print(); student2.print(); student3.print(); // Course 객체의 정보 출력 course1.print(); course2.print(); course3.print(); return 0; }
C++
복사

<시험>

시험문제에 서술형으로 시퀀스다이어그램이 그려하는게 나와요
코드랑 간단한 설명을 저렇게 놔두고 시퀀스 다이어그램을 그리시면 됩니다.
시험문제중에서 60~70 % 이상 PPT와 책에서 나와요
그리고 7주차 내용이랑 9주차 내용에서 2~3문제 정도 나오고요
숫자 계산하는거 하나 있거든요? 그거 낼지 안 낼지 모르겠지만 만약에 나온다면 다음에 한번 알려드릴꼐요.
정보처리기사 문제를 조금 가져올까 고민했는데, 공부량이 많아지실 것 같아서 요약집 드리고, 거기에 중요한것만 체크를 할께요. 그리고 개인적으로 정보처리기사 시험보시러 가실 때 까지 가지고 계시거나 공부하실 때에 쓰시면 좋을 것 같아요!