0% found this document useful (0 votes)
12 views36 pages

Lecture 4

Uploaded by

1annoyingsites
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)
12 views36 pages

Lecture 4

Uploaded by

1annoyingsites
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/ 36

Lecture 4:

Introduction to UML
OO Modeling & Class
Diagrams
BY: DR. SARAH NAIEM
M AT E R I A L S O U R C E :
DR. AMR GHONIEM
S Y S T E M S A N A LY S I S A N D D E S I G N I N A C H A N G I N G W O R L D , 7 T H E D I T I O N
THE SOFTWARE PROCESS

A structured set of activities required to develop a


software system.
Many different software processes but all involve:
▪Specification – defining what the system should do;
▪Design and implementation (Development) – defining the organization
of the system and implementing the system;
▪Validation (Testing) – checking that it does what the customer wants;
▪Maintenance & Evolution – changing the system in response to changing
customer needs.
A software process model is an abstract representation of a
process. It presents a description of a process from some
particular perspective.

2
MODELLING

A model is an abstraction of a system.


Abstraction allows us to ignore unessential details
Why building models?
▪To reduce complexity
▪To test the system before building it
▪To communicate with the customer
▪To document and visualize your ideas

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 3


SOME UML DIAGRAMS
Functional diagrams
▪Describe the functionality of the system from the user’s point of view. It describes
the interactions between the user and the system. It includes use case diagrams.

Static diagrams
▪Describe the static structure of the system: Classes, Objects, attributes,
associations.

Dynamic diagrams:
▪Interaction diagrams
▪ Describe the interaction between objects of the system
▪State diagrams
▪ Describe the temporal or behavioral aspect of an individual object
▪Activity diagrams
▪ Describe the dynamic behavior of a system, in particular the workflow.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 4


UML TOOLS
Some UML tools can generate code once UML diagram is
completed.
Some UML tools are sketching tools.
Rational Rose is one of the most popular software for UML
creation (IBM).
Bouml is an open source s/w. It supports python, C++, java.
Visio is a sketching tool.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 5


STATIC DIAGRAMS

Class diagrams: show the classes and their


relations.

Object diagrams: show objects and their relations.

Package diagrams: show how the various classes


are grouped into packages to simplify complex
class diagrams.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 6


CLASS MODEL
A class model captures the static structure of
the system by characterizing
▪the classes and objects in the system,
▪the relationships among the objects and
▪the attributes and operations for each class
of objects
Class models are the most important OO
models.
In OO systems we build the system around
objects not functionality.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 7


WHAT IS OO SOFTWARE?

Object-oriented software means that we organize


software as a collection of discrete objects that
incorporate both data structure and behavior.
The fundamental unit is the Object.
An object has state (data) and behavior (operations).
In Structured programming, data and operations on
the data were separated or loosely related.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 8


IDENTITY
An object can be concrete like a car, a file, …
An object can be conceptual like a feeling, a plan,…
Each object has its own identity even if two objects
have exactly the same state.

Omar’s car Rana’s car

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 9


IDENTITY
Object identity is the property by which each object can
be identified and treated as a distinct software entity.

Each object has unique identity which distinguishes it


from all its fellow objects. It is its memory address (or
handle) that is referred to by one or more object
identifiers (variables).

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 10


IDENTITY
car my_car = new car(“Opel”, 2005);

The object handle is 0x00FDA610 is referenced by an


object identifier (object variable) my_car

0x00FDA610 model: Opel


year: 2005
my_car
0x00FDA610

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 11


CLASSES AND OBJECTS

14
• Each object is an instance of a class

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS


• Each object has a reference to its class
(knows which class it belongs to)
CLASSIFICATION
Classification means that objects with the same
data structure (attributes) and behavior
(operations) belong to the same class.
A class is an abstraction that describes the
properties important for an application
The choice of classes is arbitrary and
application-dependent.

3 2

Mina’s car Ali’s car Samir’s car A Car

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 13


CLASSIFICATION
Objects in a class share a common
semantic
purpose in the system model.

Both car and cow have price and age.

