Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
โดย อ.ดร. นฐพงศ สงเนยมhttp://[email protected]
สาขาวชา วทยาการคอมพวเตอรคณะวทยาศาสตรและเทคโนโลย มหาวทยาลยราชภฏพระนคร
Last Update : 30-01-2561
Lec08 : Structural Modeling ( Class & Object diagram)
แหลงขอมลเพมเตม : : http://www.lumpaya.com/sdlc01.htm
http://www.siam2dev.com [ dr. nattapong songneam]
สอบปลายภาค
• บทท 5, 6, 7, 8 และ กรณศกษา
Project งานกลม 30% ให นศ. แบงกลมๆ ละไมเกน 10 คน เพอทา
การวเคราะหระบบงาน 1 ระบบดวยวธการวเคราะหและออกแบบเชงวตถ
• จดทาเปนรปเลมรายงาน กาหนดสงวนสอบปลายภาค รปเลมรายงาน + CD
– กาหนดหวขอ Project •ระบบโรงพยาบาล •ระบบโรงภาพยนตร •ระบบสนคาคงคลง•ระบบโรงแรม •ระบบบรหารการศกษา / ระบบการเรยนการ
สอน •อนฯ
กาหนดความรบผดชอบ Project โดยใช Unified Process ทา ขอเสนอโครงการ Proposal วางแผน Gantt Chart + PERT chart ออกแบบ และเขยน- Usecase diagram - Class diagram - Sequence diagram / Communication Diagram –Activity Diagram
Project งานกลม 30% รายงานประกอบไปดวย1. คานา2. สารบญ3. สารบญรป4. สารบญตาราง5. บทท 1 บทนา
1. หลกการและเหตผล2. วตถประสงคของโครงการ3. ขอบเขตองระบบงาน4. แผนการดาเนนงาน (Gantt Chart)5. งบประมาณ/ คาใชจาย6. ฮารดแวรและซอฟตแวรทใช7. ผลทคาดวาจะไดรบ
รายงานประกอบไปดวย(ตอ)บทท 2 การวเคราะหระบบเชงวตถ
- Business Rule- Requirement Specifications
- functional- none functional
- Use case diagramบทท 3 การออกแบบเชงวตถ
- Class/Object diagram- Sequence/Commuication diagram - Activity diagram
บทท 4 การพฒนาระบบและทดสอบ- Deployment Diagram
บทท 5 สรปผลและขอเสนอแนะบรรณานกรมผจดทา
Project งานกลม 30% รายงานประกอบไปดวยคานาสารบญสารบญรปสารบญตารางบทท 1 บทนา
หลกการและเหตผลวตถประสงคของโครงการขอบเขตองระบบงานแผนการดาเนนงาน (Gantt Chart)งบประมาณ/ คาใชจายผลทคาดวาจะไดรบ
รายงานประกอบไปดวย(ตอ)บทท 2 การวเคราะหระบบเชงวตถ
- Business Rule- Requirement Specifications
- functional- none functional
- Use case diagramบทท 3 การออกแบบเชงวตถ
- Class/Object diagram- Sequence/Commuication diagram - Activity diagram
บทท 4 การพฒนาระบบและทดสอบ- Deployment Diagram
บทท 5 สรปผลและขอเสนอแนะบรรณานกรมผจดทา
INCEPTION PHASE
COLLABORATION PHASE
IMPLEMENTATION /
TRANSITION PHASE
อ.ดร. นฐพงศ สงเนยม 6
Requirement Specifications : ขอกาหนดของความตองการ
UP :: โครงสรางกรรมวธ - Lifecycle Phases
เตรยมงาน (Inception) – นยามขอบเขตของโครงการ , ขอบเขตของระบบทจะพฒนา
OOAD : Object-Oriented Analysis and Design 7
Inception Elaboration Construction Transition
time
Unified process แบงการพฒนาออกเปน 4 เฟส (phases)
ทารายละเอยด (Elaboration) – วางแผนโครงการ จดทารายละเอยดความตองการ จดสรางสถาปตยกรรมระบบ
จดสราง (Construction) – สรางและทดสอบโปรแกรม
ถายโอน (Transition) – ตดตงถายโอนระบบใหกบผใช
7
Requirement Analysis
Requirement Analysis-Requirement Specification-Use Case Diagram
Design >> Class / Object Diagram
The Iterative Approach
OOAD : Object-Oriented Analysis and Design 8
Disciplinesgroup activities
logically
In an iteration,you walk through
all disciplines
8
1. Software Development Process
1. Requirement Specification : define problem domain
2. Analysis : what problem to be solved? (อะไรคอปญหาทตองแก)
3. Design : how to solve the problem? (แกอยางไร)
4. Implementation : how to implement the solution?
5. Testing : how to ensure that the solution can solve the problem?
1. ทดสอบในเรองความเรว ประสทธภาพ ความปลอดภย ความมนคง เสถยร
6. Maintenance : how to adjust the solution to accomodate change?
1. ในรอบระยะเวลาหนงอาจจะตองมการปรบเปลยน
7. Retirement : when does the system to be retired?
บทท 5 Requirement Specification
จะพดถงในวชา OOP
ผใชระบบสารสนเทศ:แหลงของความตองการ
• เจาของระบบ (System owners/Sponsors ) – มสวนไดสวนไดเสยจากการลงทนสรางระบบสารสนเทศ เจาของผบรหาร ผจดการ
• ผใชภายใน (Internal users)– End-users คอผใชทปอนขอมลเขาสระบบโดยตรง ไมจาเปนตองมทกษะหรอความร
มาก เนนความถกตองและรวดเรวของการปอนขอมลเขาสระบบ– Power-users คอผใชทมความรความชานาญเฉพาะดาน สามารถใชงานฟงกชน
ของระบบในสวนทมความซบซอนได – Administrators คอผทดแลและควบคมใหระบบสามารถดาเนนการไดอยางราบรน
ตามวตถประสงคทตงไว– Executive users คอผใชทตองการสารสนเทศมาเพอการตดสนใจและบรหาร
องคกร• ผใชภายนอก (External users)
– ผใชซงเปนบคคลภายนอกองคกร แตสามารถเขาถงบรการของระบบในองคกรได
จากทเคยสอนไปแลว บทท 5
Lecture Outline• Structural Modeling• Class Diagrams and Object Diagrams• Association• Generalization และ Classification• Aggregation และ Composition• Models
Structural Modeling
What is structural modeling?• Structural model
– กคอ มมมองของระบบทเนนโครงสรางของวตถ รวมท งคลาสของวตถ (Class) ความสมพนธระหวางคลาส (Relationship) แอททรบวต (Attribute) และ โอเปอเรชน (operation)
Static Structural Modeling ม 2 ชนดคอ• Class Diagram• Object Diagram
Construct Description Syntax
class a description of a set of objects that share the same attributes, operations, methods, relationships and semantics.
interface a named set of operations that characterize the behavior of an element.
component a modular, replaceable and significant part of a system that packages implementation and exposes a set of interfaces.
node a run-time physical object that represents a computational resource.
«interface»
Structural Modeling: Core Elements
Analysis Process แนวทางในการวเคราะห
• ทา Information gathering จะได >> Requirement Specifications >> นามาเขยนเปน Use case Description / Diagram
• จากนน นา Use case >>มาหา Class และ Object ทงหมดของระบบจากนนกนามาสรางเปนแผนภาพคลาส หรอ Class/Object Diagram
Structural Modeling: Core Elements (cont’d)
Construct Description Syntax
constraint¹ a semantic condition or restriction.
{constraint}
¹ An extension mechanism useful for specifying structural elements.
Constraint เงอนไข/กฏเกณฑ / ขอกาหนด
Construct Description Syntax
association a relationship between two or more classifiers that involves connections among their instances.
aggregation A special form of association that specifies a whole-part relationship between the aggregate (whole) and the component part.
generalization a taxonomic relationship between a more general and a more specific element.
dependency a relationship between two modeling elements, in which a change to one modeling element (the independent element) will affect the other modeling element (the dependent element).
Structural Modeling: Core Relationships
composite
Aggregate
Construct Description Syntax
realization a relationship between a specification and its implementation.
Structural Modeling: Core Relationships (cont’d)
Structural Diagrams• ประเภทของ Structural Diagrams
– static structural diagrams• class diagram• object diagram
– implementation diagrams• component diagram• deployment diagram
Static Structural Diagrams
Static Structural Diagrams• แสดง graph ของคลาสทเชอมตอกนดวย
static relationships• ประเภทของ Static Structural Diagrams
– class diagram : classifier view– object instance diagram : instance view
Static : หยดนง
Class Diagrams• Class diagrams
– แสดงรายละเอยดของ Class และความสมพนธระหวาง Class ในมมมองแบบ logical view
• องคประกอบของ UML class diagrams ไดแก– Class, โครงสรางของ Class และพฤตกรรมของ
Class– ตวบงช Multiplicity และ navigation – ชอของ Role– ความสมพนธแบบ Association, aggregation,
dependency, และ inheritance
• Logical : ตรรกะ/ความคด
• Physical : กายภาพ/สภาพความเปนจรง ลกคา
ซอ
สนคา
ERD
Physicalเกบในฐานมล มโครงอยางไร
อย Address ทเทาไรอยตรงสวนใหนของ ฮารดกส
x 5
5 9 1 4 7
ARRAY
Offset :; 01011111
Classes in UML
Person
Class name
Attributes
Operations
• เขยนได 2 รปแบบ
Person
- TaxIDNo : String- Name : String+ Income : double+ TaxPaid : Boolean
+ calcTax()+ calcTaxBal()
attribute name : type
operation name(parameter : type): result type
En
Analysis เขยนแบบคราวๆ
Design ลงรายละเอยด
Classes in UML
คน
Class name
Attributes
Operations
• เขยนได 2 รปแบบ
คน- หมายเลขประจาตวผเสยภาษ : String- ชอ : String+ รายได : floating + เสยภาษ : Boolean
+ คานวณภาษ()+ คานวณยอดเสยภาษ()
attribute name : type
operation name(parameter : type): return type
Th
UML Syntax for Attributesรปแบบ• visibility name : type
+ id : Stringความหมาย
– visibility: public (+), protected (#), private (-)• การตความไมขนกบภาษาในการเขยนโปรแกรม
programming language independent – name : เปนชอของ attributes– type : ประเภทของขอมล ไมข นอยกบภาษา
ไมไดข นอยกบภาษาใดภาษหนง ใชไดกบทกภาษา ทเปนเชงวตถJava , vb, c#, php, อนๆ
#java ….Modifier ซงจะมมากกวา 3 ชนด จะม default
หามนา Type ในภาษาใดภาษาหนงมาใชกบ UML classdiagram
Person
- TaxIDNo : String- Name : String+ Income : real+ TaxPaid : Boolean
+ calcTax()+ calcTaxBal()
Real เปนชนดตวแปรทศนยม ในภาษาปาสคาลเทานน ภาษาอนไมรจกUML ไมผกตดกบภาษา
Real Pascalfloat,Float c, c++, javadouble,Double c, c++, javaSingle,DoubleVB
Floating,String,Boolean
แสดงผลบนหนาจอ1. Echo2. Print3. Printf4. Scanf5. System.out.print6. System.out.println7. Write8. Writeln9. Display10. Input11. Read
แตละคาสงคอภาษาอะไร แลวไวทาอะไร
Display “Menu”
ในโลกนมภาษาคอมพวเตอร 160 กวาภาษา
คณรจกแค 2-3 ภาษา
เขยนไดแค 1 ภาษา
เขยนเปน 0 ภาษาจงเขยนโปรแกรม Paintดวยภาษาท ถนด
สรางรป
บนทกได
เปดได
Type- PDT- ADT
Class NameFAN
Attributes- Brandname : String- Price : Floating
เงน ?
เงน1
2
3
ขนาด
ส
มลกษณะเปนวงกลม
เงน ?
เงน1
2
3
เงนบาท
ขนาด
ส
มลกษณะเปนวงกลม
เงน ?
เงน1
2
3
- มลคา : double/floating-point
ขนาด
ส
มลกษณะเปนวงกลม
เงนดอลลาร
เงนบาท
เงนหยวน เงน
มคา
จานวนเงน/ราคา
ส
ใชจาย()ชาระ()
พดลม
พดลม1
2
3
- ยหอ : String- ส : Color- ราคา : Floating-Point
มใบพดทาความเยน
บอกราคา
บอกส
กาหนดราคา
UML Syntax for attributes
ในการทจะเขยนคลาสได จะตองคานงเรองตอไปน
1. Problem Domain2. Concept 3. Abstractions ตางๆ
4. Real World , computer world5. Analysis , requirement spcecification , Usecase
diagram
รถยนต
Attributes + functions , Association
โต โยตาอลตสBMWViosCivicTeanaswift
Concept
Car Human
Association Name
Role name
1 1
Own Owner
Own
Cardinality/Multiplicity
Class Name
Engine
- Name : String - ID : String- Tel : String
Student
class Student{
private String Name;private String ID;private String Tel;
public String getName() {return Name;
}}
class testStudent {public static void main{
private Student s;s = new Student();s.ID = ‘580001011’;txtName.setText(s.getName());
} }
Encapsulation / Class / Visibility
Student Tester
main
test
java
class Money {private double value;
}
เงน1
2
3
- มลคา : double/floating-point
public class CAR {private String Brand;private double Price;private Color color;
}
CAR
- Brand : String- Price : Double- Color : String
+ getBrandname()+ getPrice()+ setPrice
Empty Class
altis : CAR
object
class
class Color {}civic : CAR
Type
• String• Character• Boolean• Date• Integer : byte , short , int , long• Floating : single / double
VB
เครองบน
Plane- เครองยนต : String- ปก : integer- หมายเลขเครอง : String
Computer- จอ : Monitors- Mouse : Mouse-Keyboard: Keyboards- Brandname : String- Price : Double
-Private+ Public# Protected
เครองบน
Plane- เครองยนต : String- ปก : integer- หมายเลขเครอง : String
Computer- จอ : Monitors- Mouse : Mouse-Keyboard: Keyboards- Brandname : String- Price : Double
-Private+ Public# Protected
PDT
เครองบน
Plane- เครองยนต : Engine- ปก : integer- หมายเลขเครอง : String
Computer- จอ : Monitors- Mouse : Mouse-Keyboard: Keyboards- Brandname : String- Price : Double
-Private+ Public# Protected
ADT
Engine- ยหอ: String- รน : String- แรงมา: Integer / int
Plane- เครองยนต : Engine- ปก : integer- หมายเลขเครอง : String
Require Component / Optional Component
Aggregate vs Composite
Attributes
Car- Wheel : Integer
- Wheel : Integer
- w : Wheel
Require Component/Optional Component
CAR
- Brandname : String
- Wheel : Wheels
+ Start() : Boolean
+ Stop() : Boolean+ Break() : Boolean
Wheels
- Brandname : String
- NoumOfW : Integer
+ Spin() : Boolean
+ TurnLeft() : Boolean+ TurnRight() : Boolean
เครองปรบอากาศ วทย พวงมาลย
Aggregate Composit
Variables declaration
• VBDim x as int16Private x as int16Protected x as int16PUBLIC x as int16
• C, C++ , Java ,C#int x;private int x;protected int x;public int x;
java
class car {private int wheel;
}
class car {private Wheels wheel;
}
class Wheels {private int no_wheel;
}
UML syntax for operations• visibility name ([parameter list]) : return-type-expression
•+ assignAgent (a : Agent) : Boolean
– visibility: public (+), protected (#), private (-)• การตความไมข นกบ programming language
– name : string– parameter list : arguments– return-type-expression : ประเภทของคาท return ไมข นกบภาษา
Double x = pow(2,3);
Optional
+ pow(x1 : int,x2 : int) : double
double x = pow(2,3);
ตวอยางชนดของวตถและวตถ (UML)
MobilePhone
- bandname :String- button : Button- screen : Screen- antenna : Antenna- simNet : Simm
+ call() : String+ receive()+ hangup()+ sendSMS(msg : String)
:siemens
:ericsson
:nokia
53อ.ดร. นฐพงศ สงเนยม
class objects
MobilePhone
- bandname :String- button : Button- screen : Screen- antenna : Antenna- simNet : Simm
+ call() : String+ receive()+ hangup()+ sendSMS(msg : String)
Return
Not Return
Parameter List
UML Syntax for functions
- Name : String - ID : String- Tel : String
Studentclass Student{
private Sting Name;private String ID;private String Tel;
public String getName() {return Name;
}public void setName(String n) {Name = n;
}}
class testStudent {public static void main{
private Student s;s = new Student()s.ID = ‘580001011’;s.getName();
} }
Encapsulation / Class / Visibility
+ getName() : String+ setName(n : String)
CAR PersonOWNEE OWNER
Student TeacherAdvisee Advisor
InstructorMasterProfessor
- Registration:String
+ setRegis(rg: String)+ getRegis() : String
1 1
Tester
public class Tester{public static void main(String args[]) {
Car c1= new Car();Person p1 = new Person();c1.Registration =“4ก 4754”;
}}
class Car {private String Registration;}
class Student{
private Sting Name;private String ID;private String Tel;
public String getName() {return Name;
}public void setName(String n) {Name = n;
}public void setTel(String t) {Tel = t;}public String getTel() {Return Tel;}
}
•บอกชอ
•ตงชอ
•บอกเบอรโทร
•กาหนดเบอรโทร
Operations/functions
Student
Behavior/Operations/functions/Method/Sub/Routine/Module
TypePDTADT
Class NameFAN
Atributes- Brandname : String- Price : Floating
+ Open() : Boolean
+ Close() : Boolean+ Spin() : Boolean
TypePDTADT
Class NameTelevision
Atributes- Brandname : String- Price : Floating
+ Television()
+ Close()+ volume() : int
+ เปด () : Boolean
+ ปด () : Boolean
+ Open()Constructor
CustomerCustomer
-Name : String-Address : String
Attributes – A Rule• การกาหนด attributes ควรทาให simple• คาทเกบอยใน attributes ไมควรเปนวตถ (Object)
– แตควรสรางคลาส (class) ขน และแสดงความสมพนธระหวางวตถดวย association
• ไมควรสรางความสมพนธระหวาง 2 คลาส โดยใช “foreign key” attribute– แตใช association
ควรจะมองใหเปนความสมพนธเชงฟงกชน หมายถง คลาส ทางานอะไร รวมกนกบอกคลาส
Object Instance Diagrams• class diagram
– แสดงชอ (name), โครงสราง (structure) และพฤตกรรม (behavior) ของประเภทของวตถ(object)
• object instance diagram– แสดงวตถทสรางจากคลาส
• ความแตกตางระหวางทง 2 diagram เปรยบเทยบไดกบความแตกตางระหวางโครงสรางของตารางในrelational database และคาทถกเกบอยภายในตาราง
ID Name Address salary Tel
class
ID Name Address salary Tel001 สมชาย กรงเทพ 85000 087984740
002 นฐพงศ กรงเทพ 3500 089747454Object
คาถาม :: จงอธบายในเชงของคลาสไดอะแกรม class และ object diagam
Objects in UML
Tim:Person
Object name
Attributes
Same Operationsfor all instances of classes
• เขยนได 2 รปแบบ
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
attribute name : type = value
En
Person
ตางกนอยางไร
Person
Object / Class
ธนพนธ : นกเรยน นกเรยน
ธนพนธ : นกเรยน นกเรยน
- ชอ = “ธนพนธ” - ชอ : String
+ บอกชอ() + บอกชอ()สปราณ : นกเรยน
- ชอ = “สปราณ”
+ บอกชอ()
ธนพนธ : นกเรยน
นกเรยน
- ชอ = “ธนพนธ”
- ชอ : String
+ บอกชอ()
+ บอกชอ()
สปราณ : นกเรยน
- ชอ = “สปราณ”
+ บอกชอ()
Is member ofIs member of
Instance of
class
object
นฐพงศ : นกเรยน
- ชอ = “นฐพงศ”
+ บอกชอ()
Student
Student
class
- Name : string- ID : string- Tel : string- grade : double
-getName() : string-getGPA() : double
Tom: Student- Name =“Tom Meesuk”- ID =“5870144”- Tel =“0897887744”- grade =3.50
-getName() : string-getGPA() : double
object
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Jim : Person
TaxIDNo=“23005”Name=“TimLim”Income=“80000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Toon : Person
TaxIDNo=“23001”Name=“Deelan”Income=“50000.00”TaxPaid = TRUE
calcTax()calcTaxBal()
คาถาม “Same Operationsfor all instances of classes”
หมายถงอะไร จงอธบาย ?
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Tim : Person
TaxIDNo=“23004”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Tim : Person
TaxIDNo=“23003”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Tim : Person
TaxIDNo=“23004”Name=“TimLim”Income=“30000.00”TaxPaid = FALSE
calcTax()calcTaxBal()
Objects in UML
นายดา : คน
Object name
Attributes
Same Operationsfor all instances of a class
• เขยนได 2 รปแบบ
นายดา : คน
- หมายเลขผเสยภาษ=“450103”- ชอ=“นายดา”+ รายได=“50000.00”+ เสยภาษ = FALSE
คานวณภาษ()คานวณยอดภาษ()
attribute name : type = value
Th
An Object Instance Diagram
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000
London:City
- Name=London- Country=UK- Population= 2,324,320
New York :City
Name=New YorkCountry=USAPopulation= 5,734,012
Bangkok :City
Name=BangkokCountry=ThailandPopulation= 7,534,013
En
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000 + getPopulation()
+ setName(n : string)
ยง กบ แมลงวน ?
- Name : String - ID : String- Tel : String
Student
class Student{
private Sting Name;private String ID;private String Tel;
public String getName() {return Name;
}}
class testStudent {public static void main{
private Student s;private Student s1;private Student s2;private Student s3;private Student s3;
s = new Students.ID = ‘580001011’;txtName.setText(s.getName());
} }
Encapsulation / Class / Visibility
Java implementationLondon:City
- Name=London- Country=UK- Population= 2,324,320
+ getPopulation() : long+ setName(n : string)
class City {private String name;private String country;private long population;
public void setName(n:String) {name : n;
}public long getPopulation() {
return population;}
}
UML : object diagram
City.java
Class name
London:City
- Name=London- Country=UK- Population= 2,324,320
+ getPopulation() : long+ setName(n : string)
object
visibility Operation
Attributes
Class name
Attributes
Operations
Class name
Attributes
Operations
- Name : String- getName() : String- setName(n:string)
London:City
- Name=London- Country=UK- Population= 2,324,320
Attributes
Operations
An Object Instance Diagram
Sydney:City
Name=SydneyCountry=AustraliaPopulation= 3,536,000
London:City
Name=LondonCountry=UKPopulation= 2,324,320
New York :City
Name=New YorkCountry=USAPopulation= 5,734,012
Bangkok :City
Name=BangkokCountry=ThailandPopulation= 7,534,013
Th
Classes and Objects
London : City
Name = LondonCountry = UK
<<instanceOf>> <<instanceOf>> <<instanceOf>>
City
- Name : String = default
+ setName (s : String = deault)
+ setPopulation(p : integer = default)
- Population : integer = default
Population =2,324,320
New York : City
Name = New YorkCountry = USA
Population =5,734,012
Sydney : City
Name = SydneyCountry = Australia
Population =3,536,000
- Country : String = default
สญลกษณทชจากออบเจกตไปยงคลาส คอ.......................
Is member ofIs member of
Class Diagrams
• Associations
• Classification
• Generalization และ Specialization
• Aggregation
Association• ความสมพนธ (Relationship) ระหวาง instances ของ classes
Order
- dateReceived- isPrepaidnumber : String price : Money
dispatch( )
Customer
- Name : Stringaddress
+ creditRating( )
* 1
issued by
An order comes from one customerCustomer may make several orders
directionassociation
name
En
* 0...N
Association• ความสมพนธ (Relationship) ระหวาง instances ของ classes
ใบส งซอ
- วนทรบ : Date- ชาระเงน : Boolean- เลขท : String - ยอดเงน : Money
+ วางบล( )
ลกคา
- ชอ : String- ทอย : String
+อตราสนเชอ( )
* 1
ส งซอโดย
ใบส งซอหนงใบมาจากลกคาเพยงคนเดยวลกคาหนงคนส งซอไดหลายใบส งซอ
directionAssociation name
Th
Associations• คลาสมากกวา 1 คลาส สมพนธกนดวย “associations”• Association
– เทยบไดกบ ER relationship ทเชอมโยงความสมพนธระหวางคลาส และแสดง จดเชอมโยง (“links”) ระหวางวตถทสรางขนจากคลาส
Naming associations• การต งชอ association
– หลกเลยงชอทไมส อความหมาย• associated_with, has, is_related_to
– มกขนตนดวยคากรยา เชน• works_for, owns , issued by
– มกต งชอในกรณทมอง Association จากซายไปขวาหรอ จากบนลงลางของ diagram
Associations on Class Diagrams
OwnsPerson
NameAddressTelephone
Car
ManufacturerModelRegistration
En
Associations on Class Diagrams
เจาของคน
ชอทอยเบอรโทร
รถยนต
ยหอรนเลขทะเบยน
Th
Links• วตถแตละวตถ สามารถเชอมโยงกนได โดยใช “links”• A link
– เปน instance ของ association• หมายเหต
– an association สามารถดารงอยได ถงแมวาจะไมมinstance (links) ของ association ดงกลาว เชนเดยวกบ คลาสทสามารถดารงอยได ถงแมวาจะไมมวตถใดๆ ถกสรางขนจากคลาสนนๆ
Objects & Links
Name=SomchaiAddress=22 BKKTelephone=62312198
Name=PancakeAddress=41 SKLTelephone=62661734
Manufacturer=BMWModel=SeriesRegistration=YRT833
Manufacturer=ToyotaModel=altisRegistration=YYX391
OwnsOwns
Owns
You:Person
Ours:Car Mine:Car
Me:Person
Enรไดอยางไร วาเปน object หรอ class diagram
คาถาม owns เปน Link หรอ Association ?
javaclass person {private String name ;
}
class TestPerson {private Person you ;
you.setName(“somchai”);
}
You , me , mine , ours เปนวตถ
Person , car เปนคลาส
vbClass Car
PrivatE name as string…
End classclass person {private String name ;
}
java
Objects & Links
Name=ณเดชAddress=22 กทมTelephone=02578268
Name=แพนเคกAddress=41 SKLTelephone=03425464
Manufacturer=BMWModel=SeriesRegistration=1กย 6666
Manufacturer=BenzModel=E320Registration=1กค 9999
เจาของเจาของ
เจาของ
ณเดช:Person
รถณเดชและแพนเคก:Car รถแพนเคก:Car
แพนเคก:Person
Th
Association Multiplicity• multiplicity
– หมายถง การพจารณาจานวน instances (objects) ของคลาสหนง ทสามารถเชอมโยงกบ instance (object) ของคลาสทเกยวของ
Order
dateReceived
isPrepaid
number : String
price : Money
dispatch( )
Customer
nameaddress
creditRating( )
* 1
En
* 0..n
Association Multiplicity• multiplicity
– หมายถง การพจารณาจานวน instances (objects) ของคลาสหนง ทสามารถเชอมโยงกบ instance (object) ของคลาสทเกยวของ
ใบส งซอ
- วนทรบ : Date- ชาระเงน : Boolean- เลขท : String - ยอดเงน : Money
+ วางบล( )
ลกคา
- ชอ : String- ทอย : String
+อตราสนเชอ( )
* 1ส งซอโดย
Th
Association Multiplicity
Class
Class Class
Class
Class1..*
2 ..4
exactly one
many (zero or more) zero or one/at most one
one or more
as specified
* 0..1
1
Association Multiplicity
Class
Class Class
Class
Class1..*
2 ..4
exactly one
many (zero or more) zero or one/at most one
one or more
as specified
* 0..1
1
En
Association Multiplicity
Class
Class Class
Class
Class1..*
2 ..4
มแค 1 เทานน
หลาย (ศนยหรอมากกวา) ศนยถงหนง/มากสดแคหนง
หนงหรอมากกวา
ตามทระบ
* 0..1
1
Th
Example of Multiplicities
Student University
HeartBody
Bicycle Wheel
OwnerAccount
2..3
1..*
* *
*
**
1..*
1 1
1
1..* 1..*
จงอธบายแตละอยาง ตามความเขาใจ ?
Associations, Objects & Classes• สาหรบ association ใดๆ ระหวาง 2 วตถ สามารถม link ได
ตงแต 0 ถง 1 link• ระหวาง 2 วตถ สามารถไดมมากวา 1 association
(Multiple Assoications) แต association เหลานนจะตองมความแตกตางในดานความหมายของ association
• Multiple associations ระหวางคลาสกาหนดให ตงชอ associations
Roles• role
– หมายถง ชอทกาหนดใหกบดานปลายของ association ซงระบวธการในการทคลาสมสวนรวมใน association
• ปกตชอของ role มกเกดขนเปนค (เชนทงสองดานของassociation จะมชอของ role
• บงคบใหกาหนดชอของ Role กบ associations แบบreflexive (Reflexive associations)
Role names• Role ระบ ชอใหกบดานปลายของ association
NameAddress
Company Works forNameInsurance no.Address
Person
employer employee
*1
Role names
จากรปน จงอธบาย ?
Role names• Role ระบ ชอใหกบดานปลายของ association
NameAddress
Company Works forNameInsurance no.Address
Person
employer employee
*
Role names
Role names• Role ระบ ชอใหกบดานปลายของ association
ชอบรษททอย
บรษท ทางานใหชอรหสประจาตวทอย
พนกงาน
นายจาง ลกจาง
*
Role names
Association Names & Roles• association อาจมชอไดเชนเดยวกบชอของ role ทกาหนด
ใหกบดานปลายของความสมพนธ– ถามการตงชอ association แลวมกไมมการตงชอ role อก– ถามการตงชอทปลายของ association (ไดแก roles) แลว
มกไมมการตงชอ association อก
Role names• บงคบใหกาหนดชอของ Role ใหกบทเกดขนระหวาง link ของ
วตถทสรางจากคลาสเดยวกน
NameInsurance no.Address
Person
Manager
Supervises Salesperson
U-nary association
A Reflexive Association
PersonParent
Child
2 Role name
NameAddressTelephone
*
• association ทสรางจากคลาสเดยวกน
U-nary association
Person Personparent child
2 *
A Reflexive Association
PersonParent
Child
2 Role name
NameAddressTelephone
*
• association ทสรางจากคลาสเดยวกน
Ternary Associations• A ternary association
– เปน assocation ทประกอบดวย 3 คลาส• เชนเดยวกบแนวคดของ ternary relationship ในER
modelling และแนวคดคลายๆ กน• นาเสนอโดยใชรปสเหลยขนมเปยกปน เชอมโยงกบคลาสท
เกยวของ
Ternary Associations
Vendor
Customer Product
Ternary association
Sale
Ternary Association Multiplicity• multiplicity ของ ternary associations• ซบซอนกกวา binary และ unary associations• มกเปน แบบ “many” สาหรบคลาสทเกยวของ participating
N-ary Associations
• โดยทวไปแลว association อาจเชอมโยงกบคลาสจานวนเทาใดกได
• ถาเชอมโยงคลาสมากกวา 2 คลาสขนไป (รวมทง ternary) เรยกวา “n-ary” associations
• แต Associations ทเชอมโยงมากกวา 0 คลาสมกหาไดยาก
Summary: Basic notation for associations
Class A Class BAssociation name
role_Arole_B
Order Order Linecontains
line items
• ตวอยางเชน
mult1 mult2
*1
Summary: Basic notation for associations
Class B Class B
Association name
role_Arole_B
Order Order Linecontains
line items
ตวอยาง
mult1 mult2
*1
Association Classes• Link จะถกพจารณา เชนเดยวกบการทวตถถกพจารณาจาก
attribute ของวตถนน• attribute ควรผกตดอยกบ association โดยใช
association class มากกวาทจะเปนของวตถทเชอมโยงเมอใดกตามทwhenever it exists or has meaning when the link itself exists (rather than just the objects linked) - มกเกดขนบอยกบ many-to-many associations
Association Classes
Student SubjectIDNameAddressTelephone
CodeNameCredit
Mark
Enrolment
Association class
* *
Result
many-to-many associations
Dependency Association
Dependency Association
ID Name Address Tels001 โกต กาลงกราง ไมแนนอน 0879774s002 สเทพ ราชดาเนน ไมแนนอน 0879774s003 ยงยวด ยงแย ไมแนนอน 0879774
Student
code name credit descc001 การเปนคนด 6(6-3)c002 คอรปชน 6(6-6)c002 เคารพสถาบน 6(6-6)
Subject
M
M
Associations class
Enroll_ID Std_id Sub_ID commnetE0000001 s001 C001E0000002 s001 C002E0000003 s001 C003E0000004 s002 C002E0000005 s002 C003E0000006 s003 C001
Regis / Result
Association Classes
Student SubjectIDNameAddressTelephone
CodeNameCredit
Mark
Enroll
Association class
* *
Result
Association Classes
• association class สามารถมสวนรวมใน associations อนๆเชนเดยวกบทคลาสปกตสามารถมได
Association Classes
Customer ProductNameAddressTelephone
NumberNamePrice
DateLengthConditions
Purchase
Association class
Guarantee
Claim
* *
*
0..1
dependency
supplier
Vender Products
Buy-items
* *
POS
ID Name Address Tels001 โกต กาลงกราง ไมแนนอน 0879774s002 สเทพ ราชดาเนน ไมแนนอน 0879774s003 ยงยวด ยงแย ไมแนนอน 0879774
Customer
code name price QTYc001 Computer 35000 10
c002 Printer 4500 35
c002 speaker 850 100
Product
M
M
Associations class
Guarantee_ID Date Length ConditionE0000001E0000002E0000003E0000004E0000005E0000006
guarantee
Association Classes
Customer Product
NameAddressTelephone
NumberNamePrice
DateLengthConditions
Purchase
Association class
Guarantee
Claim
* *
*
0..1
Qualified Associations• qualified association
– หมายถง association ทถกทาใหชดเจนโดย attributeทเรยกวา qualifier
– พจารณา qualified association เชนเดยวกบแนวคดของweak entity type ใน ER
Qualified Associations
Golf_CourseNameLocation
Hole
NumberLengthPar
Unqualified
*1
Golf_Course
NameLocation
HoleNumberHoleLengthPar
Qualified
0..11
Qualified Association
Tournament PrizeYearPosition
0..1
Qualified association
ABC Inc. President Roger RabbitABC Inc. Vice President Finances Joe SavemoneyABC Inc. Member of board John WalkerABC Inc. Member of board Susi SanssouciABC Inc. Member of board Karl EichbaumXYZ Inc. President Donald Duck
PersonCompanyOrganization
*Function
Class Diagrams: Hints• Class หนงสามารถเปนสวนรวมไดในหลาย diagrams• Diagrams ควรจะแสดงแงมมเฉพาะดาน
– จานวนคลาสไมมากเกนไป– จานวน associations ไมมากเกนไป– ซอน attributes และ operations ทไมจาเปนตองแสดง
• อาจตองมการสราง diagram ซาๆ กนหลายรอบ
student
student
Enroll System
Registration System
PloblemDomain
Difference between attributes and associations
• Conceptual perspective– ไมแตกตางกนมากนก– Attributes เกบคาเดยว (single-valued)
• Specification/implementation perspective– Attribute เกบคา (Value) ไมใชตวอางอง (References)
• ไมมการ share สวนของ attribute values ในระหวาง instances ทสรางจากคลาสเดยวกน
• Often: Stores simple objects – Numbers, Strings, Dates, Money objectst
Require Component/Optional Component
CAR
- Brandname : String
- Wheel : Wheels
+ Start() : Boolean
+ Stop() : Boolean+ Break() : Boolean
Wheels
- Brandname : String
- NoumOfW : Integer
+ Spin() : Boolean
+ TurnLeft() : Boolean+ TurnRight() : Boolean
เครองปรบอากาศ วทย พวงมาลย
Class diagrams (cont.)
• Associations• Generalization• Generalization และ
Classification • Aggregation
• BREAK : 20-09-2557 • สาหรบ นศ. IT 30/1
Generalization• แนวคดของหลกการเชงวตถ
– Generalization จดการ classes ใหอยในรปของโครงสรางลาดบชน (class Hierarchy) ขนกบ similarities และdifferences
– เราเรยกคลาสทอยในระดบทสงกวาของ classes hierarchy วา “superclasses” และทอยในระดบตากวาวา“subclasses”
– เปน “kind-of” relationship
Inheritance and Polymorphism• “subclass” รบถายทอดคณสมบต (inherits)อนไดแก
attributes, operations และassociations มาจาก“superclass” ของตวเอง
• แตอยางไรกตาม ถา attribute หรอ operation ของ“superclass” ถกกาหนดใหคาใหมใน “subclass” จะเปนการ “overrides” คานยามทกาหนดไวใน “superclass” -นาสแนวคดของ polymorphism
Superclasses and Subclasses• “Superclass” มคานยาม attributes, operations และ
associations รวมกนกบ “subclasses” ของคลาสนนๆ• “Subclasses” ม attributes, operations และ associations
เฉพาะเปนของตวเอง โดยเลอกทจะกาหนดนยามใหมใหกบ attribute, operation หรอ relationship ทรบมาจาก “superclass” หรอไมกได
A Generalization Hierarchy
Geometric_FigureX_PositionY_Position
EllipseMajor_AxisMinor_Axis
CircleRadius
RectangleWidthHeight
Superclass
SubclassescalArea() calArea() calArea()
Polymorphism
y
x
Single & Multiple Inheritance• generalizations มกเกดใน class hierarchies ทแตละ
“subclass” มเพยง 1 “superclass” - เรยกวา “single inheritance”
• แตมบางสถานการณท “subclass” อาจมไดมากกวา 1 “superclass” - เรยกวา “multiple inheritance”
Multiple InheritanceEmployee
GeneralStaff ProfStaff FullTime PartTime
AcademicSubclass withtwo superclasses
คาถาม จากรปนสวนไหนทเปน multiple inheritance เพราะอะไร ?
Multiple Inheritance
Vehicle Furniture
Wheelchair
Specialization• Specialization
– เปนกระบวนการยอนกลบของ generalization แตใหผลเชนเดม แตกตางกนทจดเรมตนเทานน
• Generalization เปนการคนหาคณลกษณะรวมกน ของ(sub)classes
• Specialization เปนการแยก และคนหาคณลกษณะพเศษของ (super)classes เพอใหได subclasses
Class diagrams (cont.)• Associations• Generalization• Generalization และ Classification• Aggregation
Generalization and Classification (= is_a?)1. Shep is a Border Collie.2. A Border Collie is a Dog.3. Dogs are Animals4. A Border Collie is a Breed.5. Dog is a Species
1+2: Shep is a Dog1+2+3: Shep is a animal1+4: Shep is a breed?????2+5: A Border Collie is a Species?????Generalization is transitive (is kind of)Classification is not transitive (is instance of)
A B และ B C :: A C
has_a
air
samsung
Is a
LG
Is a
ยานพาหนะ
has aรถยนต เรอ
เรอหางยาว
SpecializationSubclass/Child Class รบถายทอดคณสมบต (inherit) ทงAttributes และ Methods มาจาก Superclass/Parent Class
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : StringManager
Employee
Reusable การนากลบมาใชใหม
Generalization
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
เปน flip-side ของ Specialization
การท class ทตางกนม Attributes และ Methods ทเหมอนกน
IS - A
Employee
Manager
Class HierarchiesPoint
Circle Square
Cylinder Sphere Box Cubic
ลกศรใน Class Hierararchies แสดงความสมพนธของการรบ
ถายทอดคณสมบต
Cylinder extends Circle
Circle extends Point
IS-A Relationships การถายทอดคณสมบตเปนคณลกษณะแบบ transitive
Snake IS-A Reptile และ Reptile IS-A Animal
นนคอ Snake IS-A Animal
Animal
Reptile Mammal
Snake Lizard Dog Bat
Bird
Eagle
IS-A
IS-A
Adopting a Design การออกแบบ Class Hierarchies ทเหมาะสม เปนสงสาคญการ
การออกแบบระบบเชงวตถ
Alternatives Hierarchies ขนกบระบบทตองการออกแบบ
Animal
Mammal
Dog Bat
Bird
Eagle
หรอ
Flying Creature
BirdMosquito Bat
Eagle
The “Object”Class ในภาษาจาวา
คลาส “Object” เปนคลาสบนสดของ คลาสทงหมดทประกอบ
กนขนเปน Class Hierarchies
คลาสทประกาศ โดยไมใชคาเฉพาะ “extends” ระบการสบ
ทอดคณสมบตจากคลาสใดๆ ถอวาเปนการละ “extends Object”
ไว
public class Employee {
...
}
public class Employee extends Object {
...
}
เหมอนกบ
Methods defined in “Object”Class Methods ทถกกาหนดไวในคลาส “Object” ใหคลาสทรบสบทอด
คณสมบตสามารถเรยกใช หรอทาการ Overriding Methods เหลาน
ได ตวอยางเชน
equals(Object object) : Indicates whether some other
object is "equal to" this one.
toString() : Returns a string representation of the object
Clone() : Creates and returns a copy of this object.
...etc.
ดรายละเอยดใน Java & API Language Specification
Two Approaches to reuse Software Inheritance
IS-A relationship
Composition
HAS-A relationship
HAS-A
Customer BankAccount
Employee
Manager
IS-A
Benefits of Inheritance Avoiding redundancies
Source Code ในสวนทเหมอนกนจะถกเขยนขนเพยงครงเดยว
Code reuse & Code Sharing
สามารถนาไปใชใหมได (reuse) ไดโดยการรบถายทอด
คณสมบต
สามารถสรางคลาสใหมจากคลาสเดมทมอยแลวได
Reduced code size
Source Code ถกเขยนขนเพยงครงเดยว และถกนาไปใชใหม
ไดไมจากด
Costs of Inheritance Message-passing overhead
เนองจากอนญาตใหม Code Reuse ทาใหตองมการสงตอความ
ตองการไปยงคลาสอนๆ ทสมพนธกนดวยการถายทอดคณสมบต
Execution Speed
ความเรวในการประมวลผลชาลง เนองจากตองสงตอความ
ตองการไปขอรบบรการจากคลาสอนๆ
Program Complexity
เพมความซบซอนของโปรแกรม
Class diagrams (cont.)
• Associations• Generalization• Generalization และ
Classification• Aggregation
Aggregation• Aggregation
– เปนประเภทหนงของ association แตมคณสมบตบางอยางนอกเหนอจาก association แบบปกต
– เปนวตถทม วตถอนๆ เปนสวนองคประกอบ– เปน “part-of” relationship
Notation for aggregation
Class A
Class CClass B Class C
Class B
Class A
หรอ
Inner classes
Inner class / Aggregation
class A {class b{}class c{}class d{}
}
class car {class engine{}class wheels{}class glass{}
}
Example : Aggregation
Book
Front Chapter
Title Preface
End
Appendix Index
Aggregation*1
1 0..1
0..1
* 0..1
1
1
1
1 1 1 1
Example : Aggregation
Company Unit* Department*
Group*
Employee
*
works for
จากรปน
อธบายวาอะไร ?อธบายไดอยางไร ?
Aggregation• มได 2 รปแบบ Aggregation และ Composition• Aggregation
– วตถม วตถอนๆ เปนองคประกอบยอย– เชน Car ม engine และwheels เปนองคประกอบ
• Composition– วตถมวตถอนๆ เปนองคประกอบยอยทงหมด– การสง message ตดตอกบวตถทเปนองคประกอบรวม
อาจมผลกบองคประกอบทงหมด– ถา delete วตถทเปนองคประกอบรวม สวนยอยจะตองถก
delete ทงไปดวย
Aggregation & Composition• เปนความสมพนธแบบ transitive
– ถาวตถ A เปนสวนหนงของ B และ B เปนสวนหนงของ C แลว A เปนสวนหนงของ C ดวย
– เชน ถาทจบประตเปนสวนหนงของ ประต ประตเปนสวนหนงของรถยนต แลว ทจบประตเปนสวนหนงของรถยนต
• เปนความสมพนธแบบ Anti-symmetric– วตถใดๆ อาจไมจาเปนตองเปนสวนประกอบของตวเองทง
ทางตรงและทางออม– เชน ถาประตเปนสวนหนงของรถยนต แตรถยนตไม
จดเปนสวนหนงของประต
Aggregation and composition
Polygon
Point
GraphicsBundle
1 1
1
3..*
colortexture
Aggregation Composition
Association and Aggregation• Question : Association หรอ Aggregation?• Answer : เปน Aggregation
– ถาความสมพนธเปน “part of” – Operation ทเกดกบวตถหนงมผลกระทบกบอกวตถหนง
ในฐานะทเปนองคประกอบของอกวตถหนง
Generalization and Aggregation• Question : Generalization หรอ Aggregation ?• Answer : เปน Generalization
– ถาความสมพนธเปน “kind of”– ถาความสมพนธแบบ “or”
• Answer : เปน Aggregation– ถาความสมพนธเปน “part of” – ถาความสมพนธแบบ “and”
Class Diagram - Example• A zoo consists of a set of cages. • Every cage is the home of at least 2 animals. • Cages are located besides each other. • Every cage has at most one left neighbor and at most
one right neighbor. • Animals can be reptiles, insects, and mammals. • Mammals are elephants, monkeys, and tigers. • Monkeys eat bananas. • Tigers prefer meat.
Class DiagramRegistrationForm
Professor
CoursenamenumberCredits
open()addStudent(StudentInfo)
Studentnamemajor
CourseOffering
location
open()addStudent(StudentInfo)
nametenureStatus
ScheduleAlgorithm
RegistrationManager
addStudent(Course,StudentInfo)
Class Diagram: Notation Summary(stereotype)Classname
{abstract}
attribute: Type$class_attribute: Typeattribute: Type = Default_value
operation {abstract}operation (arg1:Type)operation (arg1:Type): Type$class_operation
CompositeClassnameattribute: Type
operation
SubClassname2attribute: Type
operation
Association_name
Role_name
ComponentClassnameattribute: Type
operation
discriminators
*1 {constraint}
Qualifier
signature
AssociationClassname
association_attrib: Type
SubClassname1attribute: Type
operation
<<actor>>customer
Class Diagram: Notation Summary (cont’d)
ParameterisedClassName
formal_arg
Instantiates
InstantiatedClassName<actual_arg>
ClassName1ClassName2
ClassName3{constraint}
Notes
inheritance
association
instantiation
ternary
+ public# protected- privatenull unspecified/ derived
CategoryName
SubCategory2SubCategory1dependency
CategoryName
ClassName: access
ClassName: access
CategoryName (Public Interface)
has by-reference
has by-value
Uitlityattribute: Type
operation(arg:Type): Type
• จบบทท 8 สาหรบ IT29/1 • 20-09-2557
• ครงหนาบทท 9 และสงงาน
Models
Three Perspectives• Conceptual (OOA)
– แสดงแนวคดเกยวกบขอบเขตของปญหา– ไมขนกบ implementation
• Specification (OOD)– โครงสรางทวไปของระบบ– กาหนด Interfaces ของ software
• Implementation (OOP)– โครงการของ implementation (classes)
Three Models• Analysis Model• Design Model• Implementation Model
Summary
• Structural Modeling• Class Diagrams and Object Diagrams• Association• Generalization และ Classification• Aggregation และ Composition• Models