V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

45
V.Innocente OO A&D 1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente Vincenzo Innocente CERN CERN

description

V.Innocente OO A&D3 Software Life-Cycle n Requirements n Analysis n Design n Production n Testing n Maintenance

Transcript of V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

Page 1: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 1

Introduction to Object OrientedAnalysis and Design

Vincenzo InnocenteVincenzo InnocenteCERNCERN

Page 2: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 2

Software Development Method

A A Method Method encompasses:encompasses: A A NotationNotation

common means of expressing strategic and common means of expressing strategic and tactical decisionstactical decisions

A A ProcessProcessto specify how and when certain artifacts should to specify how and when certain artifacts should

be developedbe developed

Page 3: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 3

Software Life-Cycle

RequirementsRequirements AnalysisAnalysis DesignDesign ProductionProduction TestingTesting MaintenanceMaintenance

Page 4: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 4

Waterfall Model

Analysis

Design

Production

Testing

Requirements

Page 5: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 5

Evolutionary Model

Requirements

Analysis

Design

Production

Testing

Page 6: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 6

EvolutionaryEvolutionary

Short complete Short complete development cyclesdevelopment cycles

Risk drivenRisk driven Continuos integrationContinuos integration

Running PrototypeRunning Prototyperight from beginningright from beginning

WaterfallWaterfall

Full initial Full initial decomposition of the decomposition of the systemsystem

Single pass processSingle pass process Big Bang integrationBig Bang integration

Running Product onlyRunning Product onlyat the endat the end

Page 7: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 7

OO Methods

I will follow, in general, the I will follow, in general, the Booch MethodBooch Method..It has incorporated several practices fromIt has incorporated several practices fromother methods such as:other methods such as:

OMTOMT by J.Rumbaugh by J.Rumbaugh Objectory (Objectory (Use CasesUse Cases) by I.Jacobson) by I.Jacobson CRC CRC by R.Wirfs-Brockby R.Wirfs-Brock

Emerging Standard: Emerging Standard: UMLUML

Page 8: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 8

OO Basics

Classes and ObjectsClasses and Objects EncapsulationEncapsulation InheritanceInheritance PolymorphismPolymorphism Generic ProgrammingGeneric Programming

Page 9: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 9

Classic Concepts Revisited

Relations among objectsRelations among objects Functional decomposition in a classFunctional decomposition in a class

method responsibilitiesmethod responsibilities Functional decomposition among classesFunctional decomposition among classes

Class responsibilitiesClass responsibilities

Page 10: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 10

Unified Modeling Language

Class DiagramsClass Diagrams Sequence and Collaboration DiagramsSequence and Collaboration Diagrams Use Case DiagramsUse Case Diagrams State DiagramsState Diagrams

Page 11: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 11

UML Model of “Shape”Shape

draw()cancel()moveBy()

Circleradius_ : double

draw()

Point2dCenteredShape

moveBy() 1 11

SquareCenterToUpperCorner_ : Vector2d

draw()

relazione di aggregazione

relazioni di ereditarietà

classi concrete

classi astratte

1

Page 12: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 12

Class Diagram

Page 13: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 13

Class Diagram

Page 14: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 14

Object Sequence Diagram

Page 15: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 15

Object Collaboration Diagram

Page 16: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 16

DAQ Hardware Model Let us build a model of the Hardware for a data Let us build a model of the Hardware for a data

acquisition systemacquisition system ““Use Cases”:Use Cases”:

part management (where are, who belongs to)part management (where are, who belongs to) simulation (power, space occupied)simulation (power, space occupied) on-line control (on/off, temperature, voltage)on-line control (on/off, temperature, voltage) DAQ (where these data come from?)DAQ (where these data come from?) calibration (pedestals, gains, conversion factors)calibration (pedestals, gains, conversion factors)

Page 17: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 17

Domain Analysis

Gather Gather User RequirementsUser Requirements Gather Gather Functional RequirementsFunctional Requirements Identify Identify ConstraintsConstraints Assess Assess RisksRisks

Page 18: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 18

Use Cases and Scenarios

A A Use CaseUse Case specifies some region of the specifies some region of the system’s behaviorsystem’s behavior

A A ScenarioScenario is a is a concreteconcrete instance of a use instance of a use casecase

SecondarySecondary scenarios can be used to scenarios can be used to represent a represent a variationvariation on a theme of some on a theme of some primary scenarioprimary scenario (what if) (what if)

