ClassDiagram - WordPress.com

advertisement
8311302 การวิเคราะห์และออกแบบเชิงวัตถุ
Class Diagram
Department of Informatics
Faculty of Science and Technology
Phuket Rajabhat University
1/2553
OOAD
1
Outline
•
•
•
•
•
Structural Modeling
Class Diagrams and Object Diagrams
Association
Generalization และ Classification
Aggregation และ Composition
OOAD
2
Structural Modeling
OOAD
3
What is structural modeling?
• Structural model
– มุมมองของระบบที่เน้ นโครงสร้ างของวัตถุ รวมทัง้ คลาส
(Class) ของวัตถุ ความสัมพันธ์ ระหว่ างคลาส
(Relationship)
แอททริบวิ ต์ (Attribute) และ โอเปอ
เรชัน (operation)
OOAD
4
Structural Modeling: Core
Elements
Construct Description
Syntax
a description of a set of objects
that share the same attributes,
operations, methods, relationships
and semantics.
a named set of operations that
interface
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.
a run-time physical object that
node
represents a computational
resource.
class
OOAD
«interface»
5
Structural Modeling: Core Elements
(cont’d)
Construct Description
constraint¹
Syntax
a semantic condition or restriction.
{constraint}
¹ An extension mechanism useful for specifying structural elements.
OOAD
6
Structural Modeling: Core
Relationships
Construct
Description
Syntax
a relationship between two or more
classifiers that involves connections
among their instances.
A special form of association that
aggregation
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.
a relationship between two modeling
dependency
elements, in which a change to one
modeling element (the independent
element) will affect the other modeling
element (the dependent element).
association
OOAD
7
Construct
realization
Structural Modeling: Core
Relationships (cont’d)
Description
Syntax
a relationship between a specification
and its implementation.
OOAD
8
Structural Diagrams
• ประเภทของ Structural Diagrams
– static structural diagrams
• class diagram
• object diagram
– implementation diagrams
• component diagram
• deployment diagram
OOAD
9
Static Structural Diagrams
OOAD
10
Static Structural Diagrams
• แสดงกราฟของคลาสที่เชื่อมต่ อกันด้ วย static
relationships
• ประเภทของ Static Structural Diagrams
–class diagram : classifier view
–object instance diagram : instance view
OOAD
11
Class Diagrams
• Class diagrams
– แสดงรายละเอียดของ Class และความสัมพันธ์ ระหว่ าง
Class ในมุมมองแบบ logical view
• องค์ ประกอบของ UML class diagrams ได้ แก่
– Class, โครงสร้ างของ Class และพฤติกรรมของ Class
– ตัวบ่ งชี ้ Multiplicity และ navigation
– ชื่อของ Role
– ความสัมพันธ์ แบบ association, aggregation, dependency,
และ inheritance
OOAD
12
Classes in UML
• เขียนได้ 2 รู ปแบบ
Class name
Person
Attributes
attribute name : type
Operations
operation name(parameter : type)
: result type
OOAD
Person
- TaxIDNo : String
- Name : String
+ Income : double
+ TaxPaid : Boolean
+ calcTax()
+ calcTaxBal()
13
UML Syntax for Attributes
• visibility name : type
+ id : String
–visibility: public (+), protected (#), private (-)
• การตีความไม่ ขนึ ้ กับ ภาษาที่ใช้ เขียนโปรแกรม
–name : เป็ น string
–type : ประเภทของข้ อมูล ไม่ ขนึ ้ กับภาษา
OOAD
14
UML syntax for operations
• visibility name (parameter list) : return-type-expression
+ assignAgent (a : Agent) : Boolean
–visibility: public (+), protected (#), private (-)
• การตีความไม่ ขนึ ้ กับภาษาที่ใช้ เขียนโปรแกรม
–name : string
–parameter list : arguments
–return-type-expression : ประเภทของค่ าที่ return ไม่ ขนึ้ กับ
ภาษา
OOAD
15
Attributes – A Rule
• การกาหนด attributes ควรทาให้ ง่าย (simple)
• ค่ าที่เก็บอยู่ใน attributes ไม่ ควรเป็ นวัตถุ (Object)
– แต่ ควรสร้ างคลาส (class) ขึน้ และแสดงความสัมพันธ์
ระหว่ างวัตถุด้วย association
• ไม่ ควรสร้ างความสัมพันธ์ ระหว่ าง 2 คลาส โดยใช้
“foreign key” attribute
– แต่ ใช้ association
OOAD
16
Object Instance Diagrams
• class diagram
– แสดงชื่อ (name), โครงสร้ าง (structure) และ พฤติกรรม
(behaviour) ของประเภทของวัตถุ (object)
• object instance diagram
– แสดงวัตถุท่ สี ร้ างจากคลาส
• ความแตกต่ างระหว่ างทัง้ 2 diagram เปรียบเทียบได้ กับความ
แตกต่ างระหว่ างโครงสร้ างของตารางใน relational database
และค่ าที่ถูกเก็บอยู่ภายในตาราง
OOAD
17
Objects in UML
• เขียนได้ 2 รู ปแบบ
Object name
Tim:Person
Attributes
attribute name : type = value
Same Operations
for all instances of a class
OOAD
Tim : Person
TaxIDNo=“23003”
Name=“TimLim”
Income=“30000.00”
TaxPaid = FALSE
calcTax()
calcTaxBal()
18
An Object Instance Diagram
Sydney:City
London:City
Name=Sydney
Country=Australia
Population= 3,536,000
Name=London
Country=UK
Population= 2,324,320
New York :City
Name=New York
Country=USA
Population= 5,734,012
OOAD
19
Classes and Objects
City
Name : String = default
Country : String = default
Population : integer = default
setName (s : String = deault)
setPopulation(p : integer = default)
<<instanceOf>>
<<instanceOf>>
<<instanceOf>>
London : City
New York : City
Sydney : City
Name = London
Country = UK
Population =2,324,320
Name = New York
Country = USA
Name = Sydney
Country = Australia
Population =5,734,012
Population =3,536,000
OOAD
20
Class Diagrams
•
•
•
•
Associations
Generalization
Generalization และ Classification
Aggregation
OOAD
21
Association
• ความสัมพันธ์ (Relationship) ระหว่ าง instances ของ classes
association
name
name direction
Order
dateReceived
isPrepaid
number : String *
price : Money
Customer
issued by
1
name
address
creditRating( )
dispatch( )
Customer may make
several orders
Order comes from one
customer
OOAD
22
Associations
• คลาสมากกว่ า 1 คลาส สัมพันธ์ กันด้ วย “associations”
• Association
– เทียบได้ กับ ER relationship ที่เชื่อมโยงความสัมพันธ์
ระหว่ างคลาส และแสดง จุดเชื่อมโยง (“links”) ระหว่ าง
วัตถุท่ สี ร้ างขึน้ จากคลาส
OOAD
23
Naming associations
• การตัง้ ชื่อ association
– หลีกเลี่ยงชื่อที่ไม่ ส่ ือความหมาย
• associated_with, has, is_related_to
– มักขึน้ ต้ นด้ วยคากิริยา เช่ น
• works_for, owns , issued by
– มักตัง้ ชื่อในกรณีท่ มี อง Association จากซ้ ายไปขวา หรื อ จาก
บนลงล่ างของ diagram
OOAD
24
Associations on Class Diagrams
Person
Name
Address
Telephone
Car
Owns
OOAD
Manufacturer
Model
Registration
25
Links
• วัตถุแต่ ละวัตถุ สามารถเชื่อมโยงกันได้ โดยใช้ “links”
• A link
– เป็ น instance ของ association
• หมายเหตุ
– an association สามารถดารงอยู่ได้ ถึงแม้ ว่าจะไม่ มี instance
(links) ของ association ดังกล่ าว เช่ นเดียวกับ คลาสที่
สามารถดารงอยู่ได้ ถึงแม้ ว่าจะไม่ มีวัตถุใดๆ ถูกสร้ างขึน้
จากคลาสนัน้ ๆ
OOAD
26
Objects & Links
You:Person
Me:Person
Name=Jane
Address=22 Holly Pl
Telephone=62312198
Name=Dennis
Address=41 High St
Telephone=62661734
Owns
Owns
Owns
Ours:Car
Mine:Car
Manufacturer=Mazda
Model=626
Registration=YYX391
Manufacturer=Saab
Model=95
Registration=YRT833
OOAD
27
Association Multiplicity
• multiplicity
– หมายถึง การพิจารณาจานวน instances (objects) ของคลาส
หนึ่ง ที่สามารถเชื่อมโยงกับ instance (object) ของคลาสที่
เกี่ยวข้ อง
Order
dateReceived
isPrepaid
number : String
price : Money
1
*
Customer
name
address
creditRating( )
dispatch( )
OOAD
28
Association Multiplicity
1
1..*
Class
exactly one
Class
one or more
0..1
* Class
many (zero or more)
Class
zero or one/at most one
2 ..4
Class
as specified
OOAD
29
Example of Multiplicities
Student
*
*
University
1
Body
Heart
2-3
Bicycle
*
1..*
Wheel
1..*
Account
*
*
OOAD
Owner
30
Associations, Objects & Classes
• สาหรับ association ใดๆระหว่ าง 2 วัตถุ สามารถมี link
ได้ ตงั ้ แต่ 0 ถึง 1 link
• ระหว่ าง 2 วัตถุ สามารถได้ มีมากว่ า 1 association
(Multiple Assoications) แต่ association เหล่ านัน้ จะต้ อง
มีความแตกต่ างในด้ านความหมายของ association
• Multiple associations ระหว่ างคลาสกาหนดให้ ตัง้ ชื่อ
associations
OOAD
31
Roles
• role
– หมายถึง ชื่อที่กาหนดให้ กับด้ านปลายของ association ซึ่ง
ระบุวิธีการในการที่คลาสมีส่วนร่ วมใน association
• ปกติช่ ือของ role มักเกิดขึน้ เป็ นคู่ (เช่ นทัง้ สองด้ านของ
association จะมีช่ อื ของ role
• บังคับให้ กาหนดชื่อของ Role กับ associations แบบ
reflexive (Reflexive associations)
OOAD
32
Role names
• Role ระบุ ชื่อให้ กับด้ านปลายของ association
Company
Name
Address
Works for
*
employer employee
Person
Name
Insurance no.
Address
Role names
OOAD
33
Association Names & Roles
• association อาจมีช่ ือได้ เช่ นเดียวกับชื่อของ role ที่
กาหนดให้ กับด้ านปลายของความสัมพันธ์
–ถ้ ามีการตัง้ ชื่อ association แล้ วมักไม่ มีการตัง้ ชื่อ
role อีก
–ถ้ ามีการตัง้ ชื่อที่ปลายของ association (ได้ แก่ roles)
แล้ วมักไม่ มีการตัง้ ชื่อ association อีก
OOAD
34
Role names
• บังคับให้ กาหนดชื่อของ Role ที่เกิดขึน้ ระหว่ าง link ของ
วัตถุท่ สี ร้ างจากคลาสเดียวกัน
Person
Manager
Supervises
Name
Insurance no.
Address
Salesperson
OOAD
35
A Reflexive Association
• association ที่สร้ างจากคลาสเดียวกัน
Person
Name
Address
Telephone
Parent
2
Role name
* Child
OOAD
36
Ternary Associations
• A ternary association
– เป็ น assocation ที่ประกอบด้ วย 3 คลาส
• เช่ นเดียวกับแนวคิดของ ternary relationship ในER
modelling และแนวคิดคล้ ายๆ กัน
• นาเสนอโดยใช้ รูปสี่เหลี่ยขนมเปี ยกปูน เชื่อมโยงกับ
คลาสที่เกี่ยวข้ อง
OOAD
37
Ternary Associations
Vendor
Customer
Product
Sale
Ternary association
OOAD
38
Ternary Association Multiplicity
• multiplicity ของ ternary associations
• ซับซ้ อนกกว่ า binary และ unary associations
• มักเป็ น แบบ “many” สาหรับคลาสที่เกี่ยวข้ อง
(participating)
OOAD
39
N-ary Associations
• โดยทั่วไปแล้ ว association อาจเชื่อมโยงกับคลาส
จานวนเท่ าใดก็ได้
• ถ้ าเชื่อมโยงคลาสมากกว่ า 2 คลาสขึน้ ไป (รวมทัง้
ternary) เรียกว่ า “n-ary” associations
• แต่ Associations ที่เชื่อมโยงมากกว่ า 0 คลาสมักหาได้
ยาก
OOAD
40
Summary: Basic notation for
associations
Class A
mult1
Association name
role_B
mult2
Class B
role_A
• ตัวอย่ างเช่ น
Order
1
contains
OOAD
*
line items
Order Line
41
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
OOAD
42
Association Classes
Student
ID
Name
Address
Telephone
Enrolment
*
Subject
Code
* Name
Credit
Result
Mark
Association class
OOAD
43
Association Classes
• association class สามารถมีส่วนร่ วมใน associations
อื่นๆ เช่ นเดียวกับที่คลาสปกติสามารถมีได้
OOAD
44
Association Classes
Customer
Name
Address
Telephone
0..1
Claim
*
Purchase
Guarantee
Date
Length
* Conditions
OOAD
Product
* Number
Name
Price
Association class
45
Qualified Associations
• qualified association
–หมายถึง association ที่ถูกทาให้ ชัดเจนโดย attribute
ที่เรียกว่ า qualifier
–พิจารณา qualified association เช่ นเดียวกับแนวคิด
ของ weak entity type ใน ER
OOAD
46
Qualified Associations
Unqualified
Golf_Course
Name
1
Location
Hole
Number
* Length
Par
Qualified
Golf_Course
Name
Location
Hole
0..1 Length
HoleNumber
1
Par
OOAD
47
Qualified Association
Tournament
Prize
Year
Position
OOAD
0..1
48
Qualified association
Company Function
Organization
Person
*
ABC Inc.
ABC Inc.
ABC Inc.
ABC Inc.
ABC Inc.
XYZ Inc.
President
Vice President Finances
Member of board
Member of board
Member of board
President
OOAD
Roger Rabbit
Joe Savemoney
John Walker
Susi Sanssouci
Karl Eichbaum
Donald Duck
49
Class Diagrams: Hints
• class หนึ่งสามารถเป็ นส่ วนร่ วมได้ ในหลาย diagrams
• Diagrams ควรจะแสดงแง่ มุมเฉพาะด้ าน
–จานวนคลาสไม่ มากเกินไป
–จานวน associations ไม่ มากเกินไป
–ซ่ อน attributes และ operations ที่ไม่ จาเป็ นต้ อง
แสดง
• อาจต้ องมีการสร้ าง diagram ซา้ ๆ กันหลายรอบ
OOAD
50
Difference between attributes and
associations
• Conceptual perspective
–ไม่ แตกต่ างกันมากนัก
–Attributes เก็บค่ าเดียว (single-valued)
• Specification/implementation perspective
–Attribute เก็บค่ า (Value) ไม่ ใช่ ตัวอ้ างอิง
(References)
• ไม่ มีการ share ส่ วนของ attribute values ใน
ระหว่ าง instances ที่สร้ างจากคลาสเดียวกัน
• Often: Stores simple objects
OOAD
51
Class diagrams (cont.)
•
•
•
•
Associations
Generalization
Generalization และ Classification
Aggregation
OOAD
52
Generalization
• แนวคิดหลักเชิงวัตถุ
–Generalization จัดการ classes ให้ อยู่ในรูปของ
โครงสร้ างลาดับชัน้ (class Hierarchy) ขึน้ กับ
similarities และ differences
–เราเรียกคลาสที่อยู่ในระดับที่สูงกว่ าของ classes
hierarchy ว่ า “superclasses” และที่อยู่ในระดับต่ากว่ า
ว่ า “subclasses”
–เป็ น “kind-of” relationship
OOAD
53
Inheritance and Polymorphism
• “subclass” รับถ่ ายทอดคุณสมบัติ (inherits)อันได้ แก่
attributes, operations และassociations มาจาก
“superclass” ของตัวเอง
• แต่ อย่ างไรก็ตาม ถ้ า attribute หรือ operation ของ
“superclass” ถูกกาหนด่ ให้ เป็ นค่ าใหม่ ใน “subclass”
จะเป็ นการ “overrides” คานิยามที่กาหนดไว้ ใน
“superclass” - นาสู่แนวคิดของ polymorphism
OOAD
54
Superclasses and Subclasses
• “Superclass” มีคานิยาม attributes, operations และ
associations ร่ วมกันกับ “subclasses” ของคลาสนัน้ ๆ
• “Subclasses” มี attributes, operations และ associations
เฉพาะเป็ นของตัวเอง โดยเลือกที่จะกาหนดนิยามใหม่
ให้ กับ attribute, operation หรือ relationship ที่รับมา
จาก “superclass” หรือไม่ ก็ได้
OOAD
55
A Generalization Hierarchy
Superclass
Ellipse
Major_Axis
Minor_Axis
Geometric_Figure
X_Position
Y_Position
Circle
Radius
Rectangle
Width
Height
Subclasses
OOAD
56
Single & Multiple Inheritance
• generalizations มักเกิดใน class hierarchies ที่แต่ ละ
“subclass” มีเพียง 1 “superclass” - เรียกว่ า “single
inheritance”
• แต่ มีบางสถานการณ์ ท่ ี “subclass” อาจมีได้ มากกว่ า 1
“superclass” - เรียกว่ า “multiple inheritance”
OOAD
57
Multiple Inheritance
Employee
GeneralStaff ProfStaff
FullTime
Academic
OOAD
PartTime
Subclass with
two superclasses
58
Multiple Inheritance
Vehicle
Furniture
Wheelchair
OOAD
59
Specialization
• Specialization
– เป็ นกระบวนการย้ อนกลับของ generalization แต่ ให้ ผล
เช่ นเดิม แตกต่ างกันที่จุดเริ่มต้ นเท่ านัน้
• Generalization เป็ นการค้ นหาคุณลักษณะร่ วมกัน ของ
(sub)classes
• Specialization เป็ นการแยก และค้ นหาคุณลักษณะ
พิเศษของ (super)classes เพื่อให้ ได้ subclasses
OOAD
60
Class diagrams (cont.)
•
•
•
•
Associations
Generalization
Generalization และ Classification
Aggregation
OOAD
61
Generalization and Classification (=
is_a?)
1. Shep is a Border Collie.
2. A Border Collie is a Dog.
3. Dogs are Animals
4. A Border Collie is a Breed.
5. Dog is a Species
1+2: Shep is a Dog
1+2+3: Shep is a animal
1+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)
OOAD
62
Class diagrams (cont.)
•
•
•
•
Associations
Generalization
Generalization และ Classification
Aggregation
OOAD
63
Aggregation
• Aggregation
–เป็ นประเภทหนึ่งของ association แต่ มีคุณสมบัติ
บางอย่ างนอกเหนือจาก association แบบปกติ
–เป็ นวัตถุท่ มี ี วัตถุอ่ นื ๆ เป็ นส่ วนองค์ ประกอบ
–เป็ น “part-of” relationship
OOAD
64
Notation for aggregation
Class A
Class A
หรือ
Class B
Class B
Class C
Class C
OOAD
65
Example : Aggregation
1
Book
1
Aggregation
1
1
*
Chapter
Front
1
Title
1
0..1
End
1
1
0..1
Preface
*
Appendix
OOAD
1
0..1
Index
66
Example : Aggregation
Company
* Unit
* Department
*
Group
works for
*
Employee
OOAD
67
Aggregation
• มีได้ 2 รู ปแบบ Aggregation และ Composition
• Aggregation
– วัตถุมี วัตถุอ่ นื ๆ เป็ นองค์ ประกอบย่ อย
– เช่ น Car มี engine และwheels เป็ นองค์ ประกอบ
• Composition
– วัตถุมีวัตถุอ่ นื ๆ เป็ นองค์ ประกอบย่ อยทัง้ หมด
– การส่ ง message ติดต่ อกับวัตถุท่ เี ป็ นองค์ ประกอบรวม อาจ
มีผลกับองค์ ประกอบทัง้ หมด
– ถ้ า delete วัตถุท่ เี ป็ นองค์ ประกอบรวม ส่ วนย่ อยจะต้ องถูก
delete ทิง้ ไปด้ วย
OOAD
68
Aggregation & Composition
• เป็ นความสั มพันธ์ แบบ transitive
– ถ้ าวัตถุ A เป็ นส่ วนหนึ่งของ B และ B เป็ นส่ วนหนึ่งของ C แล้ว
A เป็ นส่ วนหนึ่งของ
– เช่ น ถ้ าทีจ่ ับประตูเป็ นส่ วนหนึ่งของ ประตู ประตูเป็ นส่ วนหนึ่ง
ของรถยนต์ แล้ว ที่จับประตูเป็ นส่ วนหนึ่งของรถยนต์
• เป็ นความสั มพันธ์ แบบ Anti-symmetric
– วัตถุใดๆ อาจไม่ จาเป็ นต้ องเป็ นส่ วนประกอบของตัวเองทัง้
ทางตรงและทางอ้ อม
– เช่ น ถ้ าประตูเป็ นส่ วนหนึ่งของรถยนต์ แต่ รถยนต์ ไม่ จัดเป็ นส่ วน
หนึ่งของประตู
OOAD
69
Aggregation and composition
Polygon
Aggregation
1
1
Composition
1
Graphics
Bundle
3..*
color
texture
Point
OOAD
70
Association and Aggregation
• Question : Association หรือ Aggregation?
• Answer : เป็ น Aggregation
–ถ้ าความสัมพันธ์ เป็ น “part of”
–Operation ที่เกิดกับวัตถุหนึ่งมีผลกระทบกับอีกวัตถุ
หนึ่งในฐานะที่เป็ นองค์ ประกอบของอีกวัตถุหนึ่ง
OOAD
71
Generalization and Aggregation
• Question : Generalization หรือ Aggregation ?
• Answer : เป็ น Generalization
–ถ้ าความสัมพันธ์ เป็ น “kind of”
–ถ้ าความสัมพันธ์ แบบ “or”
• Answer : เป็ น Aggregation
–ถ้ าความสัมพันธ์ เป็ น “part of”
–ถ้ าความสัมพันธ์ แบบ “and”
OOAD
72
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.
OOAD
73
Class Diagram
ScheduleAlgorithm
RegistrationForm
RegistrationManager
addStudent(Course,StudentInfo)
name
numberCredits
open()
addStudent(StudentInfo)
Student
name
major
Professor
name
tenureStatus
Course
CourseOffering
location
open()
addStudent(StudentInfo)
OOAD
74
Class Diagram: Notation Summary
(stereotype)
Classname
{abstract}
signature
attribute: Type
$class_attribute: Type
attribute: Type = Default_value
CompositeClassname
Association_name
1
{constraint}
operation {abstract}
operation (arg1:Type)
operation (arg1:Type): Type
$class_operation
discriminators
*
attribute: Type
Role_name operation
Qualifier
Association
Classname
association_attrib: Type
ComponentClassname
attribute: Type
SubClassname1
SubClassname2
attribute: Type
attribute: Type
operation
operation
OOAD
operation
75
Class Diagram: Notation Summary
(cont’d)
formal_arg
Parameterised
Instantiated
ClassName
Instantiates ClassName<actual_arg>
ClassName1{constraint}
Uitlity
attribute: Type
operation(arg:Type): Type
ClassName2
ClassName3
CategoryName
CategoryName
inheritance
association
instantiation
has by-value
+
#
null
/
public
protected
private
unspecified
derived
has by-reference
ternary
SubCategory1
dependency
ClassName: access
SubCategory2
CategoryName
(Public Interface)
ClassName: access
Notes
OOAD
76
Download