UML and Class Diagrams

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

ICT 1411

Object
Oriented
Programming
Lecture 1
Unified Modelling Language (UML)
Class Diagrams

Hansika Samanthi
UML
Unified Modeling Language
• The Unified Modeling Language is a standard visual modeling language intended to be used for,

• Modeling business and similar processes.

• Analysis, design, and implementation of software-based systems.

• UML is a common language for business analysts, software architects and developers used to
describe, specify, design, and document existing or new business processes, structure and
behavior of artifacts of software systems.
Structural Diagrams

• Depict a static view or structure of a system.

• It is widely used in the documentation of software architecture.

• It embraces class diagrams, composite structure diagrams, component diagrams, deployment


diagrams, object diagrams, and package diagrams.

• It presents an outline for the system.

• It stresses the elements to be present that are to be modeled.


Behavioral Diagrams
• Behavioral diagrams portray a dynamic view of a system or the behavior of a system, which
describes the functioning of the system.

• It includes use case diagrams, state diagrams, and activity diagrams.

• It defines the interaction within the system.


Interaction Diagrams

• Interaction diagrams are a subclass of behavioral diagrams that give emphasis to object
interactions and also depicts the flow between various use case elements of a system.

• In simple words, it shows how objects interact with each other and how the data flows within
them.

• It consists of communication, interaction overview, sequence, and timing diagrams.


Class Diagrams

• Class diagram is UML structure diagram which shows structure of the designed system at the level
of classes and interfaces, shows their features, constraints and relationships - associations,
generalizations, dependencies, etc.

• Object diagram could be considered as instance level class diagram which shows instance
specifications of classes and interfaces (objects), slots with value specifications, and links
(instances of association).
Why Class Diagrams?

▪ Logical Grouping of Classes


▪ Encapsulation
▪ More readable and maintainable code
What can be classes?
▪ Traditionally,
▪ Nouns in a specification
▪ Each class's instances / objects should have its own identity, behavior and state.
▪ Physical/ logical objects.
▪ Identifier for each physical/logical object.
▪ People, organizations.
▪ Person's and organization's roles.
▪ Places and locations.
▪ Transactions made among classes.
▪ Events.
Source: https://qavalidation.com/2018/06/java-oops-part-1-intro-to-oops-class-object-constructor-member-variables.html/
What are classes?
▪ A class describes a group of objects with:
▪ Similar properties (attributes)

▪ Common behavior (operations)

▪ Common relationships to other objects,

▪ And common meaning ("semantics").


• Example:
• Employee: Has a name, employee number and department; An employee is
hired, and fired; An employee works in one or more projects.

Name(Mandatory)

Attributes(Optional)

Operations(Optional)
Full Notation of a Class
Visibility Types
There are three common visibility types in UML class diagrams:

▪Public (+):
▪The attribute or method is accessible from anywhere.
▪Both within the class and from external classes.
▪It is denoted by a plus sign (+).

▪Private (-):
▪Restricts access to the attribute or method to only within the class in which it is
defined.
▪It is denoted by a minus sign (-).

