Thomas Leveque (slides adapted from Ivica Crnkovic … are created according to some object class...

56
MDH/IDT, Thomas Leveque© Page 1, 9/20/2010 SE course, Object Oriented Design Thomas Leveque (slides adapted from Ivica Crnkovic and Rikard Land) Mälardalen University Department of Computer Engineering [email protected]

Transcript of Thomas Leveque (slides adapted from Ivica Crnkovic … are created according to some object class...

MDH/IDT, Thomas Leveque© Page 1, 9/20/2010SE course, Object Oriented Design

Thomas Leveque

(slides adapted from Ivica Crnkovic and Rikard Land)

Mälardalen University

Department of Computer Engineering

[email protected]

HistoryHistory

Function Object Component

time1980 1990 2000

MDH/IDT, Thomas Leveque© Page 2, 9/20/2010SE course, Course Overview

Characteristics of OODCharacteristics of OOD

� Designing systems using self-contained objects and object classes

� Objects are abstractions of real-world or system entities and manage themselves

� Objects are independent and encapsulate state and representation information.

� System functionality is expressed in terms of object services

MDH/IDT, Thomas Leveque© Page 3, 9/20/2010SE course, Course Overview

� System functionality is expressed in terms of object services

� Shared data areas are eliminated. Objects communicate by message passing

� Objects may be distributed and may execute sequentially or in parallel

OOD structureOOD structure

changeNationality(…)

nationality= french

firstName = Thomas

Thomas:Customer

lastName = Leveque

id= xxx

changeMaritalStatus(…)

addMoney(…)

number = 123

123:Account

balance = 200

currency = EUR

removeMoney(…)

MDH/IDT, Thomas Leveque© Page 4, 9/20/2010SE course, Course Overview

A system is designed as a number of interacting objects

changeNationality(…)

nationality= swedish

firstName = Jan

Jan:Customer

lastName = Carlson

id= yyy

changeMaritalStatus(…)

addMoney(…)

number = 234

234:Account

balance = 3000

currency = SEK

removeMoney(…)

Advantages of OODAdvantages of OOD

� (Potential for) High cohesion, low coupling

� Easier maintenance

� Objects may be understood as stand-alone, communicating, entities

� Potential for reuse

MDH/IDT, Thomas Leveque© Page 5, 9/20/2010SE course, Course Overview

� Typically a set of classes in a specific relation are reusable as a…

�Framework. A customizable set (often distributed as source code)

�Component. A fixed set (often distributed as binary)

� For some systems, there may be an obvious mapping from real world entities to system objects

ObjectObject--oriented developmentoriented development

� Object-oriented analysis, design and programming are related but distinct

� OOA is concerned with developing an object model of the application domain

� OOD is concerned with developing an object-oriented system model to implement requirements

� OOP is concerned with realizing an OOD using an OO

MDH/IDT, Thomas Leveque© Page 6, 9/20/2010SE course, Course Overview

� OOP is concerned with realizing an OOD using an OO programming language such as C++ or Java

OO Design Methods OO Design Methods –– Commonalities Commonalities

� The identification of objects, their attributes and services

� The organization of objects into an aggregation hierarchy

� The construction of dynamic object-use descriptions which show how services are used

� The specification of object interfaces

MDH/IDT, Thomas Leveque© Page 7, 9/20/2010SE course, Course Overview

Objects, classes and inheritanceObjects, classes and inheritance

� Objects are entities in a software system which represent instances of real-world and system entities

� Object classes are templates for objects. They may be used to create objects

� Object classes may inherit attributes and services from other object classes

MDH/IDT, Thomas Leveque© Page 8, 9/20/2010SE course, Course Overview

ObjectsObjects

� An object is an entity which has:

� A state - The state is represented as a set of object attributes.

� A defined set of operations - The operations associated with the object provide services to other objects (clients)

changeNationality(String)

nationality : Nationality

firstName : String

Customer

lastName : String

id : String

changeLastName(String)

MDH/IDT, Thomas Leveque© Page 9, 9/20/2010SE course, Course Overview

provide services to other objects (clients) which request these services when some computation is required.

� Objects are created according to some object class definition. An object class definition serves as a template for objects.

UML Notation of a class

swedish

Nationality

french

english

<<enumeration>>

Object communicationObject communication

� Conceptually, objects communicate by message passing.

� The message is the invocation of the method plus the parameters

� In practice, messages are often implemented by procedure calls

� Name = Object.procedure_name

� Information = parameter list

� Communication

MDH/IDT, Thomas Leveque© Page 10, 9/20/2010SE course, Course Overview

� Communication

� Synchronous (by default)

� Asynchronous – if objects are implemented as concurrent processes or threads (more complicated!)

Inheritance (generalization)Inheritance (generalization)

� Objects are members of classes which define attribute types and operations

� Classes may be arranged in a class hierarchy where one class is derived from an existing class (super-class)

� A sub-class inherits the

BusinessAccount

Account

PersonalAccount

MDH/IDT, Thomas Leveque© Page 11, 9/20/2010SE course, Course Overview

� A sub-class inherits the attributes and operations from its super class and may add new methods or attributes of its own

Account

SavingAccount

rate: float

Account

Multiple inheritanceMultiple inheritance

� Properties from several super-classes are inherited

� Problems?

Employee

Customer Employee

op1()

a: A

op2()

b: B

MDH/IDT, Thomas Leveque© Page 12, 9/20/2010SE course, Course Overview

Employee Customer

op3()

c: C Employee Customer

op3()

c: C

op1()

a: A

op2()

b: B

Characteristics of inheritanceCharacteristics of inheritance

� Advantages

� It is an abstraction mechanism which may be used to classify entities

� It is a reuse mechanism at both the design and the programming level (reusability can be improved)

� The inheritance graph is a source of organizational knowledge about domains and systems

� Problems

MDH/IDT, Thomas Leveque© Page 13, 9/20/2010SE course, Course Overview

� Problems

� Object classes are not self-contained, they cannot be understood without reference to their super-classes

�Extra problematic with multiple inheritance

� Changes in super-classes may have problematic effects on sub-classes

� (Thus, the use of inheritance may decrease maintainability and reusability)

Association between objectsAssociation between objects

MDH/IDT, Thomas Leveque© Page 14, 9/20/2010SE course, Course Overview

� Relations:

� Modifying source object may have an impact on the destination

� An attribute of an object is an associated object

� The implementation of an object method relies on the associated object

Concurrent objectsConcurrent objects

� Conceptually, an object requests a service from another object by sending a ‘service request’ message to that object.

� There is no requirement for serial execution where one object waits for completion of a requested service.

� Consequently, the general model of object interaction allows objects to execute concurrently as parallel processes.

MDH/IDT, Thomas Leveque© Page 15, 9/20/2010SE course, Course Overview

� Mechanisms for concurrent execution

� Threads

� Processes

Concurrent objects implementationsConcurrent objects implementations

� Servers

� Methods start executing in response to an external message

� Execute in parallel with methods associated with other objects.

� The object suspends itself and waits for further requests for service.

� Active objects

MDH/IDT, Thomas Leveque© Page 16, 9/20/2010SE course, Course Overview

� The state of the object may be changed by internal operations executing within the object itself.

� The process representing the object continually executes these operations so never suspends itself.

� Could be implemented an infinite loop invoking various methods at a (predefined or dynamic) order.

� Could be implemented as being trigged by a timer (but should we perhaps call them “servers” then?)

Active object exampleActive object example

MDH/IDT, Thomas Leveque© Page 17, 9/20/2010SE course, Course Overview

Object identificationObject identification

� We actually identify object classes

� Identifying objects is the most difficult part of object oriented design.

� There is no 'magic formula' for object identification. It relies on the skill, experience and domain knowledge of system designers.

� Object identification is an iterative process. You are unlikely to get it right first time

MDH/IDT, Thomas Leveque© Page 18, 9/20/2010SE course, Course Overview

get it right first time

� Whenever it is possible, add a global unique id.

Approaches to identificationApproaches to identification

� Use a grammatical approach based on a natural language description of the system (used in Hood method)

� Base the identification on tangible things in the application domain (roles, locations, …)

� Use a behavioral approach - understanding overall behavior and then refine it.

� Use a scenario-based analysis – use cases

MDH/IDT, Thomas Leveque© Page 19, 9/20/2010SE course, Course Overview

� Use a scenario-based analysis – use cases

Objects and operationsObjects and operations

� Nouns in the description give pointers to objects in the system

� Verbs give pointers to operations associated with objects

� Approach assumes that the designer has a common sense knowledge of the application domain as not all objects and services are likely to be mentioned in the description

MDH/IDT, Thomas Leveque© Page 20, 9/20/2010SE course, Course Overview

MDH/IDT, Thomas Leveque© Page 21, 9/20/2010SE course, Design Example

Thomas Leveque

(example from Philippe Lalanda)

Mälardalen University

Department of Computer Engineering

[email protected]

RD2010 system descriptionRD2010 system description

ALLVideo company which rents films on Internet, wants to provide DVD automates called RD2010. RD2010 can contain 100 DVD. RD2010 software can be updated from ALLVideo offices. The user can• List available films. For each film, read its title, picture and main actors.• Rent one or many films.• Ask one or many films.• Add money to his subscription card.• Consult information about his account.

MDH/IDT, Thomas Leveque© Page 22, 9/20/2010SE course, Course Overview

• Consult information about his account.Rent a film should be done in less than 10 minutes.

RD2010 owners can perform special activities by typing a secret code:• Add/remove one or many DVD.• Get information about the RD2010 (average of available films, broken DVD number …).• List customer demands.• Define DVD price.

RD2010 system descriptionRD2010 system description

A customer can subscribe to one or many subscriptions. Each subscription is materialized as a card which is delivered by RD2010 owner. A subscription enable discounts and is unlimited in time. A copy of the card can be asked in order to have one card by family member.Subscriber can ask to limit access to children. History of rented films is kept.A DVD can be rented either with a subscription card, either with a credit card. We can rent only one film with a credit card and up to 3 DVD with a subscription card.

MDH/IDT, Thomas Leveque© Page 23, 9/20/2010SE course, Course Overview

Your job is to develop RD2010 software.

ProcessProcess

� Context diagram

� Use Case diagram

� Use case description

� Domain model

� System Architeture

� Components

MDH/IDT, Thomas Leveque© Page 24, 9/20/2010SE course, Course Overview

� Components

� Deployment

� Design Class diagram

� Implementation Class diagram

RD2010 Software

Context DiagramContext Diagram

Customer

Bank

RD2010 Owner

DVD Rental

Payment

Administration

Sys

MDH/IDT, Thomas Leveque©

RD2010 Software

Page 25, 9/20/2010SE course, Course Overview

Subscriber

RD2010

Technician

CommandsSubscriptions

Configuration

Use Case DiagramUse Case Diagram

Customer RD2010 Owner

RentADVD

ReturnADVD

Add DVD

RemoveDVD

Set priceList films

MDH/IDT, Thomas Leveque© Page 26, 9/20/2010SE course, Course Overview

Subscriber Technician

UpdatesAddMoneyToHisCard

Modify Subsciption

Ask film

System ArchitectureSystem Architecture

HCICOMTechnician

Customer/Subscriber RD2010 Owner

MDH/IDT, Thomas Leveque© Page 27, 9/20/2010SE course, Course Overview

RD2010

ConfigurationRentManagement

SubscriptionManagement

TransactionManagement

Bank

RD2010

Component DescriptionComponent Description

� HCI

� …

� Subscription Management

� …

� Configuration

� Allow to update the software.

MDH/IDT, Thomas Leveque© Page 28, 9/20/2010SE course, Course Overview

� Allow to update the software.

� Rent Management

� …

� RD2010

� Transform high level orders to RD2010 commands.

� Transaction Management

� Perform payments.

System Architecture (Deployment)System Architecture (Deployment)

Rent Management

Subscription Management

Transaction Management

MDH/IDT, Thomas Leveque© Page 29, 9/20/2010SE course, Course Overview

RD2010

Configuration

COM HCI

Transaction Management

RD2010

?

State diagramState diagram

NewDVD

Available

MDH/IDT, Thomas Leveque© Page 30, 9/20/2010SE course, Course Overview

LostOut

rentDVDreturnDVD

Broken

notReturned30DaysbrokenNotif

Weather system descriptionWeather system description

A weather data collection system is required to generate weather maps on a

regular basis using data collected from remote, unattended weather stations.

Each weather station collects meteorological data over a period and produces

summaries of that data. On request, it sends the collected, processed

information to an area computer for further processing. Data on the air

temperature, the ground temperature, the wind speed and direction, the

barometric pressure and the amount of rainfall is collected by each weather

MDH/IDT, Thomas Leveque© Page 31, 9/20/2010SE course, Course Overview

barometric pressure and the amount of rainfall is collected by each weather

station.

Weather stations transmit their data to the area computer in response to a

request from that machine. The area computer collates the collected data and

integrates it with reports from other sources such as satellites and ships. Using

a digitized map database it then generates a set of local weather maps.

Weather system descriptionWeather system description

A weather data collection system is required to generate weather maps on a

regular basis using data collected from remote, unattended weather stations.

Each weather station collects meteorological data over a period and produces

summaries of that data. On request, it sends the collected, processed

information to an area computer for further processing. Data on the air

temperature, the ground temperature, the wind speed and direction, the

barometric pressure and the amount of rainfall is collected by each weather

MDH/IDT, Thomas Leveque© Page 32, 9/20/2010SE course, Course Overview

barometric pressure and the amount of rainfall is collected by each weather

station.

Weather stations transmit their data to the area computer in response to a

request from that machine. The area computer collates the collected data and

integrates it with reports from other sources such as satellites and ships. Using

a digitized map database it then generates a set of local weather maps.

The general analysis/design processThe general analysis/design process

1. Understand and define the context and the modes of use of the

system

2. Design the system architecture

3. Identify the principal objects in the system

MDH/IDT, Thomas Leveque© Page 33, 9/20/2010SE course, Course Overview

3. Identify the principal objects in the system

4. Develop design of subsystems (principal objects)

5. Specify object interfaces

System Architecture System Architecture -- a layered structurea layered structure

MDH/IDT, Thomas Leveque© Page 34, 9/20/2010SE course, Course Overview

System ArchitectureSystem Architecture

MDH/IDT, Thomas Leveque© Page 35, 9/20/2010SE course, Course Overview

= Data flow

Packages in the weather mapping systemPackages in the weather mapping system

MDH/IDT, Thomas Leveque© Page 36, 9/20/2010SE course, Course Overview

System context and models of useSystem context and models of use

Two complementary models:

� The system context is a static model that describes the other

systems in that environment.

� The model of the system use is a dynamic model that describes

how the system actually interacts with its environment.

MDH/IDT, Thomas Leveque© Page 37, 9/20/2010SE course, Course Overview

how the system actually interacts with its environment.

Weather stationWeather station Analysis/design process1. Use Case2. Architectural design3. OO analysis4. OO design

MDH/IDT, Thomas Leveque© Page 38, 9/20/2010SE course, Course Overview

Use case for weather stationUse case for weather station

� Weather station - Package of instruments which collects data, performs some processing and transmits the data for further processing

� Use case

MDH/IDT, Thomas Leveque© Page 39, 9/20/2010SE course, Course Overview

“Actor”- from themain station

A case descriptionA case description

System Weather station

Use-case Report

Actors Weather data collection system

Data The weather station sends a summary of the weather data that has been collected from the instruments in the collection period to the weather data collection system. The data sent are the maximum, minimum and average ground and air temperatures, the maximum, minimum and average air pressures, the maximum, minimum and average wind speeds, the total rainfall

MDH/IDT, Thomas Leveque© Page 40, 9/20/2010SE course, Course Overview

maximum, minimum and average wind speeds, the total rainfall and the wind direction as sampled at 5 minute intervals.

Stimulus The weather data collection system establishes a modem link with the weather station and requests transmission of the data.

Response The summarized data is sent to the weather data collection system

Comments Weather stations are usually asked to report once per hour but this frequency may differ from one station to the other and may be modified in future.

Weather station Weather station -- Architectural designArchitectural design

� Once the interactions between the software system that is being designed and the system’s environment have been defined, you can then use this information as a basis for designing the system architecture.

MDH/IDT, Thomas Leveque© Page 41, 9/20/2010SE course, Course Overview

Weather Station Object identificationWeather Station Object identification

CommsController

requestAcknowledgesendReport

MDH/IDT, Thomas Leveque© Page 42, 9/20/2010SE course, Course Overview

getTemperature()

Dynamic modelDynamic model

� Describes the dynamic structure of the system

� Interactions between subsystems

� UML defines different models

� Sequence models (UML sequence or collaboration diagram)

�Shows interaction between objects

MDH/IDT, Thomas Leveque© Page 43, 9/20/2010SE course, Course Overview

�Shows interaction between objects

� State machine models (UML state chart diagrams)

�shows the behavior of an object in response to different messages

Sequence of operationsSequence of operations

MDH/IDT, Thomas Leveque© Page 44, 9/20/2010SE course, Course Overview

A state diagram for Weather StationA state diagram for Weather Station

MDH/IDT, Thomas Leveque© Page 45, 9/20/2010SE course, Course Overview

Object interface designObject interface design

� Concerned with specifying the detail of the object interfaces. This means defining attribute types and the signatures and semantics of object operations

� Precise specification is essential!

MDH/IDT, Thomas Leveque© Page 46, 9/20/2010SE course, Course Overview

essential!

� IDL = Interface Description Language

Design evolutionDesign evolution

� Advantage of an OOD - simpler to make changes (?)

� Object state representation does not influence the design.

� Changing the internal details of an object is unlikely to affect any other system objects. Furthermore, because objects are loosely coupled, it is usually straightforward to introduce new objects

� Example :

� Assume pollution monitoring facilities are to be added to weather

MDH/IDT, Thomas Leveque© Page 47, 9/20/2010SE course, Course Overview

� Assume pollution monitoring facilities are to be added to weather stations.

� These sample the air and compute the amount of different pollutants in the atmosphere

� Pollution readings are transmitted with weather data

Design Evolution Example Design Evolution Example -- adding pollution monitoring facilityadding pollution monitoring facility

Added

MDH/IDT, Thomas Leveque© Page 48, 9/20/2010SE course, Course Overview

Added

Added

OO analysis/design exampleOO analysis/design example

� Software system at a library.

� The main task

� keeping track of all books and the availability of each book (in library, loaned by … etc).

� Librarian

� add a new book in the system

MDH/IDT, Thomas Leveque© Page 49, 9/20/2010SE course, Course Overview

� Loan books

� I a book is out of loan, it is possible for the librarian to make a reservation for a customer;

� return a book

Analysis/design processAnalysis/design process

� Use case

� Class diagram

� Sequential diagram

� State chart diagram

MDH/IDT, Thomas Leveque© Page 50, 9/20/2010SE course, Course Overview

Use Case Example Use Case Example –– A librarian in a libraryA librarian in a library

AddNewBook

ReserveBook

LibrarianLogin

<<include>>

<<include>>

<<include>>

MDH/IDT, Thomas Leveque© Page 51, 9/20/2010SE course, Course Overview

LoanBook

LibrarianLogin

ReturnBook

<<include>>

Example 2: A library systemExample 2: A library system

Library

bookShelf : Vector

getBookByTitle()

addBook()

browseBooks()

LoginService

librarians : LibrarianType

login()11 11

Book

state : Integer

String

loanBook()

returnBook()

reserveBook()

0..* 10..* 1

title: String

state: Integer

MDH/IDT, Thomas Leveque© Page 52, 9/20/2010SE course, Course Overview

browseBooks()unreserveBook()

getTitle()

Librarian

Example Example –– a librarian loans a booka librarian loans a book

MDH/IDT, Thomas Leveque© Page 53, 9/20/2010SE course, Course Overview

The libraryThe library

NewBook

In stock

unreserveBook

unreserveBook

MDH/IDT, Thomas Leveque© Page 54, 9/20/2010SE course, Course Overview

Reserved and

out of loan

Out of loan

loanBook

returnBook

reserveBook

unreserveBook

Reserved and

in stock

returnBook

loanBook

OO Design OO Design -- key pointskey points

� The fundamental components in the design represent objects with their own private state and operations rather than functions.

� Objects may be implemented sequentially or concurrently. A concurrent object may be:

� A passive object whose state is only changed through its interface

� An active object that can change its own state without outside intervention

MDH/IDT, Thomas Leveque© Page 55, 9/20/2010SE course, Course Overview

intervention

� An important advantage of object-oriented design is its potential to simplify the evolution of the system

� The Unified Modeling Language (UML) has been designed to provide a range of different notations that can be used to document an object-oriented design

OO design OO design -- key pointskey points

� The process of object-oriented design includes activities:

� Design of the system architecture

� Identification of objects in the system

� Description of the design using different object models

� Documentation of the object interfaces

� Different models may be produced during an object-oriented

MDH/IDT, Thomas Leveque© Page 56, 9/20/2010SE course, Course Overview

� Different models may be produced during an object-oriented design process:

� Static models (class models, generalization models, association models)

� Dynamic models (sequence models, state machine models)

� Object interfaces must be defined precisely so that they can be used by other objects.

� IDL = Interface Description Language