If both were modeled as pure financial assets, they both can


belong to the same class.

If the application needs to consider that:


▪Cow eats and produces milk
▪Car has speed, make, manufacturer, etc.

then model them using separate classes.

So the semantics depends on the application


INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 14
ABSTRACTION
Abstraction is the selective examination of
certain aspects of a
problem.
Abstraction aims to isolate the aspects that
are important for some purpose and suppress
the unimportant aspects.
The purpose of abstraction determines what is
important and what is not.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 15


ABSTRACTION
Car
Car
-model: string
-model: string
-Year: string
-year: int
-problem: string
-licenseNumber: string
-owner: string
-motorCapacity: int
-balance: float
-isFinished: bool
+Car (int, ….): void
+getMake (): string +Car (string,...): void
+printDetails(): void +printBlanace (): string
+........ +printDetails(): void
+........

In Department of At the
Motor Vehicles mechani
c

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 16


ENCAPSULATION
Encapsulation separates the external aspects of
an object, that are accessible to other objects,
from the internal implementation details that are
hidden from other objects.
Encapsulation reduces interdependency between
different parts of the program.
You can change the implementation of a class (to
enhance performance, fix bugs, etc) without
affecting the applications that use objects of this
class.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 17


ENCAPSULATION

Data hiding. information from within the object


cannot be seen outside the object.
Implementation hiding. implementation details
within the object cannot be seen from the outside.

20

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS


ENCAPSULATION void sort
int i,
() { // Bubble Sort
j;
for (i = length - 1; i > 0; i-) {
List for (j = 0; j < i; j++) {
- items: int [ ] if (items [j] > items [j + 1]) {
- length: int int temp = items [j];
items [j] = items [j + 1];
+ List (array): void items [j + 1] = temp;
+ search (int): bool }
+ getMax (): int }
+ sort(): void }
}`

void sort () { // Quick Sort


……….
}`

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 19


Encapsulation
Encapsulation in object-oriented programming (OOP) is like
putting things in a box. It helps keep data safe and organized by
bundling it with the methods that operate on that data. Here’s
a simple breakdown:
Data Hiding: You keep some parts of an object private, so they
can’t be accessed directly from outside the object. This
prevents unintended interference and maintains control over
how the data is used.
Public Interface: You provide public methods (functions) to
interact with the private data. This way, you can control how
the data is accessed or modified.
Simplification: By hiding complexity, users of the object don’t
need to know all the details about how it works; they just need
to know how to use it
Inheritance
Generalization/Specialization
◦ A hierarchical relationship where subordinate classes are special
types of the superior classes. Often called an Inheritance Hierarchy

Superclass
◦ the superior or more general class in a generalization/specialization
hierarchy

Subclass
◦ the subordinate or more specialized class in a
generalization/specialization hierarchy

Inheritance
◦ the concept that subclasses classes inherit characteristics of the
more general superclass
Inheritance
INHERITANCE
Inheritance is the sharing of features (attributes and
operations) among classes based on a hierarchical
relationship.
A superclass (also parent or base ) has general
features that sublcasses (child or derived ) inherit.
and may refine some of them.
Inheritance is one of the strongest features of OO
technology.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 23


HOW TO USE INHERITANCE? Vehicle
Inheritance helps building software -motorCapacity: int
incrementally: -model: string
-make: string
First; build classes to cope with the -year: int
most straightforward (or general)
+Car (int, ….): void
case, +getMake (): string
+printDetails(): void
Second; build the special cases that
+........
inherit from the general base class.
These new classes will have the same
features of the base class plus their
own.
Taxi
Truck

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 24


POLYMORPHISM

Polymorphism means that the same operation may


behave differently for different classes.
An operation is a procedure or a transformation
that the object performs or is subject to.
An implementation of an operation by a specific
class is called a method.
Because an OO operation is polymorphic, it may
have more than one method for implementing it,
each for a different class.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 25


Shape Italic means

POLYMORPHISM - color: int operation is


specified but not
+ Shape (int): void implemented in
+ getColor (): int the base class
+ setColor (int): void
+ getArea(): float

