Lecture 02: Class diagram Doan Trung Tung, PhD OO AD
OBJECT-ORIENTED ANALYSIS AND DESIGN
1
OO AD
PLAN
OBJECT-ORIENTED ANALYSIS AND DESIGN
Class diagram Relationships From requirements to Class diagram
2
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
What is a Class Diagram? A class diagram describes the types of objects in the system and the various kinds of relationships that exist among them. A graphical representation of a static view on static elements. Richest notation in UML
3
OO AD
Objects
OBJECT-ORIENTED ANALYSIS AND DESIGN
Objects have three responsibilities:
o What they know about themselves – (e.g., Attributes)
o What they do – (e.g., Operations) o What they know about other objects – (e.g., Relationships)
4
OO AD
Class
OBJECT-ORIENTED ANALYSIS AND DESIGN
A class is a template (specification, blueprint) for a collection of objects that share a common set of attributes and operations. MovieClubMember
Class
attributes operations
Objects
5
OO AD
Class notation in class diagram
OBJECT-ORIENTED ANALYSIS AND DESIGN
Class name
private Attributes
protected public
Methods
6
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Generalization relationship A generalization connects a subclass to its superclass. It denotes an inheritance of attributes and behaviors from the superclass to the subclass and indicates a specialization in the subclass of the more general superclass. Can be viewed as “is a” relationship
Student
Person
7
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Generalization relationship UML permits a class to inherit from multiple superclasses, although some programming languages (e.g., Java) do not permit multiple inheritance.
Student
Employee
TeachingAssistant 8
OO AD
Generalization relationship
OBJECT-ORIENTED ANALYSIS AND DESIGN
Abstract class Shape draw()
Circle
Rectangle
draw()
draw() 9
OO AD
Generalization relationship
OBJECT-ORIENTED ANALYSIS AND DESIGN
Discriminator Discriminator Doctor Female role
Person Male
Gender {complete}
Nurse
patient Physicaltherapist Patient 10
OO AD
Associations relationship
OBJECT-ORIENTED ANALYSIS AND DESIGN
Relationships between instances (objects) Association describes a link, a link being a connection among objects between classes.
Can be viewed as “has a” relationship
11
OO AD
Associations relationship
OBJECT-ORIENTED ANALYSIS AND DESIGN
Properties o Name o Role › The specific role of the association
o Multiplicity › Indicates the number of objects that are connected
o Type › Plain association, aggregation, composition
o Direction
12
OO AD
Associations
OBJECT-ORIENTED ANALYSIS AND DESIGN
Example: A Person works for a Company Role
employee
employer
Person
Company works for Association Name
13
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Associations Multiplicity: specify properties of the number of links that can exist between instances (objects) of the associated classes. Association name
Class1
multiplicity
multiplicity
Class2
Multiplicity notation o* o5 o 5..8 o 5..*
0, 1, or more 5 exactly between 5 and 8, inclusive 5 or more 14
OO AD
Associations
OBJECT-ORIENTED ANALYSIS AND DESIGN
Multiplicity example o A Student can take up to 5 Courses. o Student has to be enrolled in at least one course. o Up to 300 students can enroll in a course. o A class should have at least 10 students.
Student 10..300
takes
1..5
Course
15
OO AD
Associations
OBJECT-ORIENTED ANALYSIS AND DESIGN
Multiplicity example o A teacher teaches 1 to 3 courses (subjects) o Each course is taught by only one teacher. o A student can take between 1 to 5 courses. o A course can have 10 to 300 students.
Teacher
1
teaches
1..3
Course
1..5
takes
Students 10..300
16
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Self association An association that connects a class to itself is called a self association.
1
Employee
manager Responsible for 0..10
worker
17
OO AD
Multi associations
OBJECT-ORIENTED ANALYSIS AND DESIGN
A cricket team has 11 players. One of them is the captain. A player can play only for one Team. The captain leads the team members. Team Member
Captain
Player
1
0..1
Team
10
11 1
member of
1
Captain
Leads 18
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Aggregation A specialized form of Association in which a whole is related to its part(s) Can be viewed as a “part of” relationship o Whole – parts o Container – contents o Group - members
Can they be association?
Engine Car Transmission 19
OO AD
Composition
OBJECT-ORIENTED ANALYSIS AND DESIGN
Special Aggregation
Can they be aggregation?
Strong ownership
Scrollbar
Window
Titlebar
Menu
20
OO AD
How to identify aggregation / composition?
OBJECT-ORIENTED ANALYSIS AND DESIGN
Lifetime of part is bound within lifetime of composite o There is a create-delete dependency
There is an obvious whole-part physical or logical assembly Some properties of composite propagate to parts (e.g., location)
Operations applied to composite propagate to parts (e.g., destruction, movement, recording)
21
OO AD
Why aggregation / composition?
OBJECT-ORIENTED ANALYSIS AND DESIGN
Assists in identification of a creator Operations applied to whole should usually propagate to parts
Identifying whole - parts supports encapsulation
22
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Dependency Change in specification of one class can change the other class. This can happen when one class is using another class. Always directed Caused by class methods o local variable, parameter, return value
Circle Point Move(p:Point) 23
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Dependency Dependency relationship can be used to show relationships between classes and objects
circleA:Circle Circle
circleB:Circle 24
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Realization (implementation) A realization relationship indicates that one class implements a behavior specified by another class (an interface or protocol). An interface can be realized by many classes. A class may realize many interfaces.
25
OO AD
Realization
OBJECT-ORIENTED ANALYSIS AND DESIGN
Example Keyboard brandName numOfKeys
<> TypeWriter
ctl() pageDown()
keyStroke()
26
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Constraint rules and Notes Constraints and notes annotate among other things associations, attributes, operations and classes. Constraints are semantic restrictions noted as Boolean expressions.
Customer id: long { value > 0 }
1
* { total < $50 }
Constraint
Order
may be canceled
Note 27
OO AD
Common mistakes / confusions
OBJECT-ORIENTED ANALYSIS AND DESIGN
When drawing Class Diagram o Draw classes immediately o Hard to differentiate relationships › Associations or › Aggregation or › Composition?
o Attributes or classes? o Inheritances or Interfaces? o Draw all dependencies? o…
28
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
From user requirements to Class Diagram “A class diagram describes the types of objects in the system and the various kinds of static relationships that exist among them” Make a list of objects found in user requirements Group similar objects into class Define relationships between each two classes o Multiplicity o Type (generalization, association, realization, dependency) o Name, direction, … 29
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … Zounds international (Audiovisual sales) Has A number of (sub) divisions such as Purchasing, NetSales, Stock, Retail etc. ‘Retail’ has outlets (e.g. in Malls and High street stores). NetSales has both ‘Hard’ (CDs, DVDs…) and ‘soft’ (downloads) sales. Retail and Netsales co-operate to produce ‘Personal mixes’ that they burn onto CDs to produce items for individual customers. Customers may open an account to facilitate the payment for multiple transactions or they may just buy goods in individual purchases paying by cash, cheque or card. The staff selling the good are recorded. 30
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … Zounds international (Audiovisual sales) Has A number of (sub) divisions such as Purchasing, NetSales, Stock, Retail etc. ‘Retail’ has outlets (e.g. in Malls and High street stores). NetSales has both ‘Hard’ (CDs, DVDs…) and ‘soft’ (downloads) sales. Retail and Netsales co-operate to produce ‘Personal mixes’ that they burn onto CDs to produce items for individual customers. Customers may open an account to facilitate the payment for multiple transactions or they may just buy goods in individual purchases paying by cash, cheque or card. The staff selling the good are recorded. 31
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … A sub-division may also be a parent division e.g. Zounds UK is a sub-division of Zounds international but also a parent of Zounds Sweet (specialising in musical confectionary). ZI has no parent division. Retail staff display and sell traditional stock items (CD, DVD) in the retail outlets to customers (these people may also be staff). The netSales staff maintain and sell copies of ‘softstock’ (digitally encoded mediaware that they are licensed to distribute) direct to customers (or staff). The company’s retail outlets purchase ‘SoftStock’ for the mixes. 32
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … A sub-division may also be a parent division e.g. Zounds UK is a sub-division of Zounds international but also a parent of Zounds Sweet (specialising in musical confectionary). ZI has no parent division. Retail staff display and sell traditional stock items (CD, DVD) in the retail outlets to customers (these people may also be staff). The netSales staff maintain and sell copies of ‘softstock’ (digitally encoded mediaware that they are licensed to distribute) direct to customers (or staff). The company’s retail outlets purchase ‘SoftStock’ for the mixes. 33
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Objects • Divisions • Subdivisions • Retails • Netsales • Purchasings • Stocks • Transactions • Purchases
• Hard sales • Soft sales • CDs • Mixes • Items • Goods • DVD
• Customers • Staff • Retail staff • Netsale staff
• Accounts
• Cheque • Cash • Card 34
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Classes
35
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … The divisions and subdivisions each have a single manager and may have staff. Staff are employed by Zounds international and are assigned to exactly one division. There are many types of staff and their assignment/role (Cleaner, NetSales, Manager) may vary during their time with ZI. All subdivisions have only one parent division but parent divisions may have many subdivisions. Theoretically all the parent divisions have control over all their subdivisions, though in practice this is seldom enforced for the mundane ‘day-to-day’ running of the company. All Sub-divisions are cost centres, maintaining their own accounts but constrained to follow a centralised accounting model with a standard way of maintaining the balance and transaction history and recording transactions. 36
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
User requirements … The divisions and subdivisions each have a single manager and may have staff. Staff are employed by Zounds international and are assigned to exactly one division. There are many types of staff and their assignment/role (Cleaner, NetSales, Manager) may vary during their time with ZI. All subdivisions have only one parent division but parent divisions may have many subdivisions. Theoretically all the parent divisions have control over all their subdivisions, though in practice this is seldom enforced for the mundane ‘day-to-day’ running of the company. All Sub-divisions are cost centres, maintaining their own accounts but constrained to follow a centralised accounting model with a standard way of maintaining the balance and transaction history and recording transactions. 37
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Classes and relationships
Do you agree with type of associations?
38
OO AD
Hidden objects/classes
OBJECT-ORIENTED ANALYSIS AND DESIGN
Web pages Windows
Remember class UITool?
Dialogs
“Data Access” Classes …
39
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Exercise The ABC bookstore offers a Web system that allows its customers to search, reserve, buy and rent books online. Any customer can visit the bookstore’s official Web site to search for book details, but only the registered customers (hereinafter, members) can reserve, buy and rent books online. The required information for a membership is the customer’s detail information such as name, address, a unique social ID, phone number, e-mail address, and credit card number. Once the registration is completed, the system will send the confirmation notification to the customer’s e-mail address. The ABC bookstore offers services for both hard-copied books and electronic books. The rental service is applied for hard-copied books only. For hard-copied books, a customer can search for book information and reserve the books that he/she wants to buy or rent. The reserved time is 1 day. After the customer made the reservation, he/she must go to the bookstore to pick up the books within the reserved time. Otherwise, the reserved books will be released to other customers. To pick up the books, the customer must present the reservation number to the bookstore’s staff and pay for the books.
40
OBJECT-ORIENTED ANALYSIS AND DESIGN
OO AD
Exercise The rental period is 7 days for each book. To return the books, the customer can either go to the bookstore during its office hours (8:00 to 22:00) to return them to the staff at the counter service, or use an automated self-service book return box that is located in front of the bookstore and is available for 24 hours. The book return box has a barcode reader attached to it. The customer has to use the bar code reader to scan a barcode of each book and then put the book into the book return box. If the customer returns the books later than the return date, he/she will be charged at $10 per day per book via his/her credit card. The late-returned fee will be shown in his/her membership account. In case of buying books online (both hard-copied books and electronic books), the customer must make payment using his/her credit card and also provide a shipping address for the books delivery. Draw class diagram (use CRC first). Make your own assumptions if needed 41