Session 7
Session 7
AND PROGRAMMING
Session 7
8/24/2022 2
What is UML and Why we use UML?
▪ More description about UML:
▪ It is a industry-standard graphical language for specifying, visualizing,
constructing, and documenting the artifacts of software systems
▪ The UML uses mostly graphical notations to express the OO analysis and
design of software projects.
• When any business analyst utilizes UML diagrams, it makes his/her analysis
tasks much easier and leads to very transparent communication with the
developers.
• Communication(collaboration)
• Sequence
• Interaction overview
• Timing
UML Diagram Examples
USE CASE DIAGRAM
CLASS DIAGRAM
STATE DIAGRAM
ACTIVITY DIAGRAM
SEQUENCE DIAGRAM
COLLABORATION DIAGRAM
COMPONENT DIAGRAM
DEPLOYMENT DIAGRAM
UML Class Diagram
UML Class diagram :
• The UML Class diagram is a graphical notation used to construct and
visualize object oriented systems.
• A class diagram describes the structure of a system such as Classes and
their attributes , operations (or methods) and the relationships among
objects.
• A class diagram is used to show the existence of classes and their
relationships in the logical view of a system.
Class Diagram
8/24/2022 19
Basic components of a class diagram
The standard class diagram is composed of three sections:
Upper section: Contains the name of the class. This section is always
required, to know whether it represents the classifier or an object.
Middle section: Contains the attributes of the class. Use this section to
describe the qualities of the class. This is only required when describing a
specific instance of a class.
Person
attributeName : Type
name : String
address : Address A derived attribute is one that can be computed
birthdate : Date from other attributes, but doesn’t actually exist.
/ age : Date
For example,
ssn : Id
a Person’s age can be computed from his birth
date.
A derived attribute is designated by a
preceding ‘/’ as in:
/ age : Date
Class Attributes (Cont’d)
Person
Attributes can be:
+ name : String + public
# address :
Address # protected
# birthdate : Date - private
/ age : Date
- ssn : Id / derived
Class Operations
Person
name : String
address :
Address
birthdate : Date
ssn : Id
eat
sleep Operations describe the class behavior
work
and appear in the third compartment.
play
Class Operations (Cont’d)
PhoneBook
SmokeAlarm
Responsibilities
8/24/2022 31
Class Diagram – cont..
Attributes : Second Section
• How would you identify each instance of
that class, i.e., a specific animal?
• Using Attributes
• What are Attributes?
• A significant piece of data containing values that describe each instance of that class
• They are also known as fields, variables or properties
• Format for specifying an attribute:
visibility attributeName : Type [multiplicity] = DefaultValue {property string}
• Visibility: It sets the accessibility of an attribute / method
• Private (-) Visible only to the elements within the same class
• Public (+) Visible to any element that can see the class
• Protected (#) Visible to the elements in the class and its sub class
• Package (~) Visible to elements within the same package
8/24/2022 32
Class Diagram – cont..
• Methods : Third section
• They are also known as operations or functions
• It specifies the behaviour of a class
• Eg: We may want to change the name of an Animal
• We could add a method setName
• We could also create a method for eating, since all of
our animals eat
• Lets take another example : Employee Details
8/24/2022 33
Multiplicity and Relationships
8/24/2022 34
Relationships
Class Relationships
The essential connections among classes include
•Association
•Generalization
•Aggregation
•Composition
Association
• An association is a relationship between two separate
classes. It joins two entirely separate entities.
• There are four different types of association:
– Bi-directional
– Uni-directional
– Aggregation (includes composition aggregation)
– Reflexive.
• Bi-directional and uni-directional associations are the
most common ones.
• This can be specified using multiplicity (one to one, one
to many, many to many, etc.).
• A typical implementation in Java is through the use of
an instance field. The relationship can be bi-directional
with each class holding a reference to the other.
Association Relationships
If two classes in a model need to communicate with each other,
there must be link between them.
Student Instructor
Association Relationships (Cont’d)
We can indicate the multiplicity of an association by adding
multiplicity adornments to the line denoting the association.
Student Instructor
1..*
Association Relationships (Cont’d)
Student Instructor
1..*
Association Relationships (Cont’d)
We can also indicate the behavior of an object in an association
(i.e., the role of an object) using rolenames.
membership
Student Team
1..* 1..*
Association Relationships (Cont’d)
We can specify dual associations.
member of
1..* 1..*
Student Team
1 president of 1..*
Association Relationships (Cont’d)
We can constrain the association relationship by defining the
navigability of the association. Here, a Router object requests
services from a DNS object by sending messages to (invoking
the operations of) the server. The direction of the association
indicates that the server has no knowledge of the Router.
Router DomainNameServer
Association Relationships (Cont’d)
Associations can also be objects themselves, called link classes
or an association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
Association Relationships (Cont’d)
next
LinkedListNode
previous
Inheritance
• Indicates that child (subclass) is considered to be a
specialized form of the parent (super class).
• For example consider the following:
Example
CourseSchedule
Course
add(c : Course)
remove(c :
Course)
Dependency - Aggregation
• A special form of association which is a
unidirectional (a.k.a one way) relationship between
classes.
• The best way to understand this relationship is to
call it a “has a” or “is part of” relationship.
• For example, consider the two classes: Wallet and
Money.
• A wallet “has” money. But money doesn’t
necessarily need to have a wallet so it’s a one
directional relationship.
Association Relationships (Cont’d)
We can model objects that contain other objects by way of
special associations called aggregations and compositions.
ASSOCIATION
A tortoise could leave the creep at any point and still
Exist on its own
8/24/2022 54
An aggregation specifies a whole-part relationship between an aggregate (a whole) and
a constituent part, where the part can exist independently from the aggregate.
Aggregations are denoted by a hollow-diamond adornment on the association.
Engine
Car
Transmission
Generalization Relationships
A generalization connects a subclass
Person to its superclass.
Student Employee
TeachingAssistant
Generalization Relationships (Cont’d)
Multilevel Inheritance
Relationships b/w Classes
Inheritance (Generalization)
• Lets say in our zoo, the only animals we have are tortoises, otters and Slow Loris
• We make 3 new classes: Tortoise, Otter and
Slow Loris
• These subclasses inherit all the attributes and
methods of the superclass
• We could add an attribute specific to Otter
in the Otter class
• The main advantage of inheritance is that
if we wanted to change or add an attribute
for all animals, we need not make change in
every sub class
• We just make the change to the base class ‘Animal’ class and it applies across all sub classes
8/24/2022 59
Relationships b/w Classes
Abstraction
• In this example ‘Animal’ is the Abstract Class
• We will create instance for only the subclasses
• We wouldn’t instantiate the Animal class itself
• The Animal class is just a way to simplify things and keep the code “dry”
• To show that it is an Abstract class, we can write the name of the class in italics Animal or we
can enclose it like <<Animal>>
8/24/2022 60
Relationships b/w Classes
Association
• If we had a class for Sea Urchin
we could draw an association
• Sea otter is a marine mammal in
the north Pacific Ocean
• They eat mostly hard-shelled invertebrates
including sea urchins.
ASSOCIATI
ON
8/24/2022 61
Composition
• It is a special type of association that specifies a whole and its parts, where the part cannot
exist outside the whole
• Example: Let us say we have several different visitors centers in our zoo. And each of those
visitor centers has a lobby and a bathroom
• Now if one of our visitors centers was torn down, the lobby and the bathroom of that visitor
center are torn down as well.
• Composition : A child object cannot exist without its parent object
8/24/2022 62
Association Relationships (Cont’d)
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 .. *
Composition
• A restricted form of Aggregation in which two entities (or you can
say classes) are highly dependent on each other.
• A human needs a heart to live and a heart needs a human body to
function on. In other words when the classes (entities) are
dependent on each other and their life span are same (if one dies
then another one does too) then it’s a composition.
Multiplicity
• After specifying the type of association relationship by connecting the
classes, you can also declare the cardinality between the associated
entities. For example:
• The above UML diagram shows that a house has exactly one kitchen,
exactly one bath, at least one bedroom (can have many), exactly one
mailbox, and at most one mortgage (zero or one).
Multiplicity Constraints
• A multiplicity constraint can be
– a single number
– a “*”, meaning an arbitrarily large number or
simply “many”
– a range, denoted by “min..max”
– a sequence of single numbers and ranges
8/24/2022 69
Multiplicity Constraints
is registered for>
Student Semester
1..*
0..* 1
tak
g>
es>
rin
du
ld
he
0..8
1..*
is
teaches> Class
Instructor
Section
1..3 0..6
1..* is
in
st
a
<works for
nc
e
of
>
1 sponsors>
1 1..*
Department Course
Coming up: Multiplicity Constraints
Questions
• From the previous diagram
– How many classes can a student take?
– Do you have to be registered in any classes to be a
student?
– Do I need to teach this class to be an Instructor?
Do I need to teach ANY classes?
1. What type of core-relationship is represented by the symbol in the figure below?
a) Aggregation
b) Dependency
c) Generalization
d) Association
Answer: a
2. Which core element of UML is being shown in the figure?
a) Node
b) Interface
c) Class
d) Component
Answer: d
8/24/2022 72
3. What type of relationship is represented by Shape class and Square ?
a) Realization
b) Generalization
c) Aggregation
d) Dependency
Answer: b
8/24/2022 73
4. Consider a directed line (->) from the relation ship set advisor to both entity sets
instructor and student. This indicates ___________ cardinality.
a. One to many
b. One to one
c. Many to many
d. Many to one
Answer: b
5. An entity set that does not have sufficient attributes to form a primary key is
termed as a ____
a. Strong entity set
b. Variant set
c. Weak entity set
d. Variable set
Answer: c
8/24/2022 74