Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views58 pages

Lecture 3 - UML and Class Diagrams

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views58 pages

Lecture 3 - UML and Class Diagrams

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Lecture 3

Class Diagrams

CMP120130 Object Oriented Modeling & Development 1


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.

CMP120130 Object Oriented Modeling & Development 2


CMP120130 Object Oriented Modeling & Development 3
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.

CMP120130 Object Oriented Modeling & Development 4


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.

CMP120130 Object Oriented Modeling & Development 5


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.

CMP120130 Object Oriented Modeling & Development 6


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).

CMP120130 Object Oriented Modeling & Development 7


Why Class Diagrams?

▪ Logical Grouping of Classes


▪ Encapsulation
▪ More readable and maintainable code

CMP120130 Object Oriented Modeling & Development 8


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.

CMP120130 Object Oriented Modeling & Development 9


Source: https://qavalidation.com/2018/06/java-oops-part-1-intro-to-oops-class-object-constructor-member-variables.html/

CMP120130 Object Oriented Modeling & Development 10


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").

CMP120130 Object Oriented Modeling & Development 11


• 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)

CMP120130 Object Oriented Modeling & Development 12


Full Notation of a Class

CMP120130 Object Oriented Modeling & Development 13


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 (#).
CMP120130 Object Oriented Modeling & Development 14
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??
CMP120130 Object Oriented Modeling & Development 15
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.

CMP120130 Object Oriented Modeling & Development 16


• Four main types of relationships:

• Association

• Aggregation

• Composition

• Inheritance (generalization)

• Dependency

CMP120130 Object Oriented Modeling & Development 17


CMP120130 Object Oriented Modeling & Development 18
Strength of the Relationships

CMP120130 Object Oriented Modeling & Development 19


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

CMP120130 Object Oriented Modeling & Development 20


• 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.

CMP120130 Object Oriented Modeling & Development 21


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.

CMP120130 Object Oriented Modeling & Development 22


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.

CMP120130 Object Oriented Modeling & Development 23


CMP120130 Object Oriented Modeling & Development 24
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.

CMP120130 Object Oriented Modeling & Development 25


• 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

CMP120130 Object Oriented Modeling & Development 26


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.

CMP120130 Object Oriented Modeling & Development 27


Generalization/ Specialization
• Generalization hierarchies may be created by generalizing from specific things or by specializing
from general things.

CMP120130 Object Oriented Modeling & Development 28


Inheritance

CMP120130 Object Oriented Modeling & Development 29


• 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.

CMP120130 Object Oriented Modeling & Development 30


Parent?
Child?

CMP120130 Object Oriented Modeling & Development 31


▪ 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.

▪ Super classes 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.

CMP120130 Object Oriented Modeling & Development 32


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.

CMP120130 Object Oriented Modeling & Development 33


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.

CMP120130 Object Oriented Modeling & Development 34


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.

CMP120130 Object Oriented Modeling & Development 35


• 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

CMP120130 Object Oriented Modeling & Development 36


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)

CMP120130 Object Oriented Modeling & Development 37


Example

CMP120130 Object Oriented Modeling & Development 38


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.

CMP120130 Object Oriented Modeling & Development 39


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.

CMP120130 Object Oriented Modeling & Development 40


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..”

CMP120130 Object Oriented Modeling & Development 41


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”.

CMP120130 Object Oriented Modeling & Development 42


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.

CMP120130 Object Oriented Modeling & Development 43


• 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.

CMP120130 Object Oriented Modeling & Development 44


• 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).

CMP120130 Object Oriented Modeling & Development 45


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.

CMP120130 Object Oriented Modeling & Development 46


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.

CMP120130 Object Oriented Modeling & Development 47


▪ Dependencies
When Changing the design of a class will affect other classes.
▪ Interfaces
Used to reduce coupling between objects.

CMP120130 Object Oriented Modeling & Development 48


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.

CMP120130 Object Oriented Modeling & Development 49


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.

CMP120130 Object Oriented Modeling & Development 50


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.

CMP120130 Object Oriented Modeling & Development 51


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.

CMP120130 Object Oriented Modeling & Development 52


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.”

CMP120130 Object Oriented Modeling & Development 53


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.

CMP120130 Object Oriented Modeling & Development 54


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.

CMP120130 Object Oriented Modeling & Development 55


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.

CMP120130 Object Oriented Modeling & Development 56


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.

CMP120130 Object Oriented Modeling & Development 57


▪ 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.

CMP120130 Object Oriented Modeling & Development 58

You might also like