+ All Categories
Home > Documents > OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and...

OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and...

Date post: 26-Jul-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
33
OO Paradigm and UML Version 2.2 March 2018 © Maurizio Morisio, Marco Torchiano, 2018 Object Oriented Programming http://softeng.polito.it/courses/09CBI Licensing Note This work is licensed under the Creative Commons Attribution- NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ . You are free: to copy, distribute, display, and perform the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Non-commercial. You may not use this work for commercial purposes. No Derivative Works. You may not alter, transform, or build upon this work. § For any reuse or distribution, you must make clear to others the license terms of this work. § Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.
Transcript
Page 1: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

OO Paradigm and UML

Version 2.2 � March 2018 © Maurizio Morisio, Marco Torchiano, 2018

Object Oriented Programming

http://softeng.polito.it/courses/09CBI

2

Licensing Note This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. You are free: to copy, distribute, display, and perform the work

Under the following conditions: §  Attribution. You must attribute the work in the manner specified by

the author or licensor.

§  Non-commercial. You may not use this work for commercial purposes.

§  No Derivative Works. You may not alter, transform, or build upon this work.

§  For any reuse or distribution, you must make clear to others the license terms of this work.

§  Any of these conditions can be waived if you get permission from the copyright holder.

Your fair use and other rights are in no way affected by the above.

Page 2: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

3