Rectangle Circle Square


- length: float - radius: float - side: float
- width: float
+ Circle(int, int): void + Square(int, int): void
+ Rectangle ……. + getRadius(): floa + getSide(): floa
+ getArea(): float + setRadius(): t + setSide(): t
+ getArea(): float floa + getArea(): floa
t t
length x width side2
 x radius2 floa
t

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 26


GENERICS
Pros of Generic class: Cons of Generic Classes:
Flexibility: Instead of specifying a Complexity: Sometimes,
single data type (like integers or understanding generics can be
strings), a generic class can operate challenging, especially for beginners
on various types. This means you
can use the same class for different Performance Overhead: In some
kinds of data without rewriting code languages, generics might introduce
slight performance overhead due to
Type Safety: Generics ensure that type checks.
the type you use is checked at
compile time. This helps catch errors Limited Features: Some operations
early, as you can't mix incompatible might be less intuitive or not
types accidentally. available since the exact type isn’t
known until runtime.
Reusability: You can create more
reusable and cleaner code. For
example, a generic list can store any
type of object, allowing you to use it
for integers, strings, or custom
objects.
A generic class makes your code
more versatile and safe by allowing
it to handle multiple data types
while keeping everything organized.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 27


GENERICS
Generic Class ; the data types of one or more of
its attributes are supplied at run-time (at the time
that an object of the class is instantiated).
This means that the class is parameterized, i.e., the
class gets a parameter which is the name of
another type.
Sorter T will be known
- data [ ]: T at runtime ..

+ sortData (): void


+ …….

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 28


CLASS MODEL

A class model captures the static structure


of the system by characterizing
▪the classes and objects in the system,
▪the relationships among the objects and
▪the attributes and operations for each class of objects
Class models are the most important OO
models
In OO systems we build the system around
objects not functionality

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 29


OBJECTS

Objects often appear as proper nouns in the problem


description or discussion with the customer.
Some object correspond to real world entities (BUE, MIT,
Omar’s car)
Some objects correspond to conceptual entities (the formula
for solving an equation, binary tree, etc.)
The choice of objects depends on the analyst’s judgment
and the problem in hand. There can be more than one correct
representation.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 30


CLASS
An object is an instance of a class
A class describes a group of objects with the same
▪Properties (attributes)
▪Behavior (operations)
▪Kinds of relationships

Person, Company and Window are all classes Classes


often appear as common nouns and noun
phrases in problem description and discussion with 30

customers or users

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS


CLASS MODEL

Provides a graphical notation for modeling classes and


their relationships, thereby describing possible objects.
Class diagram is useful for:
▪Designing and Implementing the programs.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 32


VALUES AND ATTRIBUTES

An attribute is a named property of class that describes a value held by each


object of that class.
Attribute name is unique per class.
Several classes may have the same attribute name.
A value is a piece of data assigned to an attribute.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 33


OPERATIONS AND METHODS
Financial Asset
-type: int
An operation is a function or procedure -age: floa
that may be applied to or by objects -currentValue:t float
-. . .
of a class.
A method is the implementation of an +getCurrentValue(): int
+printDetails(): void
operation for a class. +........
An operation is polymorphic if it takes
different forms in different classes.
All objects of the same class have the
same operations.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 34


A SAMPLE CLASS MODEL

Model the classes in a system that represents


flights. Each city has at least an
airport. Airlines operate flights from and to
various airports. A flight has a list of
passengers, each with a designated seat.
Also a flight uses one of the planes
owned by the operating airline. Finally a
flight is run by a pilot and a co-pilot.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 35


A SAMPLE CLASS MODEL

Model the classes in a system that represents


flights. Each city has at least an
airport. Airlines operate flights from and to
various airports. A flight has a list of
passengers, each with a designated seat.
Also a flight uses one of the planes
owned by the operating airline. Finally a
flight is run by a pilot and a co-pilot.

INTRODUCTION TO UML, OO MODELLING, & CLASS DIAGRAMS 36

You might also like