Object-oriented Design
Trang 1©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 1
Object-oriented Design
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 2
Objectives
represented as a set of interacting objects that manage their own state and operations
object-oriented design process
used to describe an object-oriented design
represent these models
Topics covered
Trang 2©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 4
Object-oriented development
are related but distinct
of the application domain
object-oriented system model to implement requirements
OO programming language such as Java or C++
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 5
Characteristics of OOD
entities and manage themselves
representation information
services
communicate by message passing
sequentially or in parallel
Interacting objects
state o3 o3:C3
state o4 o4: C4 state o1
o1: C1
state o6 o6: C1
state o5 o5:C5 state o2
o2: C3
ops1() ops3 () ops4 ()
ops3 () ops1 () ops5 ()
Trang 3©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 7
Advantages of OOD
understood as stand-alone entities.
mapping from real world entities to system objects.
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 8
Objects and object classes
which represent instances of real-world and system entities.
They may be used to create objects.
services from other object classes.
Objects and object classes
An object is an entity that has a state and a defined set of
operations which operate on that state The state is represented as a set of object attributes The operations associated with the object 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 It includes declarations of all the attributes and services which should be associated with an object of that class.
Trang 4©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 10
The Unified Modeling Language
object-oriented designs were proposed in the 1980s and 1990s
these notations
models that may be produced during OO analysis and design
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 11
Employee object class (UML)
Emplo yee name: string
address: string
dateOfBir th: Date
employeeNo: integer
socialSecurityNo: string
depar tment: Dept
manager: Employee
salar y: integer
status: {current, left, retired}
taxCode: integer
.
join ()
leave ()
changeDetails ()
Object communication
message passing
• The name of the service requested by the calling object;
• Copies of the information required to execute the service and the name of a holder for the result of the service
by procedure calls
• Information = parameter list
Trang 5©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 13
Message examples
// Call a method associated with a buffer
// in the buffer
v = circularBuffer.Get () ;
// temperature to be maintained
thermostat.setTemp (20) ;
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 14
Generalisation and inheritance
attribute types and operations
where one class (a super-class) is a generalisation
of one or more other classes (sub-classes)
operations from its super class and may add new methods or attributes of its own
inheritance in OO programming languages
A generalisation hierarchy
Emplo yee
Prog rammer project progLanguages Mana ger
Project
Mana ger
budgetsControlled
dateAppointed
projects
Dept.
Mana ger
Strateg ic Mana ger dept responsibilities
Trang 6©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 16
Advantages of inheritance
used to classify entities.
and the programming level.
organisational knowledge about domains and systems.
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 17
Problems with inheritance
cannot be understood without reference to their super-classes.
inheritance graph created during analysis Can lead to significant inefficiency.
and implementation have different functions and should be separately maintained.
UML associations
relationships with other objects and object classes
by an association
describes the association
attribute of an object is an associated object or that
a method relies on an associated object
Trang 7©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 19
An association model
Employee Depar tment
Manager
is-member-of is-managed-by
manages
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 20
Concurrent objects
entities make them suitable for concurrent implementation.
communication can be implemented directly
if objects are running on separate processors
in a distributed system.
Servers and active objects
• The object is implemented as a parallel process (server) with entry points corresponding to object operations If no calls are made to it, the object suspends itself and waits for further requests for service
• Objects are implemented as parallel processes and the internal object state may be changed by the object itself and not simply by external calls
Trang 8©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 22
Active transponder object
modified by operations but may also update them autonomously using internal
operations.
aircraft’s position The position may be updated using a satellite positioning system The object periodically update the position by triangulation from satellites.
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 23
An active transponder object
class Transponder extends Thread {
Position currentPosition ;
Coords c1, c2 ;
Satellite sat1, sat2 ;
Navigator theNavigator ;
public Position givePosition ()
{
return currentPosition ;
}
public void run ()
{
while (true)
{
c1 = sat1.position () ; currentPosition = theNavigator.compute (c1, c2) ; }
}
} //Transponder
Java threads
implementing concurrent objects.
and this is started up by the Java run-time system.
loop so that they are always carrying out the computation.
Trang 9©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 25
An object-oriented design process
developing a number of different system models.
and maintenance of these models and, for small systems, this may not be cost-effective.
different groups design models are an essential communication mechanism.
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 26
Process stages
any proprietary process such as the RUP.
system;
Weather system description
A weather mapping system is required to generate weather maps on a regular basis using data collected from remote, unattended weather stations and other data sources such as weather observers, balloons and satellites Weather stations transmit their data to the area computer in response to a request from that machine
The area computer system validates the collected data and integrates it with the data from different sources The integrated data is archived and, using data from this archive and a digitised map database a set of local weather maps is created Maps may be printed for distribution on a special-purpose map printer or may be displayed in a number of different formats
Trang 10©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 28
System context and models of use
between the software being designed and its external environment
• A static model that describes other systems in the environment Use a subsystem model to show other systems Following slide shows the systems around the weather station system
• A dynamic model that describes how the system interacts with its environment Use use-cases to show interactions
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 29
Layered architecture
«subsystem»
Data collection
«subsystem»
Data processing
«subsystem»
Data archiving
«subsystem»
Data collection layer where objects are concerned with acquiring data from remote sources
Dataprocessinglayerwhereobjects are concerned with checking and integ rating the collected data
Data archiving layer where objects areconcernedwithstoringthedata for future processing
Datadisplaylayerwhereobjectsare concerned with preparing and presenting the data in a human-readable form
Subsystems in the weather mapping system
Data storage
User inter face
«subsystem»
Data collection
«subsystem»
Data processing
«subsystem»
Data archiving
«subsystem»
Weather
station
Satellite
Comms
Balloon Observer
Map store Data store
Data storage
Map
User inter face
Map display Map printer
Data
checking
Data
integ ration
Trang 11©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 31
Use-case models
interaction with the system.
features as ellipses and the interacting entity
as a stick figure.
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 32
Use-cases for the weather station
Star tup
Shutdown
Repor t
Calibrate
Test
Use-case description
System Weather station
Use-case Report
Actors Weather data collection system, Weather station
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 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 summarised 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.
Trang 12©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 34
Architectural design
environment have been understood, you use this information for designing the system architecture
appropriate for the weather station
• Interface layer for handling communications;
• Data collection layer for managing instruments;
• Instruments layer for collecting data
an architectural model
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 35
Weather station architecture
Weather station
Manages all external communications
Collects and summarises weather data
Package of instruments for raw data collections
«subsystem»
Data collection
«subsystem»
Instruments
«subsystem»
Inter face
Object identification
most difficult part of object oriented design.
identification It relies on the skill, experience and domain knowledge of system designers.
You are unlikely to get it right first time.
Trang 13©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 37
Approaches to identification
language description of the system (used in Hood OOD method)
application domain
based on what participates in what behaviour
attributes and methods in each scenario are identified
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 38
Weather station description
A weather station is a package of software controlled instruments which collects data, performs some data processing and transmits this data for further processing The instruments include air and ground thermometers, an anemometer, a wind vane, a barometer and a rain gauge Data is collected periodically
When a command is issued to transmit the weather data, the weather station processes and summarises the collected data The summarised data is transmitted to the mapping computer when a request is received
Weather station object classes
• Application domain objects that are ‘hardware’ objects related to the instruments in the system
• The basic interface of the weather station to its environment It therefore reflects the interactions identified in the use-case model
• Encapsulates the summarised data from the instruments
Trang 14©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 40
Weather station object classes
identifier
repor tWeather ()
calibrate (instruments)
test ()
star tup (instruments)
shutdown (instruments)
WeatherStation
test ()
calibr ate ()
Gr ound
thermomet er
temper ature
Anemomet er windSpeed windDirection test ()
Bar omet er pressure height test () calibr ate ()
WeatherData airTemper atures groundT emper atures windSpeeds windDirections pressures rainf all collect () summarise ()
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 41
Further objects and object refinement
operations
• Weather stations should have a unique identifier;
• Weather stations are remotely situated so instrument failures have to be reported automatically Therefore attributes and operations for self-checking are required
• In this case, objects are passive and collect data on request rather than autonomously This introduces flexibility at the expense of controller processing time
Design models
classes and relationships between these entities.
the system in terms of object classes and relationships.
interactions between objects.
Trang 15©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 43
Examples of design models
objects into coherent subsystems
interactions
objects change their state in response to events
models, generalisation models, etc
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 44
Subsystem models
logically related groups of objects.
packages - an encapsulation construct This
is a logical model The actual organisation of objects in the system may be different.
Weather station subsystems
«subsystem»
Inter face
«subsystem»
Data collection
CommsController
WeatherStation
WeatherData
Instrument Status
«subsystem»
Instruments
Air
thermometer
Ground
thermometer
RainGauge
Barometer Anemometer
WindVane
Trang 16©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 46
Sequence models
object interactions that take place
top;
read top to bottom;
Different styles of arrow represent different types of interaction;
the time when the object is the controlling object
in the system
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 47
Data collection sequence
:CommsController
request (repor t)
acknowledge ()
repor t ()
summarise ()
reply (repor t)
acknowledge ()
send (repor t) :WeatherStation :WeatherData
Statecharts
requests and the state transitions triggered by these requests
• If object state is Shutdown then it responds to a Startup() message;
• In the waiting state the object is waiting for further messages;
• If reportWeather () then system moves to summarising state;
• If calibrate () the system moves to a calibrating state;
• A collecting state is entered when a clock signal is received
Trang 17©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 49
Weather station state diagram
transmission done
calibrate ()
test () star tup ()
shutdown ()
calibration OK
test complete
weather summary complete
clock collection done Operation
repor tWeather ()
Transmitting
Collecting Summarising Calibrating
©Ian Sommerville 2004 Software Engineering, 7th edition Chapter 14 Slide 50
Object interface specification
objects and other components can be designed in parallel
representation but should hide this in the object itself
viewpoints on the methods provided
specification but Java may also be used
Weather station interface
interface WeatherStation {
public void WeatherStation () ;
public void startup () ;
public void startup (Instrument i) ;
public void shutdown () ;
public void shutdown (Instrument i) ;
public void reportWeather ( ) ;
public void test () ;
public void test ( Instrument i ) ;
public void calibrate ( Instrument i) ;
public int getID () ;
} //WeatherStation