Programming paradigms §  Procedural (Pascal, C,…) §  Object-Oriented (C++, Java, C#,…) §  Functional (LISP, Haskell, SQL,…) §  Logic (Prolog)

4

Languages timeline

�60 �70 �80 �90

1st gen HLL

FORTRAN COBOL

ALGOL60

2nd gen HLL

ALGOL68 PASCAL

C

3rd gen HLL ADA

MODULA2

SIMULA67 SMALLTALK C++

EIFFEL ADA9X

Python JAVA

Procedural languages

Object-oriented languages

Global variables

Data structures

Structured prog Types Blocks

Modules/ADT Concurrency Exceptions

�00

C#

Page 3: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

5

Procedural int vect[20]; void sort() { /* sort */ } int search(int n){ /* search */ } void init() { /* init */ } // … int i; void main(){ init(); sort(); search(13); }

6

Modules and relationships

Modules: Data

Function (Procedure)

Relationships Call

Read/write

20

int v[20]

main()

sort() search() init()

Page 4: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

7

Problems §  There is no syntactic relationship between:

w Vectors ( int vect[20] ) w Operations on vectors (search, sort, init)

§  There is no control over size: for (i=0; i<=20; i++){ vect[i ]=0; }; §  Initialization

w  Actually performed?

8

The vector

§  It�s not possible to consider a vector as a primitive and modular concept

§  Data and functions cannot be modularized properly

20

int v[20]

main()

sort() search() init() vector

Page 5: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

9

Procedural - problems

§  No constraints on read/write relationships

§  External functions can read/write vector�s data

foo()

20

int v[20]

main()

sort() search() init() vector

Procedural - In the long run

§  (All) functions may read/write (all) data §  Evolution leads to a growing number of

relationships, source code becomes difficult to understand and maintain w Problem known as “Spaghetti code”

foo()

20

int v[20]

main()

sort() search() init() vector foo() foo()

Page 6: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

11

What is OO? §  Procedural Paradigm

w  Program defines data and then calls subprograms acting on data

§  OO Paradigm w  Program creates objects that encapsulate both

the data and the procedures operating on data

§  OO is simply a new way of organizing a program w  Cannot do anything using OO that can’t be done

using procedural paradigm

12

Why OO? §  Programs are getting too large to be fully

comprehensible by any person §  There is a need for a way of managing

very-large projects §  Object Oriented paradigm allows:

w  programmers to (re)use large blocks of code w without knowing all the picture

§  OO makes code reuse a real possibility §  OO simplifies maintenance and evolution

Page 7: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

13

Why OO? §  Benefits only occur in larger programs §  Analogous to structured programming

w Programs < 30 lines, spaghetti is as understandable and faster to write than structured

w Programs > 1000 lines, spaghetti is incomprehensible, probably doesn’t work, not maintainable

§  Only programs > 1000 lines benefit from OO really

14

An engineering approach

§  Given a system, with components and relationships among them, we have to: w  Identify the components w Define component interfaces w Define how components interact with each

other through their interfaces w Minimize relationships among components

Page 8: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

15

Object-Oriented Design

§  Objects introduce an additional aggregation construct

§  More complex system can be built

16

Procedural vs. OO

Main Program

Data

Subprogram #1 Subprogram

#2

Subprogram #3 Subprogram

#4

Subprogram #5

Procedural

Main Program

Object #3

Data

Object #2

Data

Object #1

Data Object Oriented

Page 9: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

17

Object-Oriented approach

§  Defines a new component type w Object (and class) w Both data and functions accessing it are

within the same module w Allows defining a more precise interface

§  Defines a new kind of relationship w Message passing w Read/write operations are limited to the

same object scope

Classification of OO languages §  Object-Based (Ada)

w Specific constructs to manage objects §  Class-Based (CLU)

w + each object belongs to a class §  Object-Oriented (Simula, Python)

w + classes support inheritance §  Strongly-Typed O-O (C++, Java)

w + the language is strongly typed

Page 10: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

UML §  Unified Modeling Language §  Standardized modeling and

specification language §  Defined by the Object Management Group (OMG)

§  Graphical notation to specify, visualize, construct and document an object-oriented system

§  Integrates the concepts of Booch, OMT and OOSE, and merges them into a single, common and widely used modeling language

UML §  Several diagrams

w Class diagrams w Activity diagrams w Use Case diagrams w Sequence diagrams w Statecharts

Page 11: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

UML Class Diagram § Captures

w Main (abstract) concepts w Characteristics of the concepts

– Data associated to the concepts w Relationships between concepts w Behavior of classes

Abstraction levels

Abstract

Concept Entity Class Category Type

Concrete

Instance Item Object Example Occurrence

Page 12: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Class §  Represents a set of objects

w Common properties w Autonomous existence. w E.g. facts, things, people

§  An instance of a class is an object of the type that the class represents. w  In an application for a commercial

organization CITY, DEPARTMENT, EMPLOYEE, PURCHASE and SALE are typical classes.

Class - Examples Class Diagram0 2013/10/03 powered by Astah

pkg

Employee

Department

City

Sale

Page 13: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Object §  Model of a physical or logical item

w ex.: a student, an exam, a window §  Characterized by

w  identity w attributes (or data or properties or status) w operations it can perform (behavior) w messages it can receive

Object

Communication Diagram1 2014/03/05 powered by Astah

Communication Diagram1sd

John : Employee

DAUIN : Department

Page 14: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Class and Object §  Class (the description of object

structure, i.e. type): w Data (ATTRIBUTES or FIELDS) w Functions (METHODS or OPERATIONS) w Creation methods (CONSTRUCTORS)

§  Object (class instance) w State and identity

27

Class and object §  A class is a type definition

w  Typically no memory is allocated until an object is created from the class

§  The creation of an object is called instantiation. The created object is often called an instance

§  There is no limit to the number of objects that can be created from a class

§  Each object is independent. Interacting with one object doesn't affect the others

Page 15: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Classes and objects

Class Diagram0 2013/10/03 powered by Astah

pkg

Employee

Department

City

Sale

Communication Diagram1 2014/03/05 powered by Astah

Communication Diagram1sd

John : Employee

DAUIN : Department

Attribute §  Elementary property of classes

w Name w Type

§  An attribute associates to each object (occurrence of a class) a value of the corresponding type w Name: String w  ID: Numeric w Salary: Currency

Page 16: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Attribute – Example

31

4a-Attributes 2017/03/09 powered by Astah

pkg

- Year : int- Code : String

Course

- Inhabitants : int- Name : String

City- Salary : Currency

Employee

Method §  Describes an operation that can be

performed on an object w Name w Parameters

§  Similar to functions in procedural languages

§  It represent the means to operate on or access to the attributes

Page 17: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Method - Example Class Diagram0 2014/03/06 powered by Astah

pkg

+ getSalary() : double+ printName() : void

- salary : double- name : String- ID : int

Employee

Message passing §  Objects communicate by message

passing w Not by direct access to object’s local data

§  A message is a service request

Note: this is an abstract view that is independent from specific programming languages.

Page 18: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Messages Communication Diagram0 2014/03/05 powered by Astah

Communication Diagram0sd

DAUIN : Department

Jane : Employee

John : Employee

1: printName()()

2: printName()

Interface §  Set of messages an object can receive

w Each message is mapped to an internal “function” within the object

w The object is responsible for the association (message à function)

w Any other message is illegal §  The interface

w Encapsulates the internals w Exposes a standard boundary

Page 19: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Interface §  The interface of an object is simply the

subset of methods that other �program parts� are allowed to call w Stable

37

Interface

Rest of the

Program

method

method

method

method

method

method

method

Fields of The Object

Other parts

Object

Encapsulation §  Simplified access

w To use an object, the user need only comprehend the interface. No knowledge of the internals are necessary

§  Self-contained. w Once the interface is defined, the

programmer can implement the interface (write the object) without interference of others

38

Page 20: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Encapsulation §  Ease of evolution

w  Implementation can change at a later time without rewriting any other part of the program (as long as the interface doesn't change)

§  Single point of change w Any change in the data structure means

modifying the code in one location, rather than code scattered around the program (error prone)

39

Association §  Represents a logical link between two

classes. §  An occurrence of an association is a

pair made up of the occurrences of the entities, one for each involved class w Residence is an association between the

classes City and Employee; w Exam is an association between the

classes Student and Course.

Page 21: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Associations Class

Student Class

Course

Link between objects

Association between classes

Association - Examples 2-Associations 2013/10/03 powered by Astah

Student CourseAttend

Employee City

Works_in

Residence

Page 22: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Recursive association-Samples 3.a-Recursive associations 2013/10/04 powered by Astah

pkg

Employee

Friend

- employee

- manager

Supervise

Student

Link §  Model of association between objects

Page 23: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Attribute – Example

Is everything ok?

Multiplicity - Example

0..4

Min Max

A car can mount none, up to four wheels

Page 24: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Multiplicity - Example

0..1

A wheel can be mounted on none or

at most one car

Multiplicity §  Typically, only three values are used:

0, 1 and the symbol * (many) §  Minimum: 0 or 1

w 0 means the participation is optional, w 1 means the participation is mandatory;

§  Maximum: 1 or * w 1: each object is involved in at most one

link w *: each object is involved in many links

Page 25: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Multiplicity

n Exactly n

* Zero or more

0..1 Zero or one (optional)

m..n Between m and n (m,n included)

m..* From m up

Multiplicity 3-Multiplicity 2013/10/04 powered by Astah

Order Invoice

0..11Sale

Person City

10..*Residence

Tourist Trip

0..*1..*Reservation

Page 26: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Aggregation §  B is-part-of A means that objects

described by class B can be attributes of objects described by A

A B

Example Car

Engine

power

CD player

Tyre

1 4

1

Page 27: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Association Class §  The association class define the

attributes related to the association §  A link between two object includes

w The two linked objects w The attributes defined by the association

class

Association class - Equivalence

Page 28: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Association Class Limitations §  Association class

w Fee is a function of consultant and company

w fee ( Consultant , Company )

§  Intermediate class w Fee is a function of the contract w fee ( Contract )

Association class limitation §  Case

w Consultant working several time for the same Company

§  Cannot be represented by association class

§  Only representable through intermediate class

Page 29: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

57

Inheritance §  A class can be a sub-type of another class §  The inheriting class contains all the

methods and fields of the class it inherited from plus any methods and fields it defines

§  The inheriting class can override the definition of existing methods by providing its own implementation

§  The code of the inheriting class consists only of the changes and additions to the base class

Specialization / Generalization §  B specializes A means that objects

described by B have the same properties of objects described by A

§  Objects described by B may have additional properties

§  B is a special case of A §  A is a generalization of B (and possible

other classes)

Page 30: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Generalization

60

Inheritance terminology

§  Class one above w Parent class

§  Class one below w Child class

§  Class one or more above w Superclass, Ancestor class, Base class

§  Class one or more below w Subclass, Descendent class, Derived class

Page 31: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

Set-Specialization Inheritance 2015/07/22 powered by Astah

- SSN : String- Last : String- First : String

Person

- ID : intStudent

- Salary : CurrencyEmployee

Person Employee

Student

62

Why inheritance §  Frequently, a class is merely a modification

of another class. In this way, there is minimal repetition of the same code

§  Localization of code w  Fixing a bug in the base class automatically

fixes it in the subclasses w  Adding functionality in the base class

automatically adds it in the subclasses w  Less chances of different (and inconsistent)

implementations of the same operation

Page 32: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

63

Example of inheritance tree

Animal

salesman

Living species

vegetal

Flower

Human being

Flower seller

Customer

High DIT make code hard to understand

DIT

Conceptual model quality §  Correctness

w No requirement is misrepresented §  Completeness

w All requirements are represented §  Readability

w  It is easy to read and understand §  Minimality

w There are no avoidable elements

Page 33: OO and UML - PoliTo · 2018-06-07 · § Unified Modeling Language § Standardized modeling and specification language § Defined by the Object Management Group (OMG) § Graphical

References §  Fowler, M. “UML Distilled: A Brief

Guide to the Standard Object Modeling Language - 3rded.”, Addison-Wesley Professional (2003)


Recommended