Page 19: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 19

Compute Power in a Rack

Get the rack Get the rack (how? See later)(how? See later) Loop over all its crates Loop over all its crates (how many? Is it (how many? Is it

relevant)relevant) for each crate loop over its modulesfor each crate loop over its modules

sum their power sum their power (how a module knows its power)(how a module knows its power) Sum the power of all crates Sum the power of all crates

Page 20: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 20

Where are my modules?

Get the collection of all modules which Get the collection of all modules which belongs to mebelongs to me

iterate over it and print their locationiterate over it and print their location (location = room/rack/crate/slot)(location = room/rack/crate/slot)

Page 21: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 21

Check temperature of a crate

Given its location find the room, the rack in Given its location find the room, the rack in the room and the crate in the rackthe room and the crate in the rack

Send it the proper messagesSend it the proper messages

Page 22: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 22

Change a module with a spare

Find a proper spareFind a proper spare Move the old module from crate to labMove the old module from crate to lab Move the new module form spare to crateMove the new module form spare to crate

Page 23: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 23

Iterative OO Development

SpecifySemantics

Identify Objects and Classes

Specify interfacesand implementation

IdentifyRelationships

Page 24: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 24

Discovering Objects and Classes

Examine the Examine the VocabularyVocabulary used in the used in the DomainDomain

Analyze the Analyze the Use CasesUse Cases and and Scenarios: Scenarios: look for who does whatlook for who does what

Look for Look for ThingsThings with common properties: with common properties:belong to different classes or are just different belong to different classes or are just different

objects?objects?

Page 25: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 25

CRCClass, Responsibilities, Collaborators

A

C

DE

F

B

xy

zs

f q

p

w

Page 26: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 26

Who are the players?

The Hardware components:The Hardware components:Racks, crates, modules of various typesRacks, crates, modules of various types

““Users” (people, displays, interfaces)Users” (people, displays, interfaces)it is useful to have “correspondents” in the modelit is useful to have “correspondents” in the model

First candidates for classes and objectsFirst candidates for classes and objects

Page 27: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 27

A First Model

Page 28: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 28

Power in a rack

Page 29: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 29

Base of a composite model

Page 30: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 30

#include “DaqComponent.h”class DaqLeaf: public DaqComponent {public: explicit DaqLeaf(float ip=0): power_(ip) {}

float power() const { return power_;} }protected: float power_;};

DaqLeaf.h

Code of the composite modelclass DaqComposite; // forward declarationclass DaqComponent {public: DaqComponent() : parent_(0) {} virtual float power() const=0protected: DaqComposite * parent_;};

DaqComponent.h

Page 31: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 31

Code of the composite model

#include “DaqComponent.h”class DaqComposite : public DaqComponent {public: typedef vector<DaqComponent *> VC; typedef VC::const_iterator CIter; DaqComposite(){} float power() const { tp =0; CIter p=components.begin(); CIter pe=components.end(); while (p!=pe) {tp+=(*p)->power(); ++p;} return tp; }protected: VC components;};

DaqComposite.h

Page 32: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 32

The “concrete” classes

Page 33: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 33

Add an inventory

Page 34: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 34

Where is a component located?

Given a component, we want to know its Given a component, we want to know its global locationglobal location The answer is a string which concatenates the The answer is a string which concatenates the

local locations (room,rack,crate,module)local locations (room,rack,crate,module) Who is responsible for the knowledge of the Who is responsible for the knowledge of the

location: the component or the composite?location: the component or the composite? Both solutions have advantages and disadvantages Both solutions have advantages and disadvantages A “flexible” solution is to assign the location to the A “flexible” solution is to assign the location to the

composition associationcomposition association

Page 35: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 35

Location of a component

In C++:In C++:DaqComponent points to LocationDaqComponent points to LocationDaqComposite has a vector of (pointer to) LocationsDaqComposite has a vector of (pointer to) LocationsLocation (besides an identifier) points to DaqComponent and DaqCompositeLocation (besides an identifier) points to DaqComponent and DaqCompositeLocation can be an abstract class and the representation of its identifierLocation can be an abstract class and the representation of its identifiercan thus depend on the composite (an integer for the slot of a module in a crate,can thus depend on the composite (an integer for the slot of a module in a crate,a string for the position of a rack in a room)a string for the position of a rack in a room)

Page 36: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 36

Print the global location

