scj1203 week 10 object-oriented design ood · polymorphism principle#1 ... define the behavior of...
TRANSCRIPT
4/22/13
1
ObjectOrientedDesign(OOD):TheConcept
Objec,ves
• Toexplainhowaso8waredesignmayberepresentedasasetofinterac;ngobjectsthatmanagetheirownstateandopera;ons
4/22/13
2
3
Topicscovered
• ObjectOrientedDevelopment(OOD)
• Characteris,csofOOD• BasicPrinciplesofOOD• ObjectOrientedConcepts• AdvantagesofOOD• Implementa,onIssues
RECAPONSDLCPHASESVS.ARTEFACTSPRODUCED
4/22/13
3
RecaponSDLCPhases&Artefacts
Business Domain Analysis
Requirement
Analysis
Design
Implementation
Testing & Deployment
Maintenance & Evolution
Domain Model (Class Diagram)
1) Functional & Non-Functional requirement 2) Use Case diagram
1) System Sequence Diagram 2) Activity Diagram
1) Class Diagram (refined) 2) Detail Sequence Diagram 3) State Diagram
1) Application Source Code 2) User Manual Documentation
1) Test Cases 2) Prototype
1) Change Request Form
SRS
SDD
…(zoom‐intoDesign)
• RequirementSpecifica,ons(Func,onal&Non‐Func,onal)Requirement
• RequirementModels(Use‐casesdiagram,classdiagram,ac,vitydiagram,sequencediagram)Analysis
• Architecturaldesignmodel(Packagediagram)• Detaileddesignmodels(Classdiagram(refined),sequencediagram(refined),statediagram)
Design
4/22/13
4
ArchitecturalDesignClassifica,on
ArchitecturalDesign
SystemOrganisa.on(Style/
Pa4ern)
RepositoryModel
Client‐ServerModel
Layered(Tiers)Model
Pipe&Filter
Model‐View‐ControllerControlStyle
Model
Centralised
Call‐Return
Manager
Event‐Driven Broadcast
Interrupt‐Driven
ModularComposi.on
Object‐Oriented
Func,on‐Oriented
ArchitecturalDesignClassifica,on
ArchitecturalDesign
SystemOrganisa.on(Style/
Pa4ern)
RepositoryModel
Client‐ServerModel
Layered(Tiers)Model
Pipe&Filter
Model‐View‐ControllerControlStyle
Model
Centralised
Call‐Return
Manager
Event‐Driven Broadcast
Interrupt‐Driven
ModularComposi.on
Object‐Oriented
Func,on‐Oriented
4/22/13
5
OBJECT‐ORIENTEDDEVELOPMENT
Object‐orientedApproach
• Therehavebeenbasically3approachesininforma,onsystemdevelopmentarea:– process‐oriented,– data‐orientedand– object‐orientedapproaches
• Thefocusoffirsttwowaseitheronprocessordata,whiletheobject‐orientedapproachcombinesdataandprocessesintosingleen,,escalledobjects.
• Objectsusuallycorrespondtotherealthingssuchas– customers,suppliers,contracts,offices,vehicles
• Thegoalofobject‐orientedapproachistomakesystemelementsmorereusable
10
4/22/13
6
Object‐orienteddevelopment
• Keyideainobjectorienteddevelopment:– Therealworldcanbeaccuratelydescribedasa
collec,onofobjectsthatinteract.
• Processtotransformtheanalysismodel(fromOOA)intoOODesignModel.– FromWHAT(specifica,on)intoHOW
(implementa,on).
• Assump,ons:– Describinglarge,complexsystemsasinterac,ng
objectsmakethemeasiertounderstand.– Thebehaviorsofrealworldobjectstendtobestable
over,me.– Changestendtobelocalizedtoafewobjects.
11
OOA(What?)OOD(How?)
• SPECIFICATIONSAnalysis(OOAusingUML)
• IMPLEMENTATIONDesign(OODusingUML)
4/22/13
7
Characteris,csofOOD
• OODprocessinvolvesdesigningobjectclassesandrela,onshipbetweentheseclasses.
• Objectsareabstrac,onsofreal‐worldorsystemen,,esandmanagethemselves.
• Objectsareindependentandencapsulatestateandrepresenta,oninforma,on.
– Changingimplementa,on(code)ofoneobjectshouldNOTaffectothersystemobjects.
• Systemfunc,onalityisexpressedintermsofobjectservices.
• Shareddataareasareeliminated.• Objectscommunicatebymessagepassing.
OOPRINCIPLES
4/22/13
8
OODBasicPrinciples
Abstrac;on
Encapsula;on
Modularity
Polymorphism
Principle#1.Abstrac,onAbstrac,onfocusesontheessen,alcharacteris,csofsomeobject,rela,vetotheperspec,veoftheviewer.
4/22/13
9
BasicPrinciplesofOOD:Abstrac,on
• Abstrac,onallowsustomanagecomplexitybyconcentra,ngontheessen,alcharacteris,csthatmakesanen,tydifferentfromothers.
• OOusesabstrac,ontodepictclassesandobjectsinasystem.
Principle#2.Encapsula,on
Encapsula,onhidesthedetailsoftheimplementa,onofanobject.
4/22/13
10
BasicPrinciplesofOOD:Encapsula,on
• Encapsula,onseparatestheimplementa,onofobjectsbehaviorfromitspublicinterface
• Itiscalled“informa,onhiding”,itallowsobject’sbehaviortobeusedwithoutknowingitsimplementa,on
• Offerstwokindsofsecurity:– Protectsobject’sinternalstatefrombeingchangedfrom
outsideusers.– Changescanbedonetothebehaviorimplementa,on
withoutaffec,ngotherobjects• Ananalogy:
– Whenyoudriveacar,youdon’thavetoknowthedetailsofhowmanycylinderstheenginehasorhowthegasolineandairaremixedandignited.
– Insteadyouonlyhavetoknowhowtousethecontrols.
Principle#3.Modularity
4/22/13
11
BasicPrinciplesofOOD:Modularity
• Modularitycanbedefinedastheprocessofbreakingupofacomplexsystemintosmall,selfcontainedpiecesthatcanbemanagedeasily.
• Packagesandsubsystemssupportthedefini,onofthemodularity.
• Classesareindependentmodules
Principle#4.Polymorphism
4/22/13
12
BasicPrinciplesofOOD:Polymorphism• Polymorphism—thesamewordorphrasecanmean
differentthingsindifferentcontexts• Analogy:inEnglish,bankcanmeansideofariveror
aplacetoputmoney(oneword,bank,butdifferentmeaningindifferentcontext)
• InOOD,Polymorphismistheabilitytohidemanyimplementa,onsbehindthesameinterface.
• Example:
OOCONCEPTS
4/22/13
13
Object‐OrientedConcepts
Object Class Anribute
Opera,on Interface Package
Subsystem Rela,onships
ObjectOrientedConcepts:Object
• Anobjectisaseparatelyiden,fiableen,tythathasasetofopera,onsandastatethatrecordstheeffectsoftheopera,ons.
• Objectsarecharacterizedby:– state,– opera,ons,– iden,ty.
4/22/13
14
ObjectOrientedConcepts:Object(cont.)• aQributes:
– thecollec,onofinforma,onheld(i.e.,stored)bytheobject.
– Itcanchangeover,me.– Itchangesastheresultofanopera,onperformedontheobject.
– Theanributeisencapsulatedwithintheobject• opera;ons:
– aprocedurethattakestheanributesoftheobjectandzeroormoreargumentsandchangesthestateand/orreturnsoneormorevalues.
• iden;ty:– awaytodis,nguishbetweentwodis,nctobjects
(eveniftheyhavethesamestateandopera,ons).
ObjectOrientedConcepts:Class
• Aclassisatemplateforcrea,ngobjects.– Aclassdescribesacollec,onofrelatedobjects(i.e.,instancesoftheclasses).
– Objectsofthesameclasshavecommonopera,onsandacommonsetofpossiblestates.
– Theconceptofclassiscloselyrelatedtotheconceptofabstractdatatypethatwediscussedpreviously.
• Aclassisadescrip,onofasetofobjectsthatsharethesameanributes,opera,ons,rela,onships(Booch,1999).– Anobjectisaninstanceofaclass
4/22/13
15
ObjectOrientedConcepts:Class(cont.)
ObjectOrientedConcepts:Class(cont.)
• InUML,aclasshas3compartments– Name
– Anributes– Opera,ons
• Classnota,oninUML:
4/22/13
16
ObjectOrientedConcepts:Class(Anributes)
• Anributesisadatatypeheldbytheobjectsintheclass.
• Onlytheobjectitselfshouldbeabletochangethevalueofitsanributes.
• Differentobjectsofthesameclass
ObjectOrientedConcepts:Class(Opera,ons)
4/22/13
17
ObjectOrientedConcepts:Classes&Objects
ObjectOrientedConcepts:Classes&Objects• Aclassisanabstractdefini,onofanobject.Itdefinesthestructureandthebehaviorofeachobject.
• Aclassistemplateforproducingobjectsofacertaintype.
• Objectsaregroupedintoclasses
• Anobjectisarun,meinstancesofthecorrespondingclass
4/22/13
18
ObjectOrientedConcepts:Classes&Objects
ObjectOrientedConcepts:TerminologyRecap
• object‐usuallyaperson,placeorthing(anoun)
• method‐anac,onperformedbyanobject(averb)
• class‐acategoryofsimilarobjects(suchasautomobiles)
*Classdoesnotholdanyvaluesoftheobject’sanributes
4/22/13
19
Stereotype
• Astereotypeisanewclassofmodelingelementthatisintroducedatmodeling,me.
• Thestereotypeisa“tag”,orsymbol,usedtomarkthetypeofanobjectorotherconstructsothatitcanbedis,nguishedandtreateddifferentlyfromothertypesofconstructs
ObjectOrientedConcepts:Interfaces• Aspecialclassthatcontainsacollec,onofopera,onsthatareusedtospecifyaserviceofaclass.– Interfacesspecify,butnotimplementbehaviour.
4/22/13
21
Abstrac,onwithInterface
ObjectOrientedConcepts:Component• Acomponentisaphysicalandreplaceablepartofasystemthatconformstoandprovidestherealiza,onofasetofinterfaces(Booch,1999)
• Componentmaybe:– Sourcecodecomponent(shellscripts,datafiles,*.cpp)
– Link,mecomponent(*.dllfiles):<<library>>– Run,mecomponent(JavaBeans,Ac,veCcontrols,COMobjects,CORBAobjects,*.exefiles):<<applica,on>>,<<table>>
• AcomponentenforcesEncapsula,on
4/22/13
22
ObjectOrientedConcepts:Component(cont.)
ObjectOrientedConcepts:Packages• Apackageisageneralpurposemechanismfor
organizingelementsintogroups(Booch,1999)• Itisamodelelementwhichcancontainother
modelelements• Asagroupingmechanism,doesnothaveany
seman,csdefined• Itmaynothavearepresenta,onin
implementa,on(osenitmayrepresentadirectory)
• Apackageownsitselements;anelementcanbelongtoonlyonepackage
• ApackageenforcesModularity
4/22/13
23
ObjectOrientedConcepts:Packages(cont.)
ObjectOrientedConcepts:Subsystems• Asystemthatispartofalargersystem,and
whichhasadefiniteinterface.• Amodelelementthathastheseman,csof
apackageandaclassthatprovidesbehavior.
• Itimplementsoneormoreinterfaceswhichdefinethebehaviorofthesubsystem
• AsubsystemenforcesEncapsula,on&Modularity
4/22/13
24
ObjectOrientedConcepts:Rela,onshipbetweenClasses/Components
• Arela,onshipisaconnec,onamongthings
• UMLusesthesekindsofrela,onships:– Associa,on
• Aggrega,on• Composi,on
– Dependency– Generaliza,on– Realiza,on
ObjectOrientedConcepts:Associa,onRela,onship
• Associa,onrepresentsageneralbinaryrela,onshipthatdescribesanac,vitybetweentwoclasses.
• Therela,onshipallowsoneclasstocallmethodsinotherclass.
• Whenthereisassocia,onbetweenclasses,instances/objectsoftheclasshasassocia,on.
4/22/13
25
ObjectOrientedConcepts:Associa,onRela,onship(cont.)
Association between classes
Instance association
ObjectOrientedConcepts:Associa,onRela,onship‐Mul,plicity
• Specifieshowmanyobjects/instancesofoneclassmayrelatetoasingleinstanceofanassociatedclass.
• i.e.
4/22/13
26
ObjectOrientedConcepts:Associa,onRela,onship‐Role
• Arolenameisanamethatuniquelyiden,fiesoneendofanassocia,on.
• Wrinennexttotheclassthatplaystherole.
• i.e.
Student Subject take 1SCS core
ObjectOrientedConcepts:Aggrega,onRela,onship
• Aspecialformofassocia,on.• Representsanownershiprela,onshipsbetweentwoclasses.
• Modelstherela,onshiplike“has‐a”,“part‐of”,“owns”.
• Partsmaybelongtomul,plewholes.
4/22/13
27
ObjectOrientedConcepts:Aggrega,onRela,onship(cont.)
ObjectOrientedConcepts:Composi,onRela,onship• Modelsawhole/partrelationshipwithastrongownership;whenthewholedies,thepartdoessoaswell
• Anobjectcanbeonlypartofonewholeobject• Thewholepartisresponsibleforcreationanddestructionofitsparts
4/22/13
28
ObjectOrientedConcepts:InheritanceRela,onship
• “is‐a”or“akindof”rela,onship• Classeswithproper,esincommoncanbegroupedsothattheircommonproper,esareonlydefinedonce.
• Superclass:inherititsanributes&methodstothesubclass(es).
• Subclass:inheritallitssuperclassanributes&methods+haveitsownuniqueanributes&methods.
ObjectOrientedConcepts:InheritanceRela,onship(cont.)
Sub-Class
4/22/13
29
ObjectOrientedConcepts:InheritanceRela,onship(cont.)
• Howdowestructuretheseclasses?
ObjectOrientedConcepts:InheritanceRela,onship(cont.)
• Asubclassinheritsfromitssuperclass:– attributes– operations– relationships.
• Asubclassmayaddtoitsdefinition:– Attributes– Operations– Relationships– Redefineinheritedoperations
4/22/13
30
ModelingExample
• Modelthefollowingscenario:
– PaulaismarriedtoPaul
– Theyhavechildren
ObjectOrientedConcepts:Realiza,onRela,onship
• Arealizationisasemanticrelationshipbetweenclassifiersinwhichoneclassifierspecifiesacontractthatanotherguarantiestocarryout.
• Itisusedinthecontextofinterfacesandcollaborations
• Aninterfacecanberealizedbymanyclassesorcomponents
• Aclassmayrealizemanyinterfaces
4/22/13
31
61
ObjectOrientedConcepts:DependencyRela,onship
• Adependencyisarela,onshipthatspecifiesthatachangeinonethingmayaffectotherthingsthatuseitbutnotnecessarilytheinverse.
ObjectOrientedConcepts:DependencyRela,onship(cont.)
4/22/13
32
AdvantagesofOOD
• Easiermaintenance.Objectsmaybeunderstoodasstand‐aloneen,,es.
• Objectsarepoten,allyreusablecomponents.
• Forsomesystems,theremaybeanobviousmappingfromrealworlden,,estosystemobjects.
Implementa,onIssues
• Reuse– Whenyouaredevelopingsosware,youshouldmakeas
muchuseaspossibleofexis,ngcode.Soswarereuseispossibleatdifferentlevels
– Theabstrac;onlevel:Designpanernorarchitecturalpanern
– Theobjectlevel:directlyreuseobjectsfromlibraryratherwri,ngcodeyourself.E.g.,youneedtoprocessemailmessageinJava,youmayuseobjectsandmethodsfromJavaMaillibrary.
– Thecomponentlevel:collec,onofobjectandobjectclasses
• Youwritesomecode:Userinterfaceconnectedtodatabase– Thesystemlevel: youresueen,reapplica,onsystems.
Itususallyinvolvessomeconfigura,onofthesesystems
• Configura,onManagement:Versionmanagement,systemintegra,on(compiling),problemtracking(bugreport)
• Host‐targetdevelopment:developmentcomputer(host),executeprogramoncomputer(target).Host&targetcomputersmaybedifferent.