Software Engineering for Digital Home
1
單元 4:數位家庭軟體開發與再利用Chapter 4-3 – Mapping Models to Code
Software Engineering for Digital Home
Software lift cycle
2
Software life cycle
Development cycle
Design Implementation Testing
Maintenance
RequirementsEngineering
System design
Object design
Analysis Model(Class diagrams)
RequirementsElicitation
Analysis
SRSODD
(Class diagram)Code
ODD(Class diagram)
Sourcecode
Mapping
Software Engineering for Digital Home
Mapping Activities
3
Object Design model (Doc.)
(Class diagram)
1.Mapping Associations (concept)
Unidirectional, one-to-many association
Source Code
RDB Table
Aggregation
2.Mapping Contracts to Exceptions
3.Mapping Object Models to a Persistent Storage Schema
Mapping
Bidirectional, one-to-many association
Software Engineering for Digital Home
Class Diagram
4
使用者
老師
個人作業
學生
修課
1
*
課程
作業資料1* 上傳
Software Engineering for Digital Home
Mapping Object Models to RDB
5
編號 名稱 學期 說明課程 老師帳號
帳號 密碼使用者 電子郵件 電話
帳號 Office Hour老師 辦公室
帳號學生 系別
修課學生帳號組別課程編號
編號個人作業
名稱 截止日期課程編號 作業或報告 檔名
作業資料
大小學生帳號功課編號
Software Engineering for Digital Home
What is mapping?
• A transformation aims at improving one aspect of the model (e.g., its modularity) while preserving all of its other properties (e.g., its functionality).
(model在改善某一方面所做的轉變,同時維持其功能的特性 )
• A transformation is usually localized, affects a small number of classes, attributes, and operations, and is executed in a series of small steps.
(這些轉變是經過一些連續的步驟,是局部的、影響少數的class, attributes, operation)
6
Software Engineering for Digital Home
Four types of transformations
7
Source code spaceForward engineering(CH 4-3)
Refactoring (CH 4-3)Reverse engineering
Object Model space
Modeltransformation
(CH 3-6)
Software Engineering for Digital Home
Model Transformation Example
8
Object design model before transformation:
Object design model after transformation:
學生+Id:String+Password:String
老師+Id:String+Password:String
使用者+Id:String+Password:String
學生 老師
+OfficeHour:String
Software Engineering for Digital Home
Reuse concepts
• The focus of inheritance during object design is to “reduce redundancy” and “enhance extensibility”.(目的為降低系統重覆提高延展性 )
• Steps of design inheritance:– Step1: to factor all redundant behavior into a single
superclass.(步驟一:把重複的屬性操作都放到一個 superclass)
– Step2: we reduce the risk of introducing inconsistencies during changes since we have to make changes only once for all subclass.(步驟二:為了降低不一致的狀況發生,同時間我們只做一次步驟一的行為,換句話說,就是一次只產生一個 superclass)
9
Software Engineering for Digital Home
Refactoring Example: Pull Up Field
Before Refactoring
public class Student{private String Id;private String Password;//...
}public class Teacher{
private String Id;private String Password;//...
}
After Refactoring
public class User {private String Id;private String Password;
}
public class Student : User {//...
}public class Teacher : User {
//...}
10
Software Engineering for Digital Home
Forward Engineering Example Realization of User and Teacher
public class User{
public String Id;public String Password;
}
public class Teacher:User
{
public string OfficeHour;
public string Room;
}
11
Object design model before transformation
Source code after transformation
使用者+Id:String+Password:String
老師+OfficeHour:String+Room: String
Software Engineering for Digital Home
Transformation Principles
• To avoid introducing new errors, all transformations should follow these principles:– Each transformation must address a single criteria.
( 每個 Transformation是滿足一單一標準,是增進系統在單一目的,如一個 transformation是為了簡化類別 )
– Each transformation must be local. (Transformation是只有少數的 method or class)
– Each transformation must be applied in isolation to other changes.
(Transformation與其他的改變是隔離 )– Each transformation must be followed by a validation step.
(Transformation是人工的,應該要有確認的步驟以維持功能需求)
12
Software Engineering for Digital Home
Unidirectional one-to-many association
public class HomeworkData{
private TakeCourse _tc;
private File _file;
public Student getStudent() {
return _tc.getStudent(); }
public File getFile(){ return _file; }
public HomeworkData(TakeCourse tc){
_tc = tc;
}
//…
}
public class TakeCourse{private Course _course;private Student _student;public TakeCourse(){
}public Student getStudent(){
return _student;}//…
}
13
Object design model before transformation
Source code after transformation
修課作業資料1* 上傳
Software Engineering for Digital Home
Bidirectional one-to-many association
public class Student : User{private Collection<TakeCourse>
_takeCourses;public Collection<TakeCourse>
getTakeCourses(){
return _takeCourses;}
}
public class TakeCourse{private Student _student;private Course _course;public Student getStudent(){
return _student;}public void setStudent(Student student){
_student = student;}public Course getCourse(){
return _course;}
}
14
Object design model before transformation
Source code after transformation
修課1 *修課
學生
Software Engineering for Digital Home
Aggregation
public class Teacher: User {
private Collection <Courses > _courses;
public List getCourses(){
return _courses;
}
}
public class Course {
private Teacher _teacher;
public Teacher getTeacher(){
return _teacher;
}
public void setTeacher(Teacher teacher){
_teacher = teacher;
}
}
15
Source code after transformation
Object design model before transformation
老師 課程
Software Engineering for Digital Home
Class diagram contracts with OCL
16
Public class HomeworkData{ context HomeworkData::setFile(file:File) pre: file.size <= 3145728 and file.size > 0 and !file.FileName.EndWith.include(‘.bat’, ‘.exe’)
context HomeworkData::setFile(file:File) post: self._uploadDate = DateTime.Now self._file = file}
HomeworkData
-_student : Student-_file : File-_uploadDate: DateTime
+setFile(file: File ) : void+getFile() : File+getStudent(): Student+setStudent(student: Student ) : void+getUploadDate() : DateTime+setUploadDate() : void
Software Engineering for Digital Home
Mapping Contracts to Exceptions
Public class HomeworkData{
//…
Public void setFile(File file){
if(file.size() > 3145728 || file.size () <= 0) throw new OutofRangeException(“..”) ;
if( file.size () <= 0) throw new OutofRangeException(“..”) ;
const string[] filter = new string[]{“.bat”,”.exe”};
foreach(string s in filter){
if( file.FileName.EndWith(s) ) throw new SecurityException(“..”) }
//…
_uploadData = DateTime.Now;
_file = file;
}
//..
}
17
Software Engineering for Digital Home
Mapping Object Models to a Persistent Storage Schema
• UML object models mapping to relational databases:
• UML mappings
– Each class is mapped to a table(每個類別映射到一個資料表 )
– Each class attribute is mapped onto a column in the table(每個屬性對應到資料表的一個欄位 )
– An instance of a class represents a row in the table(資料表中的每個資料錄是對映到 class 的 instance)
– Inheritance association
– A one-to-many association is implemented as buried foreign key(一對多的關連是用外來鍵實做 )
18
Software Engineering for Digital Home
Inheritance Association
19
Primary key
Foreign key referencing
帳號CHAR(20)
密碼CHAR(20)
使用者 電子郵件CHAR(50)
電話CHAR(20)
帳號CHAR(20)
Office HourCHAR(20)
老師辦公室
CHAR(50)帳號
CHAR(20)系別
CHAR(20)
學生
…
Foreign key referencing
使用者
老師學生
Software Engineering for Digital Home
Mapping Object
20
學生 修課1 *
課程
帳號CHAR(20)
系別CHAR(20)
學生
… 編號BIGINT
課名CHAR(20)
課程
…
學生帳號CHAR(20)
課程編號BIGINT
修課 組別INT
Primary key
Software Engineering for Digital Home
Mapping Object
21
修課作業資料1* 上傳
編號BIGINT
檔名CHAR(255)
作業資料
大小BIGINT
檔案BINARY
學生帳號BINARY
作業項目編號BIGINT
學生帳號CHAR(20)
課程編號BIGINT
修課 組別INT
Primary key