To obtain a string which identifies the global position, To obtain a string which identifies the global position, each component first invokes the location of its each component first invokes the location of its parent parent and then prints its own locationand then prints its own location(delegating this last operation to Location in order to (delegating this last operation to Location in order to be independent from the concrete representation of be independent from the concrete representation of the identifier of the location itself)the identifier of the location itself)

Page 37: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 37

Assign Responsibilities

Analyze the Analyze the role role of the various objectsof the various objects Focus on Focus on behavior behavior not on representationnot on representation Define the Define the interfaceinterface (the operations that (the operations that

satisfy these responsibilities)satisfy these responsibilities)

A correct assignment of responsibilitiesis the key to successful modularity and reuse

Page 38: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 38

Identify Relationships

Look for Look for collaborationscollaborations Look for Look for aggregatesaggregates Look for Look for generalizationsgeneralizations

How a How a client client knows its service knows its service providerprovider??

Page 39: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 39

Relationships

LogicalLogical

Generalization: Generalization: IsaIsa Aggregation: Aggregation: HasHas Acquaintance: Acquaintance: KnowsKnows

ImplementationImplementation

InheritanceInheritance Template instantiationTemplate instantiation Composition by valueComposition by value Composition by Composition by

referencereference

Page 40: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 40

To Have or To Be LorenzVector: LorenzVector: isis a Vector or a Vector or hashas a Vector a Vector Particle: Particle: isis a LorenzVector or a LorenzVector or hashas a LVector a LVector Track: Track: isis a Trajectory or a Trajectory or hashas a Trajectory a Trajectory Track: Track: isis a vector<Hit> or a vector<Hit> or hashas a vector<Hit> a vector<Hit> DetectorUnit: DetectorUnit: isis a Surface or a Surface or hashas a Surface a Surface Car: Car: hashas 4 wheels or 4 wheels or isis 4 times a wheel 4 times a wheel

(impossible in C++, typical in EIFFEL)(impossible in C++, typical in EIFFEL)

Page 41: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 41

To Have or To Be

In C++, public inheritance obeys the In C++, public inheritance obeys the “Liskov “Liskov Principle”Principle” : : child objects can be used child objects can be used wherever a base class is requiredwherever a base class is required..

Use Use InheritanceInheritance if if PolymorphismPolymorphism is is requiredrequired

Don’t change the Don’t change the Base Class BehaviorBase Class Behavior

Page 42: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 42

What should I Be? Look for good (useful) abstractions:Look for good (useful) abstractions:

in a model with chairs, tables, cats and dogs a “4-in a model with chairs, tables, cats and dogs a “4-Legs-Type” is not necessarily a good abstractionLegs-Type” is not necessarily a good abstraction Furniture and Domestic-Animal maybe are.Furniture and Domestic-Animal maybe are. Could be that the best is “Things-which-belongs-to-a-Could be that the best is “Things-which-belongs-to-a-

member-of-the-family”member-of-the-family” In our DAQ Hardware example are TDC, ADC In our DAQ Hardware example are TDC, ADC

good abstractions or CAMAC, VME are more good abstractions or CAMAC, VME are more relevant?relevant?

Look at the scenarios and check what is used Look at the scenarios and check what is used polymorphicallypolymorphically! !

Page 43: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 43

Composition:by value or by reference Simple Type (Simple Type (intint, , doubledouble, …): , …): by valueby value Part of the Object Status: Part of the Object Status: by valueby value Object is shared: Object is shared: by referenceby reference Allocated at run time: Allocated at run time: by referenceby reference Used Polymorphically:Used Polymorphically: by reference by referencevector<Hit>vector<Hit>: logically by: logically by value, value,

implementation is byimplementation is by reference reference

Page 44: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 44

Code It!

Look for Look for ReuseReuse Use commonUse common idioms idioms Choose the proper Choose the proper semanticssemantics for the for the

associationsassociations Select a suitable Select a suitable algorithmalgorithm for each for each

operationoperationTest itTest it against the scenarios against the scenarios

Page 45: V.Innocente OO A&D1 Introduction to Object Oriented Analysis and Design Vincenzo Innocente CERN.

V.Innocente OO A&D 45

Summary Decompose the problemDecompose the problem

Use CasesUse Cases ScenariosScenarios

Uncover Uncover CClasses,lasses,RResponsibilities,esponsibilities,CCollaborationsollaborations CRC CardsCRC Cards Class DiagramsClass Diagrams Trace DiagramsTrace Diagrams

Iterate to discover generalizationIterate to discover generalization