Software Engineering Dr. K. T. Tsang
description
Transcript of Software Engineering Dr. K. T. Tsang
![Page 1: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/1.jpg)
11
Software EngineeringDr. K. T. Tsang
Lecture 7Advanced class modeling
http://www.uic.edu.hk/~kentsang/SWE/SWE.htm
![Page 2: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/2.jpg)
2
Multiplicity • Multiplicity - a constraint on cardinality of a set• Multiplicity also applies to attributes• Multiplicity for an attribute specifies the number
of possible values for each instantiation of an attribute
Person
name: string [1]birthday: date [1]phoneNumber: string [*]address: string [1..*]
![Page 3: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/3.jpg)
3
Scope & visibility of features• Scope – whether a feature applies to an
object (object scope) or an entire class (static, class scope)
• Visibility – public(+), protected(#) or private(-)
![Page 4: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/4.jpg)
4
Scope – example 1
PhoneMessage
maxDurationmaxDaysRetaineddateRecordtimeRecordedprioritymessagehasBeenReceived
PhoneMailbox
maxMsgCountphoneNumberpassWordgreeting
Person name
owner
owner
source
*
*
*
0..1
1
1
Not a good model
{ordered}
![Page 5: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/5.jpg)
5
Scope – example 2
PhoneMessage
dateRecordtimeRecordedprioritymessagehasBeenReceived
PhoneMailbox
phoneNumberpassWordgreeting
MailCategory
categoryNamemsgMaxDurationmsgDaysRetainedmsgMaxCount
Person name
*
*
0..1
1owner
source
{ordered}
1
*
owner
*
1
Preferred model
![Page 6: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/6.jpg)
6
N-ary Associations• Binary Associations – 2 ends• Ternary Associations – 3 ends• N-ary Associations – n ends, avoid them• Most N-ary Associations can be decomposed
into binary associations
![Page 7: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/7.jpg)
7
Restating an N-ary Association• Decompose non-atomic ternary into binary associations
Company
name* *Person
name
Purchase
quantitydatecost
11
stock
stock Company
name
**
Person
name
Purchase
quantitydatecost
*
![Page 8: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/8.jpg)
8
Example - A genuine ternary associationp.65 B&R, Fig.4.6
Language name
**
Project name
Person name
*programmer
An atomic n-ary association is one that cannot be subdivided into binary associations without losing information.
Class diagram
![Page 9: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/9.jpg)
9object diagram
Java:Language name=“Java”
P135:Project name=“P135”
Mary:Person name=“Mary”
C:Language name=“C”
library:Project name=“library”
Example - A genuine ternary association..
![Page 10: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/10.jpg)
10
Example 2 - A ternary association
TextBook
Semester
Course
Professor
roomNumber
DeliveredCourse **
* *
*
A class associates with an association?
p.65 B&R, Fig. 4.7
![Page 11: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/11.jpg)
11
Semester
Course
Professor
roomNumber
DeliveredCourse **
*
TextBook *
Example 2 – An n-ary association
A better way??
![Page 12: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/12.jpg)
12
Promoting an n-ary association to classes
Semester
Course
Professor
roomNumber
DeliveredCourse *
*
*
TextBook *
A regular class
Programming languages cannot express n-ary association, so we must promote them to class.
![Page 13: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/13.jpg)
13
Aggregation• Aggregation is a special form of association
in which an aggregate object is made of constituent parts (is a part of).
• An aggregate object is an extended object treated as a unit in many operation.
• Properties of Aggregation – Transitive: if A is part of B and B is part of C,
then A is also part of C– Anti-symmetric: if A is part of B then B is not part
of A
![Page 14: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/14.jpg)
14
Aggregation - example
Auto-mobile
Engine Body Wheel Seat 1 1
** *
*
*
*
UML symbol-Hollow diamond
![Page 15: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/15.jpg)
15
• There are 2 forms of part-whole relationships in UML.
• The general form is Aggregation.• The more restricted form is Composition:
– a part can belong to at most to one assembly– once assigned, the part has the same lifetime as
the assembly• Deletion of the assembly object triggers the
deletion of all objects in the composition.
Aggregation & Composition
![Page 16: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/16.jpg)
16
Composition - example
Company Division Department
Person*
**1
1
1
UML symbol- solid diamond
![Page 17: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/17.jpg)
17
Propagation of operations• The automatic application of an operation to
a group of objects when the operation is applied to a starting object.
• Examples – – Moving an aggregate moves its parts– Copying a document copies all its paragraphs
and all the characters in a paragraph
![Page 18: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/18.jpg)
18
Paragraph
copy
Document
copy
Character
copy
*1 *1
Propagation of operations
Person 1
*
The copy operation propagates from document to paragraphs to characters, but not in the reverse direction.
owns
copy copy
p.68 B&R, Fig.4.11
![Page 19: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/19.jpg)
19
Abstract & concrete classes• An abstract class has no direct instance but its
descendant classes have direct instances.• A concrete class is a class that can have direct
instances (instantiable).• Abstract class can have abstract operation,
designated by italics or the keyword {abstract}.• An abstract operation defines the signature of an
operation for which each concrete subclass may provide its own implementation.
• A concrete class may not contain any abstract operation.
![Page 20: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/20.jpg)
20
Abstract operation - example
Employee yearTodateEarnings
computePay
FullTimeEmployee weeklyRate
computePay
PartTimeEmployee hourlyRate
computePay
Note: Avoid concrete super-class
![Page 21: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/21.jpg)
21
Multiple inheritance
• Allows a class to have more than one superclass, and inherit features from all parents.
• Some OOP languages does not support multiple inheritance, e.g. Java.
• Multiple inheritance may create ambiguities. Be careful with its usage.
![Page 22: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/22.jpg)
22
Multiple inheritance - example
Employee yearTodateEarnings
computePay
FullTimeEmployee weeklyRate
computePay
PartTimeEmployee hourlyRate
computePay
Partner ownership
computePay
FullTimeEmployeePartner
![Page 23: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/23.jpg)
23
• An object is an instance of all ancestors of its class.
Multiple classification
![Page 24: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/24.jpg)
24
Multiple classification - example
UniversityMemberPerson
Faculty StudentStaff
*1
Instructor
{overlapping}
Overlapping means a person may belong to more than one kind of UniversityMember
![Page 25: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/25.jpg)
25
Delegation using composition of parts
Worker Management
Employee
Manager DirectorFullTime PartTime
1
1 1
1
employmentStatus managerialStatus
p.74 B&R, Fig. 4.19
Workaround for Multiple inheritance: recast a super-class with multiple inheritance as a composition in which each part replaces a subclass (generalization).
![Page 26: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/26.jpg)
26
Workaround 2 – nested generalization
Employee
FullTime PartTime
FullTimeManager FullTimeWorker PartTimeManager PartTimeWorker
![Page 27: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/27.jpg)
27
Metadata• Metadata is data that describes other data.• Example: A class definition is metadata.• Class is meta-object. Class that describes other
class is meta-class.
CarModel
modelNameyearbasePrice
PhysicalCar
serialNumbercoloroptions
Company Person
*
1
*
1manufacturer owner
*1describes
![Page 28: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/28.jpg)
28
Reification• Reification is the promotion of something
that is not an object to an object.
Substance
substanceNameReification:
Promote attribute to a class
Substance SubstanceName
substanceName* 1..*
alias
![Page 29: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/29.jpg)
29
Constraints on objects
Employee
salary
boss0..1
*(salary<boss.salary)
Windowlengthwidth
(0.8<length/width<1.5)
Job
priority
(priority never increases)
![Page 30: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/30.jpg)
30
Constraint on generalization set (subclasses)
• Disjoint – subclasses are mutually exclusive
• Overlapping – an object can belong to more than one subclass
• Complete – the generalization lists all possible subclasses
• Incomplete – the generalization may miss some subclasses
![Page 31: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/31.jpg)
31
• Multiplicity for an association restricts the number of objects related to a given object.
• An ordinary association has no presumed order on the objects of the “many” end. The constraint {ordered} indicates that objects of the “many” end have an order that must be preserved.
Constraint on links
![Page 32: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/32.jpg)
32
Subset constraint between associations
Person Committee
Member of
Chair of
(subset)* *
*1
The chair of a committee must be a member of the committee.
![Page 33: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/33.jpg)
33
Derived data• Classes, attributes and associations may be
derived from others.• The notation for a derived element is a slash (/)
in front of the element name.• The constraint that determines the derivation
should also be shown.
Personbirthdate/ age
{age=currentDate – birthdate}
CurrentDate
Derived attribute
![Page 34: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/34.jpg)
34
Derived object & association
Machine Assembly Part*1*1
offset offset
/ Offset
1
1
offset = MachineAssembly.offset XPartAssembly.offset
/ NetOffset
Derived data can complicate implementation. Use them only if they are truly necessary.
![Page 35: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/35.jpg)
35
Package• A package is a group of elements (classes,
associations, generalizations and packages of smaller sizes) with a common theme or purpose.
• Packages partition a model, making it easier to understand or manage.
• Large applications may have several tiers of packages.
PackageName
Notation of a package
![Page 36: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/36.jpg)
36
Reading for this lecture
• Chapter 4 Blaha & Rumbaugh
![Page 37: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/37.jpg)
3737
Software EngineeringDr. K. T. Tsang
Lecture 8State modeling
http://www.uic.edu.hk/~kentsang/SWE/SWE.htm
![Page 38: Software Engineering Dr. K. T. Tsang](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815b15550346895dc8c40a/html5/thumbnails/38.jpg)
38
Reading for this lecture
• Chapter 5 Blaha & Rumbaugh