Upload
dangnhu
View
213
Download
0
Embed Size (px)
Citation preview
Object-oriented Design
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 1
ObjectivesObjectives
To explain how a software design may be To explain how a software design may berepresented as a set of interacting objects thatmanage their own state and operations
To describe the activities in the object-orienteddesign processT i t d i d l th t b d t To introduce various models that can be used todescribe an object-oriented design
To show how the UML may be used to represent To show how the UML may be used to representthese models
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 2
Topics coveredTopics covered
Objects and object classes Objects and object classes An object-oriented design process
Design evolution Design evolution
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 3
Object-oriented developmentObject oriented development
Object-oriented analysis design and programming are Object oriented analysis, design and programming arerelated but distinct.
OOA is concerned with developing an object model of theapplication domain.
OOD is concerned with developing an object-orientedsystem model to implement requirementssystem model to implement requirements.
OOP is concerned with realising an OOD using an OOprogramming language such as Java or C++.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 4
Characteristics of OODCharacteristics of OOD
Objects are abstractions of real-world or system entities Objects are abstractions of real world or system entitiesand manage themselves.
Objects are independent and encapsulate state andrepresentation information.
System functionality is expressed in terms of objectservicesservices.
Shared data areas are eliminated. Objectscommunicate by message passing.
Objects may be distributed and may executesequentially or in parallel.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 5
Interacting objectsInteracting objects
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 6
Advantages of OODAdvantages of OOD
Easier maintenance Objects may be Easier maintenance. Objects may beunderstood as stand-alone entities.
Objects are potentially reusable components Objects are potentially reusable components. For some systems, there may be an obvious
mapping from real world entities to systemmapping from real world entities to systemobjects.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 7
Objects and object classesObjects and object classes
Objects are entities in a software system which Objects are entities in a software system whichrepresent instances of real-world and systementities.
Object classes are templates for objects. Theymay be used to create objects.y j
Object classes may inherit attributes andservices from other object classes.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 8
Objects and object classesObjects 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. p q
Objects are created according to some object class definition. An object class definition serves as a template for objects It includes declarationsclass 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.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 9
The Unified Modeling Languageg g g
Several different notations for describing object-oriented Several different notations for describing object orienteddesigns were proposed in the 1980s and 1990s.
The Unified Modeling Language is an integration of thesenotations.
It describes notations for a number of different modelsthat may be produced during OO analysis and designthat may be produced during OO analysis and design.
It is now a de facto standard for OO modelling.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 10
Employee object class (UML)p y j ( )
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 11
Object communicationObject communication
Conceptually objects communicate by Conceptually, objects communicate bymessage passing.
Messages• 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.
In practice, messages are often implementedby procedure calls
N d• Name = procedure name;• Information = parameter list.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 12
Message examplesg p
// Call a method associated with a buffer// Call a method associated with a buffer// object that returns the next value// in the buffer
v = circularBuffer.Get () ;
// Call the method associated with a// thermostat object that sets the// temperature to be maintained// temperature to be maintained
thermostat.setTemp (20) ;
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 13
Generalisation and inheritanceGeneralisation and inheritance
Objects are members of classes that define Objects are members of classes that defineattribute types and operations.
Classes may be arranged in a class hierarchywhere one class (a super-class) is a generalisation of oneor more other classes (sub-classes).
A sub-class inherits the attributes and A sub-class inherits the attributes andoperations from its super class and may addnew methods or attributes of its own.
Generalisation in the UML is implemented as inheritancein OO programming languages.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 14
A generalisation hierarchyA generalisation hierarchy
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 15
Advantages of inheritanceAdvantages of inheritance
It is an abstraction mechanism which may be It is an abstraction mechanism which may beused to classify entities.
It is a reuse mechanism at both the design and It is a reuse mechanism at both the design andthe programming level.
The inheritance graph is a source of The inheritance graph is a source oforganisational knowledge about domains andsystems.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 16
Problems with inheritanceProblems with inheritance
Object classes are not self contained they Object classes are not self-contained. theycannot be understood without reference to theirsuper-classes.p
Designers have a tendency to reuse theinheritance graph created during analysis. Cang p g ylead to significant inefficiency.
The inheritance graphs of analysis, design andimplementation have different functions andshould be separately maintained.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 17
UML associations
Objects and object classes participate in relationships Objects and object classes participate in relationshipswith other objects and object classes.
In the UML, a generalised relationship is indicated by anassociation.
Associations may be annotated with information thatdescribes the associationdescribes the association.
Associations are general but may indicate that anattribute of an object is an associated object or that amethod relies on an associated object.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 18
An association modelAn association model
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 19
Concurrent objectsConcurrent objects
The nature of objects as self contained entities The nature of objects as self-contained entitiesmake them suitable for concurrentimplementation.p
The message-passing model of objectcommunication can be implemented directly ifp yobjects are running on separate processors in adistributed system.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 20
Servers and active objectsServers and active objects
Servers Servers.• The object is implemented as a parallel process (server)
with entry points corresponding to object operations. If nocalls are made to it the object suspends itself and waits forcalls are made to it, the object suspends itself and waits forfurther requests for service.
Active objects• Objects are implemented as parallel processes and the
internal object state may be changed by the object itself and notsimply by external calls.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 21
Active transponder objectActive transponder object
Active objects may have their attributes modified Active objects may have their attributes modifiedby operations but may also update themautonomously using internal operations.y g p
A Transponder object broadcasts an aircraft’sposition. The position may be updated using ap p y p gsatellite positioning system. The objectperiodically update the position by triangulationf t llitfrom satellites.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 22
An active transponder objectAn active transponder object
class Transponder extends Thread {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 () ;c1 sat1.position () ;c2 = sat2.position () ;currentPosition = theNavigator.compute (c1, c2) ;
}
}
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 23
} //Transponder
Java threads
Threads in Java are a simple construct for Threads in Java are a simple construct forimplementing concurrent objects.
Threads must include a method called run() and Threads must include a method called run() andthis is started up by the Java run-time system.
Active objects typically include an infinite loop so Active objects typically include an infinite loop sothat they are always carrying out thecomputation.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 24
An object-oriented design processj g p
Structured design processes involve developing Structured design processes involve developinga number of different system models.
They require a lot of effort for development andy q pmaintenance of these models and, for smallsystems, this may not be cost-effective.H f l t d l d b However, for large systems developed bydifferent groups design models are an essentialcommunication mechanism.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 25
Process stagesg
Highlights key activities without being tied to any Highlights key activities without being tied to anyproprietary process such as the RUP.• Define the context and modes of use of the system;Define the context and modes of use of the system;• Design the system architecture;• Identify the principal system objects;• Develop design models;• Specify object interfaces.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 26
Weather system descriptionWeather system description
A weather mapping system is required to generate weather maps on a regular basisA 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.a s e da a o e a ea co pu e espo se o a eques o a ac e.
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.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 27
System context and models of usey
Develop an understanding of the relationships between Develop an understanding of the relationships betweenthe software being designed and its external environment
System context• A static model that describes other systems in the environment.
Use a subsystem model to show other systems. Following slideshows the systems around the weather station system.
Model of system use• A dynamic model that describes how the system interacts with
its environment. Use use-cases to show interactionsits environment. Use use cases to show interactions
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 28
Layered architecturey
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 29
Subsystems in the weather mapping system
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 30
Use-case models
Use case models are used to represent each Use-case models are used to represent eachinteraction with the system.
A use-case model shows the system features as A use case model shows the system features asellipses and the interacting entity as a stickfigure.g
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 31
Use-cases for the weather station
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 32
Use-case descriptionp
System Weather stationSystem Weather stationUse-case ReportActors Weather data collection system, Weather stationData The weather station sends a summary of the weather data that has been
collected from the instruments in the collection period to the weather datacollection system. The data sent are the maximum minimum and averageground and air temperatures, the maximum, minimum and av erage airpressures, the maximum, minimum and average wind speeds, the totalpressures, the maximum, minimum and average wind speeds, the totalrainfall and th e wind direction as sampled at 5 minute intervals.
Stimulus The weather data collection system establishes a modem link with theweather station and requests transmission of the data.
R Th i d d i h h d ll iResponse The summarised data is sent to the weather data collection systemComments 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 infuture.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 33
u u e.
Architectural designg
Once interactions between the system and its Once interactions between the system and itsenvironment have been understood, you use thisinformation for designing the system architecture.
A layered architecture as discussed in Chapter 11 isappropriate for the weather station• Interface layer for handling communications;Interface layer for handling communications;• Data collection layer for managing instruments;• Instruments layer for collecting data.
Th h ld ll b th 7 titi i There should normally be no more than 7 entities in anarchitectural model.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 34
Weather station architecture
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 35
Object identificationObject identification
Identifying objects (or object classes) is the most Identifying objects (or object classes) is the mostdifficult part of object oriented design.
There is no 'magic formula' for object There is no magic formula for objectidentification. It relies on the skill, experienceand domain knowledge of system designers.g y g
Object identification is an iterative process. Youare unlikely to get it right first time.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 36
Approaches to identificationApproaches to identification
Use a grammatical approach based on a natural Use a grammatical approach based on a naturallanguage description of the system (used in Hood OODmethod).
Base the identification on tangible things in theapplication domain.
Use a behavioural approach and identify objects based Use a behavioural approach and identify objects basedon what participates in what behaviour.
Use a scenario-based analysis. The objects, attributesand methods in each scenario are identified.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 37
Weather station descriptionWeather 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 groundfurther 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 pdata is transmitted to the mapping computer when a request is received.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 38
Weather station object classesj
Ground thermometer Anemometer Barometer Ground thermometer, Anemometer, Barometer• Application domain objects that are ‘hardware’ objects related
to the instruments in the system.
W th t ti Weather station• The basic interface of the weather station to its environment. It
therefore reflects the interactions identified in the use-casemodel.
Weather data• Encapsulates the summarised data from the instruments.Encapsulates the summarised data from the instruments.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 39
Weather station object classesj
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 40
Further objects and object refinementFurther objects and object refinement
Use domain knowledge to identify more objects and Use domain knowledge to identify more objects andoperations• Weather stations should have a unique identifier;• Weather stations are remotely situated so instrument failures
have to be reported automatically. Therefore attributes andoperations for self-checking are required.
Active or passive objects• In this case, objects are passive and collect data on request
rather than autonomously. This introduces flexibility at they yexpense of controller processing time.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 41
Design modelsg
Design models show the objects and object Design models show the objects and objectclasses and relationships between these entities.
Static models describe the static structure of the Static models describe the static structure of thesystem in terms of object classes andrelationships.p
Dynamic models describe the dynamicinteractions between objects.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 42
Examples of design modelsExamples of design models
Sub-system models that show logical groupings of Sub system models that show logical groupings ofobjects into coherent subsystems.
Sequence models that show the sequence of objectinteractions.
State machine models that show how individual objectschange their state in response to eventschange their state in response to events.
Other models include use-case models, aggregationmodels, generalisation models, etc.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 43
Subsystem modelsy
Shows how the design is organised into logically Shows how the design is organised into logicallyrelated groups of objects.
In the UML these are shown using packages - In the UML, these are shown using packagesan encapsulation construct. This is a logicalmodel. The actual organization of objects in theg jsystem may be different.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 44
Weather station subsystemsWeather station subsystemsThe image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 45
Sequence modelsq
Sequence models show the sequence of object Sequence models show the sequence of objectinteractions that take place• Objects are arranged horizontally across the top;• Time is represented vertically so models are read top
to bottom;• Interactions are represented by labelled arrowsInteractions are represented by labelled arrows,
Different styles of arrow represent different types ofinteraction;
• A thin rectangle in an object lifeline represents the• A thin rectangle in an object lifeline represents thetime when the object is the controlling object in thesystem.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 46
Data collection sequenceData collection sequence
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 47
Statecharts
Show how objects respond to different service requests Show how objects respond to different service requestsand the state transitions triggered by these requests• If object state is Shutdown then it responds to a Startup()
message;g ;• 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;If calibrate () the system moves to a calibrating state;• A collecting state is entered when a clock signal is received.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 48
Weather station state diagramWeather station state diagram
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 49
Object interface specificationj p
Object interfaces have to be specified so that the objects Object interfaces have to be specified so that the objectsand other components can be designed in parallel.
Designers should avoid designing the interfacerepresentation but should hide this in the object itself.
Objects may have several interfaces which areviewpoints on the methods providedviewpoints on the methods provided.
The UML uses class diagrams for interface specificationbut Java may also be used.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 50
Weather station interfaceWeather station interface
interface We atherStation {{
public void WeatherStation () ;
public void startup () ;bli id t t (I t t i)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 () ;
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 51
} //WeatherStation
Design evolutionDesign evolution
Hiding information inside objects means that Hiding information inside objects means thatchanges made to an object do not affect otherobjects in an unpredictable way.
Assume pollution monitoring facilities are to beadded to weather stations. These sample theair and compute the amount of differentair and compute the amount of differentpollutants in the atmosphere.
Pollution readings are transmitted with weatherd tdata.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 52
Changes requiredChanges required
Add an object class called Air quality as part of Add an object class called Air quality as part ofWeatherStation.
Add an operation reportAirQuality to Add an operation reportAirQuality toWeatherStation. Modify the control software tocollect pollution readings.p g
Add objects representing pollution monitoringinstruments.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 53
Pollution monitoringPollution monitoring
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 54
Key points
OOD is an approach to design so that design
Key points
OOD is an approach to design so that designcomponents have their own private state and operations.
Objects should have constructor and inspectionoperations. They provide services to other objects.
Objects may be implemented sequentially orconcurrentlyconcurrently.
The Unified Modeling Language provides differentnotations for defining different object models.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 55
Key pointsKey points
A range of different models may be produced A range of different models may be producedduring an object-oriented design process. Theseinclude static and dynamic system models.y y
Object interfaces should be defined preciselyusing e.g. a programming language like Java.g g p g g g g
Object-oriented design potentially simplifiessystem evolution.
Adapted from Ian Sommerville 2006, Software Engineering, 8th edition. Chapter 14 56