0% found this document useful (0 votes)
23 views79 pages

Lect 4

Uploaded by

messagetome133
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)
23 views79 pages

Lect 4

Uploaded by

messagetome133
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/ 79

SOFTWARE ENGINEERING I

Lecture 4
Introduction to Modelling using UML (Class Models)
1

1
TOPICS COVERED Object Oriented “OO” Characteristics
Identity
• Modelling Classes and Objects
Classification
• Some UML Diagrams
Abstraction
• UML Tools Encapsulation
• Static Diagrams Inheritance
How to use Inheritance?
• Class Model Polymorphism
• Objects Generics
Class Model
• Values and Attributes Objects
• Values and Attributes
2

Operations and Methods


Operations and Methods
• Summary of Basic Class Notation Summary of Basic Class Notation
• Sample Class Model Sample Class Model
2
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 – defining the organization of the
system and implementing the system;
• Validation – checking that it does what the customer wants;

3

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

4
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 diagram
• 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 5
Describe the dynamic behavior of a system, in particular the workflow.
STATIC DIAGRAMS

• Class diagrams : show the classes and their relations

• Object diagrams : show objects and their relations

• Package diagrams :shows how the various classes are grouped into
packages to simplify complex class diagrams.

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

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.

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


5
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). 6
IDENTITY

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

0x00FDA610 model: Opel


my_car year: 2005
0x00FDA610
•The object handle is 0x00FDA610 is referenced by an object identifier
7

(object variable) my_car


8
CLASSES AND
OBJECTS

• Each object is an instance of a


class
• Each object has a reference to
its class (knows which class it
belongs to)

8
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 9


A Car
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
10
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.

11
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
+Car (string,...): void
+getMake (): string
+printBlanace (): string
+printDetails(): void
+printDetails(): void
+........
+........

In Department of At the
Motor Vehicles mechanic
12
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.

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