▪Protected (#):
▪Allows access to the attribute or method within the class and its subclasses (i.e.,
classes that inherit from it).
▪It is denoted by a hash (#).
Objects Vs. Classes
▪ The instances of a classes are called as objects.
▪ Objects are represented as:

• The relation between object and its class is called "Instantiation".


• Two different objects may have identical attribute values (like two people with identical name
and address).
• Note: Make sure attributes are associated with the right class.
• Ex: You don't want both manager name and manager number as attributes of Project. Why??
Relationships

▪ A relationship refers to the connection or association between two or more classes


within a system.

▪ These relationships define how the classes interact with each other and can represent
various aspects of the system's structure and behavior.
• Four main types of relationships:

• Association

• Aggregation

• Composition

• Inheritance (generalization)

• Dependency
Strength of the Relationships
Association
• Association connects one instance of a class with another instance of another class.

• The relationship can be bi-directional (two way)or uni-directional (one way)

• If there is an arrowhead, it means there is a one-way relationship.

Bi-directional Uni-directional
• Here, it means that;
• Class A is associated with class B.
• Class A uses and contains one instance of class B, but B does not know about or contain any
instances of class A.
• In an association relationship, the dependent class (A) defines an instance of the associated class
(B) within its class scope.
Employee Department

• The Employee class and the Department class are associated.


• Here, it is bi-directional.
• The Employee is related to the Department in some way.
• The Department is also related to the Employee in some way.
• The exact nature of the association is unknown.
Association
Associations may optionally have the following:
▪ Association name
▪ May be prefixed or postfixed with a small black arrowhead to indicate the direction in which the
name should be read.
▪ Should be a verb or verb phrase.
▪ Role name
▪ On one or both association ends.
▪ Should be a noun or noun phrase describing the semantics of the role.
▪ Multiplicity
▪ The number of objects that can participate in an instantiated relation.
▪ Navigability.
Association Multiplicity
▪ Ask questions about associations:

▪ Can a company exist without any employee?

▪ If yes, then the association is optional at the Employee end –zero ore more (0...*)

▪ If no, then it is not optional – one or more (1...*)

▪ What about the other end of the association?

▪ Can an employee work for more than one Company?

▪ No. So the correct multiplicity is one.


• Some examples for specifying multiplicity:
• Optional (0 or 1) 0..1

• Exactly one 1 OR 1..1

• Zero or more 0...* OR *

• One or more 1...*

• A range of values 2..6


Inheritance

▪ Otherwise Known as Generalization.

▪ Inheritance represents a “is-a-kind-of" relationship.

▪ Inheritance is a relationship between a general thing (superclass/ parent) and a more


specific kind of that thing (subclass/ child).

▪ Graphically, a dependency is rendered as a empty block arrow.


Generalization/ Specialization
• Generalization hierarchies may be created by generalizing from specific things or by specializing
from general things.
Inheritance
• Generalization takes place from sub-class to super-class: the super-class is a generalization of the
sub-class.

• Specialization takes place from super-class to sub-class: the sub-class is a specialization of the
super-class.

• The functionality of the child should be a specialization of the functionality of the parent.
Parent?
Child?
▪ Notes:

▪ A subclass may override an inherited aspect.

Ex: AdminStaff and CreativeStaff have different methods to calculate bonuses.

▪ A subclass may add new features.

Qualification is a new attribute in CreativeStaff.

▪ Supeclasses may be declared {abstract}, meaning they have no instances.

Implies that the subclasses cover all possibilities.

Ex: There are no other staff than AdminStaff and CreativeStaff.


Inheritance- Activity
• Draw a class diagram for the following football club. Try to identify different types of relationships
between classes.

• The football club has lots of members. A member may be a playing member or a social member. A
playing member may be an adult member or a junior member.

• A playing member may be chosen to play for one or more teams, and each team has 11 playing
members. Each team plays a number of matches.
Misuse of Inheritance
1. Inheritance in place of aggregation:
• using “is a” instead of “has a”.

Car Engine

• Car has an Engine.


• Engine is not a Car

• A better way to represent the relationship is through aggregation or composition.


2. Inverted hierarchy:

• The design (right) represents a hierarchy in the real world.

• The idea is that capturing the real-world hierarchy via inheritance


relationships will capture real-world behavior.

• This implies that:

• a Worker can do everything a CEO can do;

• a Worker is also “better” at doing these things.


• The solution may be simply to reverse the direction of
inheritance:

• In the real-world, the higher entity in the hierarchy has the


most power.

• In object oriented design, the higher entity in the hierarchy


has the least power
Whole-part Relationships

• A "whole/part“ relationship refers to a fairly strong connection between two classes.


• One class represents a larger thing("whole"), which consists of smaller things("parts").
• This means “has a” relationship, meaning that an object of the whole has objects of the part.
• Two types;
• Aggregation(Relatively Weak)
• Composition(Relatively Strong)
Example
Aggregation
• Aggregation is just a special kind of association.

• Aggregation is a weak form of whole-part relationship.

• Graphically, a dependency is rendered as an empty diamond arrow.

• This means that A aggregates B.


Multiplicity:

• The same rule for multiplicity can be applied for aggregation relationship.

• Aggregation should have a relationship with a multiplicity of “0..”

• In this case, every frequent course followed by zero or more students.

1 0..*
Course Student

• Whole: Course

• Part: Student

• A Course followed by zero or more students.

• This implies that the Course can exist without any Students.

• This Implies that the whole can exist without the parts.
Composition
• Composition is a strong form of whole-part relationship.

• Graphically, a dependency is rendered as a filled diamond arrow.

• Composition should have a relationship with a multiplicity of “1..”


1 1..*
Building Room

• Whole: Building
• Part: Room
• A Building is composed of at least one Room.
• If there are no rooms, there is no Building.
• Implies that the “whole cannot exist without the parts”.
Dependency
▪ It is the simplest form of relationship between two classes.

▪ It means: one class depends on another class in some way.

▪ It implies that a change to one class may affect the other but not vice versa.

▪ It describes a connection at a higher level of abstraction than an association.

▪ As it may have a broad meaning, it is best not to overuse the dependency relationship.
• It means that;

• A uses B, But A does not contain an instance of B as part of its own state.

• If B's interface changes, it will likely to impact A and require it to change.


• You may use dependency to indicate that,

• “A” receives an instance of “B” as a parameter to at least one of its methods.

• “A” creates an instance of “B” local to one of its methods.

• You would not use dependency to indicate that

• “A” declares an instance variable of “B”. Use association to do that (covered next).
Cart class depends on a Product class because the Cart class uses the Product class as a parameter to
Add() method.
Changing the Product Class will affect the Cart Class.
What UML Diagrams can show??
▪ Division of Responsibility.
Operations that objects are responsible for providing.
▪ Subclassing.
Inheritance, generalization.
▪ Navigability/ Visibility.
When object need to know about other objects to call their operations.
▪ Aggregation / Composition.
When objects are part of other objects.
▪ Dependencies
When Changing the design of a class will affect other classes.
▪ Interfaces
Used to reduce coupling between objects.
Exercise 1
Person Class:
Properties:
First Name, Last Name, Contact No, Address
Operations:
displayFullName(): Output the full name (first name and last name) of the person.
setContactNo(): Modify the contact number of the person.
Exercise 2
Bank Account Class:
Attributes:
Account Number, Balance
Operations:
deposit (amount): Adds the specified amount to the account balance and return
the balance.
withdraw (amount): Subtracts the specified amount from the account balance and
return the balance.
Exercise 3
Imagine a university student system where students are categorized as either
Undergraduate or Postgraduate. All students have a unique student ID and a name.
Undergraduate students has a major field of study during their program. Postgraduate
students have a research area (e.g., "Machine Learning") and first degree qualifications. As
operations, postgraduate can request a supervisor and undergraduate can request for
hostel.
Exercise 4
In an institute building, there are multiple floors, each with its own number and name.
Classrooms are located on these floors and are considered a part of the floors. Each
classroom is uniquely named. Students attend classes in the classrooms but are not
exclusively assigned to one classroom. Some classrooms may not have any students at all.
Exercise 5
1. Identify a relationship that represent
aggregation?

2. How many instances from shopping


cart will associate with a customer?

3. State whether the following


statement is true or false. Justify your
answer using the class diagram.
“Customer and Admin both have the
verifyLogin() functionality.”
Exercise 6
A hospital has a large number of registered physicians. Attributes of Physician include
physicianID and specialty. Patients are admitted to the hospital by physicians. Attributes of
Patient include patientID and patient Name. Any patient who is admitted must have exactly
one admitting physician. A physician may optionally admit any number of patients. Once
admitted, a given patient must be treated by at least one physician. A particular physician
may treat any number of patients, or he or she may not treat any patients. Each patient
has a medical record that include record id, diagnosis and medications. A medical record
cannot exist without a patient.
Exercise 7
A bank has three types of accounts: checking, savings, and loan. Following are the
attributes for each type of account:
• CHECKING: Acct_No, Date_Opened, Balance, Service_Charge
• SAVINGS: Acct_No, Date Opened, Balance, Interest_Rate
• LOAN: Acct_No, Date Opened, Balance, Interest_Rate, Payment
Assume that each bank account must be a member of exactly one of these subtypes. At
the end of each month, the bank computes the balance in each account and mails a
statement to the customer holding that account. The balance computation depends on the
type of the account. For example, a checking account balance may reflect a service charge,
whereas a savings account balance may include an interest amount. Draw a class diagram
to represent the situation. Your diagram should include an abstract class, as well as an
abstract operation for computing balance.
Exercise 8
In the university there are permanent lecturers and visiting lectures. A lecturer has an id,
name, contact number and address. A lecturer can teach one or many courses. A course
has an id, name and number of credits. A university has a large number of courses in its
catalog. Attributes of Course include course Number, course Name, and units. Each course
may have zero or more different courses as prerequisites. Similarly, a particular course may
be a prerequisite for any number of courses, or it may not be a prerequisite for any other
course. The lecturers are categorized into permanent and visiting lecturers. There is a basic
salary for a permanent lecturer. For a visiting lecturer, there are number of hours and hour
rate. The students enroll for many courses. A student has registration number, index
number, name and address. The students can involve with project/s if they are interested.
A project has an id, name and subject areas. Permanent lecturers can supervise the
project/s if they need.
Exercise 9
Problem Statement:
• An online bookstore, struggles with keeping track of its books, suppliers, and customers.
Doing this manually causes mistakes and delays. When customers order books, it's hard
to manage because everything is done by hand. This leads to slow order processing and
unhappy customers. Plus, dealing with suppliers to get new books on time is tough
without a good system. Fixing these problems, like organizing inventory better,
automating orders, and managing suppliers well, is important for XYZ Books to work
smoother and make customers happier.

Design the system for the online bookstore dealing with manual inventory, order
processing, and supplier management. Draw a use case diagram to outline the system's
functionalities and a class diagram to represent its structure.

Submit a single document in PDF format via the LMS.


▪ Object : An entity that has well-defined role in the application domain, and it has
Terms state behavior, and identify characteristics.
▪ State : Encompasses an object's properties (attributes and relationships) and the
values of those properties.
▪ Behavior : Represents how an object acts and reacts.
▪ Class/ Object Class : A logical grouping of objects that have the same attributes,
relationships and behaviors.
▪ Class diagram - lass diagram shows the static structure of an object-oriented model
the object classes, their internal structure, and the relationships in which they
participate.
▪ Operation : A function that is provided by all the instances of the class.
▪ Association : A named relationship between or among classes.
▪ Multiplicity : A specification that indicates how many objects participate in a given
relationship.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy