Beruflich Dokumente
Kultur Dokumente
DesignandModelViewController
CSC216
CSC216:ProgrammingConceptsSarahHeckman 1
Design
Goal:decidethestructureofthesoftwareandthe
hardwareconfigurationsthatsupportit.
Howindividualclassesandsoftwarecomponentswork
togetherinthesoftwaresystem
Smallprograms:~10classesandinterfaces
Mediumprograms:1000sofclassesandinterfaces
Designforsingleuserapplicationsdifferentfromweb
applicationsandmobileapplications
SoftwareArtifacts:designdocuments,classdiagrams,
otherUMLdiagrams
CSC216:ProgrammingConceptsSarahHeckman 2
1
1/26/2016
OODesign
Discoverclasses(andtheirstate)
Whataretheusefulentitiesorconceptsinyour
projectrequirements?
Determinetheresponsibilitiesofeachclass
Whatshouldyourclassdo?Howdoesyouclass
manipulateitsdata?
Describetherelationshipsbetweentheclasses
Doclasseshaveinstancesofotherclasses?Doesa
methodofaclassneedtouseaninstanceofanother
class?
CSC216:ProgrammingConceptsSarahHeckman 3
FindingCandidateClassesandMethods
CandidateClasses(andstate)
Nouns
Youwilldetermineifthenounisanappropriate
objectorappropriatestate
CandidateMethods
Verbs
Youwilldeterminewhatclassthemethodismost
appropriatefor.
UseCRCcardstohelpidentifycandidateclasses
CSC216:ProgrammingConceptsSarahHeckman 4
2
1/26/2016
UniversityRegistrationSystem
UniversityXYZhasthreetypesofstudents:
undergraduates,masters,andPhDs.All
studentscanregisterforcoursesusingthe
RegistrationSystem.
CSC216:ProgrammingConceptsSarahHeckman 5
UML
UML:UnifiedModelingLanguage
Modelsobjectorientedsoftware
Convergencefromthreeearliermodelinglanguages
OMT(JamesRumbaugh)
OOSE(Ivar Jacobson)
Booch (GradyBooch)
OverseenbytheObjectMentorGroup(OMG):
(www.omg.org)
CSC216:ProgrammingConceptsSarahHeckman 6
3
1/26/2016
TypesofUMLDiagrams
UseCaseDiagrams
ClassDiagrams
SequenceDiagram
StateChartDiagram
CSC216:ProgrammingConceptsSarahHeckman 7
UseCaseDiagrams
Howthesystemprovidesservice(s)totheactors
thatwilluseit
Enter Player
Info
Visit Jail
Go to Jail
Pass Go Go to Free
Cell Parking
extend extend
extend extend
Roll Dice
Move include include
Player include
extend
Play More Than
One Turn in a
Purchase Round
Tradable Cell include
extend avoid
extend Switch Turn
include
Pay Rent
include
Draw Card
View Information
include
Bad Player
Trade
Buy House include
Get Out
of Jail
CSC216:ProgrammingConceptsSarahHeckman 8
4
1/26/2016
ClassDiagrams
Classesandrelationshipsbetweenthem
CSC216:ProgrammingConceptsSarahHeckman 9
SequenceDiagram
Sequenceofactions(methodcalls)tocompleteascenario
CSC216:ProgrammingConceptsSarahHeckman 10
5
1/26/2016
StateChartDiagrams
Howanobjectchangesstatewhenperformingatask
CSC216:ProgrammingConceptsSarahHeckman 11
CreatingUMLDiagrams
Commercialoropensourcetools
MicrosoftVisio
CommercialEclipsePlugins
VioletUML(opensource)
Dia (freeware)
CSC216:ProgrammingConceptsSarahHeckman 12
6
1/26/2016
ClassDiagramsinDepth
CSC216:ProgrammingConceptsSarahHeckman 13
Classes
Name
State
Behaviors
CSC216:ProgrammingConceptsSarahHeckman 14
7
1/26/2016
Classes
Name
State
Behaviors
CSC216:ProgrammingConceptsSarahHeckman 15
Generalization(isa)
Generalizationrelationshipsshowinheritance
Thechildclassesinheritthestateandbehavior
oftheparentclass
AnUndergraduateStudent isaStudent!
ParentClass
ChildClass
CSC216:ProgrammingConceptsSarahHeckman 16
8
1/26/2016
Composition(hasa)
Compositioniswhenoneclasshasaninstanceofanotherclass
Fields
Parameters
ManywaystomodelinUMLdependingontypeofcomposition
relationship
Inassignments,requiringacompositionrelationshipmeans
thereMUSTbeahasarelationshipinthediagram
ItdoesnthavetousethecompositionUMLconnectorofasoliddiamond
Itmaybemodeledusingassociation(arrow)oraggregation(open
diamond)connectors
CSC216:ProgrammingConceptsSarahHeckman 17
has-a
CSC326:SoftwareEngineeringNCStateSoftwareEngineeringFaculty L13 18
9
1/26/2016
Properties:Attributesvs.Associations
Ifattributesandassociationsareessentiallythesame
thing,whenshouldIuseeachone?
Attributes
Smallthings(Strings,primitives,Dates,etc.)
Partofexistinglibrary
Immutablevalueobjects
Associations
Significantclasses
Partofwhatwillbeimplemented
Mutablereferences
CSC326:SoftwareEngineeringNCStateSoftwareEngineeringFaculty L13 19
Aggregationvs.CompositionConnectors
WholePartRelationship
Aggregation:strongerassociation(unidirectional)
Ex.Adept containsasetofemployees
Ex.Afacultycontainsasetofteachers
Awhitediamondattheendoftheassociationnexttothe
aggregateclass
Thepartcanexistseparatefromthewhole
Composition:strongeraggregation(unidirectional)
Ex.Invoice InvoiceLine
Ablackdiamondontheendofassociationnexttothe
compositeclass
Thepartcannotexistseparatefromthewhole
CSC326:SoftwareEngineeringNCStateSoftwareEngineeringFaculty L13 20
10
1/26/2016
CompositionRelationship
Aggregation
Dependency
Association
CSC216:ProgrammingConceptsSarahHeckman 21
Relationships Interface
Implements
Composition
AbstractMethod
Abstract
Association Class
Inheritance
Dependency
CSC216:ProgrammingConceptsSarahHeckman 22
11
1/26/2016
EvaluatingaClass/ClassDiagram
1. Intentionrevealingnaming:Doesthenameoftheobject
conveyitsabstractions?Doestheabstractionhaveanatural
meaninganduseinthedomain?
2. SingleResponsibility:Dothename,mainresponsibility
statementdataandfunctionsalign?
??
CSC326:SoftwareEngineeringNCStateSoftwareEngineeringFaculty L13 23
SequenceDiagrams
Howobjectscollaboratewithinaspecified
scenario
Behavioroftheobjects
Sequenceofmethodcalls
Anobjectslifetimeisrepresentedvertically
Arectangle(activationbar)representswhenthe
objectisactive
Methodisonthestack
CSC216:ProgrammingConceptsSarahHeckman 24
12
1/26/2016
SequenceDiagrams(2)
Messagesarepassedbetweenobjectsusinga
solidarrow
Messagesmaybepassedwithinanobject(selfcall)
Returnmessagespointbacktothecallingobject
andareshownindashedarrows
Drawingsequencediagramshelpdevelopers
understandhowtoimplementascenario.
CSC216:ProgrammingConceptsSarahHeckman 25
SequenceDiagram
CSC216:ProgrammingConceptsSarahHeckman 26
13
1/26/2016
DesignPatterns
Eachpatterndescribesaproblemwhichoccurs
overandoveragaininourenvironment,and
thendescribesthecoreofthesolutiontothat
problem,insuchawaythatyoucanusethis
solutionamilliontimesover,withouteverdoing
itthesamewaytwice.
ChristopherAlexander
CSC216:ProgrammingConceptsSarahHeckman 27
DesignPatterns(2)
Descriptionsofcommunicatingobjectsand
classes
Participatingclasses
Rolesandcollaborations
Distributionofresponsibilities
Customizedtosolveageneraldesignproblemin
aspecificcontext
CSC216:ProgrammingConceptsSarahHeckman 28
14
1/26/2016
WhyDesignPatterns?
Dontneedtostartfromscratchtosolvea
programmingproblem
Reuseprevioususefulideas
Designpatternsaretheclosestthingwehaveto
aDesignHandbookforsoftwareengineering
Designpatternsarenotasilverbullet!!!
SometimesthebestdesignsdoNOTrequire
designpatterns!!!
CSC216:ProgrammingConceptsSarahHeckman 29
TheGangofFour
THEbookonDesignPatterns
23designpatterns
Moreinliterature
BookfocusesonC++,butwe
haveanonlinereferencefor
Java
Patternsarefound,not
created!
CSC216:ProgrammingConceptsSarahHeckman 30
15
1/26/2016
PatternFamilies
Creational:processofobjectcreation
Singleton
Structural:compositionofclassesorobjects
Behavioral:waysinwhichclassesorobjects
interactanddistributeresponsibility
State/Strategy
Command
CSC216:ProgrammingConceptsSarahHeckman 31
ModelViewController(MVC)
Isolatesbusinessordomainlogicfromtheinputand
presentation
Model:dataunderlyingtheapplication
Changesinstaterequirenotifyingtheview
Modelabstractsthedatastorage(DB,POJOs,etc.)
View:UI
Amodelmayhavemanyviews
Controller:receivesinputandinitiatesresponsebycallingmodel
CSC216:ProgrammingConceptsSarahHeckman 32
16
1/26/2016
MVCinWebApps
View:HTMLorXHTML
Controller:GETorPOSTinput
GivestheGETorPOSTinformationtotheobjects
Model:underlyingdomainlogic
CSC216:ProgrammingConceptsSarahHeckman 33
MVCinJava
View/Controller:verytightlycoupled
GUIApplications
View(howtheobjectispresented)andcontroller(how
eventsontheobjectareprocessed)belongtooneUI
object
Usegenericcomponents(i.e.JButton),whichdelegateto
theunderlyinglookandfeel(i.e.theoperatingsystem)
ConsoleApplications
View(consoleprompts)andcontroller(howuserinputis
processed)typicallybelongtoone(ormore)UIobjects.
Model:underlyingdomainlogic
CSC216:ProgrammingConceptsSarahHeckman 34
17
1/26/2016
DesignBestPractices
Classesshouldhavecohesion
Theextenttowhichthecodeforaclassrepresentsasingle
abstraction
Degreetowhichthemembersofaclassarerelatedtothe
generalpurposeoftheclass
Allowsforreusabilityoftheclassinotherprograms
Examples:
Student:onlycontainsinformationrelevanttoastudent
Courses:onlycontainsinformationrelevanttocourses
CSC216:ProgrammingConceptsSarahHeckman 35
DesignBestPractices(2)
Aprogramshouldhavelowcoupling
Aconnectionbetweentwoclassesisadependencyor
coupling
Instanceofanobjectintheclass
Callanotherclasstocompleteatask
Internalcoupling:modifyinganotherclassdata avoidif
possible!
Parametercoupling:usingservicesprovidedbyanotherclass
unavoidable
Highlycoupledprogramsaredifficulttowriteand
maintain
CSC216:ProgrammingConceptsSarahHeckman 36
18
1/26/2016
DesignBestPractices(3)
Dataandbehaviorshouldbeencapsulated
withinaclassorapackage
Usepackagestogrouptogethercommon
functionality
Example:InanAndroidapplication,allActivitiesarepart
oftheactivitypackage
Informationhiding:makedatamembersprivate
Detailsaboutimplementationarehiddenwithin
classandonlyexposedwithpublicmembers
CSC216:ProgrammingConceptsSarahHeckman 37
References
LaurieWilliams,UMLClassDiagramSlides:
http://agile.csc.ncsu.edu/SEMaterials/UMLClass
Diagrams.pdf
http://www.ibm.com/developerworks/rational/l
ibrary/content/RationalEdge/sep04/bell/
M.Fowler,UMLDistilled,ThirdEdition,Addison
Wesley,Boston,2004.
CSC216:ProgrammingConceptsSarahHeckman 38
19