14
ENCAPSULATION
void sort () { // Bubble Sort
int i, 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


……….
}`
15
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.

16
INHERITANCE

• Inheritance is the facility by which objects of a class (say


B) may use the methods and variables that are defined A
only to objects of another class (say A), as if these
methods and variables have been defined in class B
•Inheritance is represented as shown in UML notation. B

17
HOW TO USE INHERITANCE?

• Inheritance helps building software incrementally: Car

• First; build classes to cope with the most -motorCapacity:


-model: string
int
straightforward (or general) case, -make: string

• Second; build the special cases that


-year: int
inherit from
+Car (int, ….): void
the general base class. These new classes will have +getMake (): string
the same features of the base class plus their own. +printDetails():
+........
void

Taxi Truck

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

19
POLYMORPHISM
Shape Italic means
- 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(): float + getSide(): float
+ getArea(): float + setRadius(): float + setSide(): float
+ getArea(): float + getArea(): float
length x width side2
 x radius2
20
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.
• T will be known
• at runtime
Sorter
- data [ ]: T

+ sortData ():void
+ ……. 21
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

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

27
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 customers or users

28
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

29
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. 30
MORE ON ATTRIBUTES

31
OPERATIONS AND METHODS


Financial Asset
An operation is a function or procedure that may be
applied to or by objects of a class. -type: int

• A method is the implementation of an operation for


-age:
-currentValue:
float
float
a class. -. . .

• An operation is polymorphic if it takes different +getCurrentValue(): int


+printDetails(): void
forms in different classes. +........

• All objects of the same class have the same


operations.

32
SUMMARY OF CLASS NOTATION

• The attribute and operation compartments are optional.


• You may show them or not depending on the level of abstraction you want.
• A missing attribute compartments means that the attributes
are not specified yet.
• But empty compartment means that the attributes are specified but
there are none.

33
SUMMARY OF BASIC CLASS NOTATION

17

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.

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.

36
A SAMPLE CLASS MODEL
• Flights • City • Airlines
• List of Passengers • Seat • Planes
• Pilot and a Co-Pilot

City Airline Pilot Plane

Passenger

Airport Flight Seat

37
CONTINUING CLASS DIAGRAMS
Associations
Multiplicity
Links and Associations
Roles
Self-Associations
Aggregation & Composition Relation
Generalization, Specialization & Inheritance
Overriding Features
Abstract Class

22
ASSOCIATIONS

• An association is a relationship between classes that indicate some


meaningful and interesting relationship.
• It’s represented by a line with a name.
• Properlynaming associations is important to enhance
understanding: use verb phrase.

39
ASSOCIATIONS

• An optional "reading direction arrow" indicates the direction to read the


association name; it does not indicate direction of visibility or navigation

INTRODUCTIONTOMODELLINGUSINGUML(CLASSMODELS)

40
ASSOCIATIONS

• There may be more than one associations between classes


(this is not uncommon).

OwnsStock
Person Company
* *
WorksFor

* 0..1

41
ASSOCIATIONS

• Associations are usually implemented by a reference from an object to


another.
• Associations are inherently bidirectional. They can be traversed in either
direction. A person Works For a company and a company Employs a person.
• Associations could be unidirectional.

42
MULTIPLICITY

• Multiplicity specifies the number of instances of one class that may relate to
a single instance of an associated class.

43
MULTIPLICITY

For example, if a person WorksFor a company, can he work for more than one
company? In other words, is it one-to-one or one-to-many association?
Multiplicity exposes hidden assumptions in the model

44
MULTIPLICITY VALUES

45
LINKS AND
ASSOCIATIONS
A link is a physical or conceptual connection
among objects
• For example John works for GE company.

An association is a relationship between


classes and represents group of links.
• For example, a person Works For a company.
46
LINKS AND ASSOCIATIONS

47
LINKS AND ASSOCIATIONS

48
ROLES

• Each association End can be labeled by a role.


• This makes understanding associations easier.
• They are especially important for Self-associations between objects of
the same class.

49
SELF ASSOCIATIONS

Pre-req.

0..2
Course

50
ASSOCIATION CLASSES

An Association class is an association that is also a class (has attributes and


operations)

15
ASSOCIATION CLASSES

• Many-to-may associations provide compelling rationale for


association classes.
• accessPermission is a joint property of File and User and
cannot placed in one of them only.

52
EXERCISES
Sketch a class diagram for the following:
Each person works for a company receives a salary and has a job title. The
person can work for more than one company. The boss evaluates the
performance of each worker.

53
EXERCISES
Sketch a class diagram for the following:
A user may be authorized on many workstations. Each authorization has a
priority and access privileges. A user has a home directory for each authorized
workstation, but several workstations and users can share the same home
directory.

54
ASSOCIATION CLASSES VS. ORDINARY
CLASSES
Association class OwnsStock has only one occurrence for each pair of person
and company (i.e. only one link exists between pair of person and company
objects ).
In contrast there can be any number of purchase objects for each pair of
person and company objects
QUALIFIED ASSOCIATION
A qualified association is an association in which an attribute called the
qualifier is used to reduce a many relation to one relation on the other end.
Not qualified

1 * File
Directory
filename

qualified

1 0..1
Directory File
filename

56
QUALIFIED ASSOCIATION

• A bank has many accounts [0..*]


• But a bank + account number gives only one account Account Number is
attribute of account class.

57
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 usesone of the
planes owned by the operating airline. Finally a flight is run by a pilot and a
co-pilot.

58
The order of modeling is:

• Define classes
• Define associations
• Define multiplicity

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

60
A SAMPLE CLASS MODEL
• Flights • City • Airlines
• List of Passengers • Seat • Planes
• Pilot and a Co-Pilot

City Airline Pilot Plane

Passenger

Airport Flight Seat

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

62
A SAMPLE CLASS MODEL

City has airport(s) Airline operates flights Flight from airport


Flight to airport Flight has passengers Passenger has seat
Flight uses airplane Airplane owned by
Flight run by pilot & co-pilot airline
1 owns *
1
City Airline Pilot Plane
1 2 1 has
1
fly *
has operates uses
Seat
*
*
* *
Airport 1 from * Flight Passenger
* has * 27
1 to *
A SAMPLE CLASS MODEL

• Add some suitable attributes and operations to the classes of the flight
system.

28
FURTHER CLASS CONCEPTS
AGGREGATION RELATION

• Aggregation is a special kind of association where an aggregate


object contains constituent parts. It’s has-a relationship

29
AGGREGATION RELATION

• This diagram shows that a patient record is a


composite of Patients and an indefinite number of
Consultations.

66
FURTHER CLASS CONCEPTS
COMPOSITION RELATION

• Composition is a special kind of association in which a constituent part belongs


at most to one assembly and exists only if the assembly exists.

30
FURTHER CLASS CONCEPTS
AGGREGATION & COMPOSITION RELATION

68
FURTHER CLASS CONCEPTS
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
•Generalization is the relationship between class (the superclass) and
one or more variations of the class (the subclass).
•The superclass holds common attributes, operations and associations.
•The subclasses add specific attributes, operations and
associations.
•Each subclass is said to inherit the features of the its superclass.
• Inheritance is called “is-a” relationship.

6
9
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
• The terms generalization, specialization and inheritance refer to
aspects of the same idea, they are used in place of one another .
• Generalization refers to the super-class generalizing its subclasses
• Specialization refers to the fact that the subclass specializes or
refines the super-class,
• Inheritance refers to the mechanism for implementing generalization
/ specialization.

70
GENERALIZATION, SPECIALIZATION AND
INHERITANCE

•Generalization is transitive across an arbitrary number of levels.


• An ancestor is parent or grandparent class of a class. A descendant is
a child or grandchild of a class.

71
35
USE OF GENERALIZATION

• Generalization serves three purposes.


1.Supporting polymorphism.
2.Structuring the description of objects and their relation to each other
based on their similarity and differences.
3.Reusing code. You inherit code from super-classes or from a class library
automatically with inheritance. Reuse is more productive than repeatedly
writing code form scratch. When reusing code, you can adjust the code
if necessary to get the exact desired behavior.

73
OVERRIDING FEATURES

• A subclass may override a superclass feature by defining a feature with the same
name.

• The overriding feature (in the sub-class) replaces the overridden feature (in the
super-class)
• Never override a feature so that it is inconsistent with the original inherited feature.

• Overriding should preserve the attribute type, number and type of arguments
of an operation and its return type.

7
4
GENERALIZATION, SPECIALIZATION AND
INHERITANCE

Shape
- color: int

+ Shape (int): void


+ getColor ():int
+ 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(): float + getSide(): float
+ getArea(): float
+ setRadius(): float
+ setSide(): float
+ getArea(): float
+ getArea(): float
side2 38
length x width  x radius2
FURTHER CLASS CONCEPTS
ABSTRACT CLASS
• An incomplete class that cannot be instantiated.

39
A generalization hierarchy

77
78
Class diagram Example

79

You might also like