lecture-06a-class-diagram - SW
lecture-06a-class-diagram - SW
Lecture 6
Software Design
4
Types of Classes
– Entity class a.k.a domain class
• A class that exists in the problem domain. E.g student, course,…
(usually remembered)
• These are the most important classes.
– Control class
• a class that mediates between boundary classes and entity classes,
acting as a switchboard between the view layer and domain layer
• Consider this to be an eventHandler – takes an event from boundary
class and calls the Entity class
5
CRC – How To?
– Create a CRC for the entity object will receive the first message
from the controller (primary object)
• For example, a Customer object for new sale.
• Questions to ask:
– What are its responsibilities
– What information does the class need to do its responsibilities
– Create CRC for other classes that must collaborate with the
primary object class to complete the use case.
• Questions to ask:
– What are its responsibilities
– What information does the class need to do its responsibilities
6
Example – CRC Cards Results
Several Use Cases
7
Case Study
• Use Case Diagram for Student Assessment Management
System Grade system
Record
grades
Student
View grades
Teacher Distribute
Report cards
Create report
cards Printing administrator
8
Noun analysis From
Requirements
• A University record system should keep information about its
students and academic staff.
ClassName
Circle
13
Classes (with attributes and operations)
ClassName
ClassName
attr1:type1
attr1:type1 attr2:type2=“def”
attr2:type2=“def” operation1()
operation2(args)
operation3() : ret type
Circle
Circle centreX:Int
centreX:Int centreY:Int=0
centreY:Int=0 draw()
move(Int X, Int Y)
14
Classes (with attributes and operations)
• Attribute
– is a property of a class (E.g centreX, centreY,…).
– A class may have zero or more attributes.
– A one-word attribute name is written in lowercase letter.
– If the name consists of more than one word each word other than
the first word begins with an uppercase letter.
– The list of attribute names begins below a line separating them
from the class name.
• Operations
– An operation is something that a class Circle
can do, or that you (or another class) centreX:Int
can do to a class.
– Naming – similar to attribute centreY:Int=0
– The list of operations begins below a draw(): void
line separating operations from the move(Int X, Int Y): void
attributes.
15
Class Visibility
• Visibility
– specifies the extent to which other classes can use a given class's
attributes or operations.
– applies to attributes or operations
– Three levels of visibility are possible
• public level - usability extends to other classes +
• protected level - usability is open only to classes that inherit from
original class #
• private level - only the original class
can use the attribute or operation - Circle
- centreX:Int
- centreY:Int=0
+ draw()
# move(Int X, Int Y)16
Class Stereotypes in UML
• stereotype a way
of categorizing a
model element by
its characteristics,
indicated by
<< >>
17
Class - Implementation
public class Circle {
18
Class Multiplicity
• A multiplicity in a class specifies the number of
instances (objects) of that class that can exist
simultaneously.
1
Library
19
Objects: Instances of Classes
(a) (b)
(c) (d)
20
Class Relationships
• Classes can related to each other through different
relationships:
– Association (delegation)
– Generalization (inheritance)
– Realization (interfaces)
– Dependency
21
1. Association
• Association describes a link, a link being a connection among
objects between classes.
• Association is shown by a solid line between classes.
Eg. A Person works for a Company.
Role
employee employer
Person Company
works for
Association Name
22
Association - Properties
• Name
– Name of the association
• Role
– The specific role of the association
• Multiplicity
– Indicates the number of objects that are connected
• Type
– Plain association, aggregation, composition
• Direction
– Direction can also be shown for a association
23
Association - Multiplicity
• Multiplicity on association specify properties of the
number of links that can exist between instances
(objects) of the associated classes.
– That is, it indicates how many objects of one class relate to
one object of another class. It is indicated by a single
number or a range of numbers.
• We can add multiplicity on either end of class
relationship by simply indicating it next to the class
where the relationship enters.
Association name
Class1 Class2
multiplicity multiplicity
24
Association - Multiplicity
• A Student can take up to five Courses.
• Student has to be enrolled in at least one course.
• Up to 300 students can enroll in a course.
• A class should have at least 10 students.
Student Course
10..300 takes 1..5
25
Association
Association - Multiplicity
– Multiplicity
• A teacher teaches 1 to 3 courses (subjects)
• Each course is taught by only one teacher.
• A student can take between 1 to 5 courses.
• A course can have 10 to 300 students.
1 teaches 1..3
Teacher Course
1..5
takes
Student
10..300
26
• Multiplicity can be expressed as,
• Exactly one - 1
• Zero or one - 0..1
• Many - 0..* or *
• One or more - 1..*
• Exact Number - e.g. 3..4 or 6
• Or a complex relationship – e.g. 0..1, 3..4, 6..* would mean
any number of objects other than 2 or 5
Self Association: An association that connects a class to itself
is called a self association.
27
Association - Self
A Company has Employees.
A single manager is responsible for up to 10 workers.
1
Employee
manager
Responsible worker
0..10
for
28
Association types Collage
1
Unidirectional/not vice versa Teacher
clear white diamond
composition: "is entirely made Book
SubClass1 SubClass2
30
Inheritance - Implementation
31
3. Realization- Interface
• The relationship between a class and an interface is
called realization.
Interface class doesn’t have
attributes, only method
signatures
Classes can implement
the methods
Relationship Represented
by a dashed, white arrow
32
Dependency
Change in specification of one class can change
the other class.
This can happen when one class is using
another class.
E.g If you change a point from a 2D point to a 3D point,
you also need to change the move method to draw a 3D
movement
Circle
Point
Move(p:Point)
33
Dependency cont
Dependency relationship can be used to show
relationships between classes and objects.
f >>
nc eO circleA:Circle
ta
ins
<<
Circle
<<
i
ns
tan
ce
Of circleB:Circle
>>
34
Class Diagrams
The UML class diagram consists of
several Classes, connected with
Relationships.
Must include Entity classes
Sometimes controller is included
35
Class Diagram - Example
Draw a class diagram for a information modeling
system for a school.
School has one or more Departments.
Department offers one or more Subjects.
A particular subject will be offered by only one
department.
Department has instructors and instructors can work for
one or more departments.
Student can enrol in upto 5 subjects in a School.
Instructors can teach upto 3 subjects.
The same subject can be taught by different instructors.
Students can be enrolled in more than one school.
36
Class Diagram - Example
School has one or more Departments.
School Department
1 has 1..*
Department Subject
1 offers 1..*
37
Class Diagram - Example
Department has Instructors and instructors can
work for one or more departments.
Instructor Department
1..* assigned to 1..*
Student Subject
* takes 0..5
38
Class Diagram - Example
Instructors can teach up to 3 subjects.
The same subject can be taught by different
instructors.
1..*
Instructor Subjects
teaches 1..3
39
Class Diagram - Example
Students can be enrolled in more than one
school.
*
Student School
member 1..*
40
Class Diagram Example
has
School Department
1 1..* 1..*
1…* 1
offeres
assignedTo
member
* 1..* 1..*
41
Object Diagram
Object Diagram shows the relationship
between objects.
42
Object Diagram - Example
c1: Company
c1: Company
name=“UniMelb”
name=“Sales” name=”CSSE”
manager employee
name=“Rao” name=“Raj”
43
Tasks to do this week
Revision on oo concepts.
Identify your classes and objects in your
project.
44