09 grasp-object oriented analysis and design
TRANSCRIPT
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
1/26
GRASP: Designing ObjectsGRASP: Designing Objects
with Responsibilitieswith ResponsibilitiesApplying UML and PatternsApplying UML and Patterns
Craig LarmanCraig LarmanChapter 17Chapter 17
Glenn D. Blank CSE432, Lehigh UniversityGlenn D. Blank CSE432, Lehigh University
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
2/26
Chapter Learning ObjectivesChapter Learning Objectives
Learn about design patternsLearn about design patterns
Learn how to apply five GRASP patternsLearn how to apply five GRASP patterns
Youve learned about static class diagramsYouve learned about static class diagramsand dynamic interaction diagramsand dynamic interaction diagrams
UML is just notation; now you need to learnUML is just notation; now you need to learnhow to make effective use of the notationhow to make effective use of the notation
UML modeling is an art, guided by principlesUML modeling is an art, guided by principles
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
3/26
Design patterns in architectureDesign patterns in architecture
AA patternpattern is ais arecurringrecurring solutionsolution to a standardto a standardproblemproblem,,in ain acontextcontext..
Christopher Alexander, professor of architectureChristopher Alexander, professor of architecture
Why is what a prof of architectureWhy is what a prof of architecturesays relevant to software?says relevant to software?
A pattern describes a problemA pattern describes a problemwhich occurs over and over againwhich occurs over and over againin our environment, and thenin our environment, and then
describes the core of the solutiondescribes the core of the solutionto that problem, in such a way thatto that problem, in such a way thatyou can use this solution a millionyou can use this solution a milliontimes over, without ever doing ittimes over, without ever doing itthe same way twice.the same way twice.
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
4/26
Design and dress patternsDesign and dress patterns Jim Coplein, a software engineer:Jim Coplein, a software engineer:
I like to relate this definition to dress patterns I like to relate this definition to dress patterns
I could tell you how toI could tell you how tomake a dress by specifyingmake a dress by specifyingthe route of a scissorsthe route of a scissorsthrough a piece of cloththrough a piece of clothin terms of angles andin terms of angles andlengths of cut. Or, I couldlengths of cut. Or, I couldgive you a pattern.give you a pattern.Reading the specification,Reading the specification,
you would have no ideayou would have no ideawhat was being built or if you had built the right thing whenwhat was being built or if you had built the right thing whenyou were finished. The pattern foreshadows the product:you were finished. The pattern foreshadows the product:it is the rule for making the thing, but it is also,it is the rule for making the thing, but it is also,in many respects, the thing itself.in many respects, the thing itself.
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
5/26
Patterns in engineeringPatterns in engineering
How do other engineers find and use patterns?How do other engineers find and use patterns? Mature engineering disciplines haveMature engineering disciplines have handbookshandbooks
describing successful solutions to known problemsdescribing successful solutions to known problems
Automobile designers don't design cars from scratchAutomobile designers don't design cars from scratch
using the laws of physicsusing the laws of physics
Instead, theyInstead, theyreusereuse standard designs with successfulstandard designs with successfultrack records, learning from experiencetrack records, learning from experience
Should software engineers make use of patterns? Why?Should software engineers make use of patterns? Why?
Be sure that you make everything according to the patternBe sure that you make everything according to the patternI have shown you here on the mountain. Exodus 25:40.I have shown you here on the mountain. Exodus 25:40.
Developing software from scratch is also expensiveDeveloping software from scratch is also expensive
Patterns supportPatterns supportreusereuse of software architecture designof software architecture design
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
6/26
Definitions and namesDefinitions and names
Alexander: AAlexander: A patternpattern is ais arecurringrecurring solutionsolutionto a standardto a standardproblemproblem, in a, in acontextcontext..
Larman: In OO design, aLarman: In OO design, a patternpattern is a namedis a nameddescription of a problem and solution that can bedescription of a problem and solution that can beapplied in new contexts; ideally,applied in new contexts; ideally,a pattern advises us on how to apply the solutiona pattern advises us on how to apply the solutionin varying circumstances and considersin varying circumstances and considersthe forces and tradethe forces and trade--offs.offs.
How is Larmans definition similar to Alexanders?How is Larmans definition similar to Alexanders?
How are these definitions significantly different?How are these definitions significantly different?
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
7/26
Patterns have suggestive names:Patterns have suggestive names: Arched Columns Pattern, Easy Toddler Dress Pattern, etc.Arched Columns Pattern, Easy Toddler Dress Pattern, etc.
Why is naming a pattern or principle helpful?Why is naming a pattern or principle helpful? It supports chunking and incorporating that conceptIt supports chunking and incorporating that concept
into our understanding and memoryinto our understanding and memory It facilitates communicationIt facilitates communication
Naming PatternsNaming Patternsimportant!important!
Star and Plume Quilt
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
8/26
GRASPGRASP
Name chosen to suggest the importance ofName chosen to suggest the importance ofgraspgrasping fundamental principles to successfullying fundamental principles to successfullydesign objectdesign object--oriented softwareoriented software
Acronym forAcronym forGG
eneraleneralRR
esponsibilityesponsibilityAAssignmentssignmentSSoftwareoftware PPatternsatterns
Describe fundamental principles ofDescribe fundamental principles ofobject design and responsibilityobject design and responsibility
Expressed as patternsExpressed as patterns
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
9/26
Five GRASP patterns:Five GRASP patterns:
CreatorCreator
Information ExpertInformation Expert
Low CouplingLow Coupling ControllerController
High CohesionHigh Cohesion
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
10/26
Creator patternCreator pattern
Name:Name: CreatorCreator
Problem: Who creates an instance of A?Problem: Who creates an instance of A?
Solution: Assign class B the responsibilitySolution: Assign class B the responsibilityto create an instance of class A if one ofto create an instance of class A if one ofthese is true (the more the better):these is true (the more the better):
B contains or aggregates A (in a collection)B contains or aggregates A (in a collection)
B records AB records A
B closely uses AB closely uses A
B has the initializing data for AB has the initializing data for A
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
11/26
Who creates the Squares?Who creates the Squares?
Figure 17.3, page 283Figure 17.3, page 283
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
12/26
How does Create pattern lead toHow does Create pattern lead to
this partial Sequence diagram?this partial Sequence diagram?
Figure 17.4, page 283Figure 17.4, page 283
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
13/26
How does Create pattern developHow does Create pattern develop
this Design Class Diagram (DCD)?this Design Class Diagram (DCD)?
Figure 17.5 , page 283Figure 17.5 , page 283
Board has a composite aggregation relationship with Square I.e., Board contains a collection of Squares
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
14/26
Discussion of Creator patternDiscussion of Creator pattern
Responsibilities for object creation are commonResponsibilities for object creation are common
Connect an object to its creator when:Connect an object to its creator when:
Aggregator aggregates PartAggregator aggregates Part Container contains ContentContainer contains Content
Recorder recordsRecorder records
Initializing data passed in during creationInitializing data passed in during creation
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
15/26
Contraindications or caveatsContraindications or caveats
Creation may require significant complexity:Creation may require significant complexity:
recycling instances for performance reasonsrecycling instances for performance reasons
conditionally creating instances from a familyconditionally creating instances from a familyof similar classesof similar classes
In these instances, other patterns are availableIn these instances, other patterns are available
Well learn aboutFactoryand other patterns laterWell learn aboutFactoryand other patterns later
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
16/26
Information ExpertInformation Expertpattern or principlepattern or principle
Name: Information ExpertName: Information Expert
Problem: How to assign responsibilities to objects?Problem: How to assign responsibilities to objects?
Solution: Assign responsibility to the class that hasSolution: Assign responsibility to the class that has
the information needed to fulfill it?the information needed to fulfill it? E.g., Board information needed to geta SquareE.g., Board information needed to geta Square
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
17/26
Benefits and ContraindicationsBenefits and Contraindications
Facilitates information encapsulation:Facilitates information encapsulation: why?why? Classes use their own info to fulfill tasksClasses use their own info to fulfill tasks
Encourages cohesive, lightweight class definitionsEncourages cohesive, lightweight class definitions
But:But: Information expert may contradict patterns ofInformation expert may contradict patterns of
Low Coupling andHigh CohesionLow Coupling andHigh Cohesion
Remember separation of concerns principleRemember separation of concerns principlefor large subfor large sub--systemssystems I.e., keep business orapplication logicI.e., keep business orapplication logic
in one place, user interface in other place,in one place, user interface in other place,database access in another place, etc.database access in another place, etc.
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
18/26
Name:Name: Low CouplingLow Coupling
Problem: How to reduce the impact ofProblem: How to reduce the impact ofchange and encourage reuse?change and encourage reuse?
Solution: Assign aresponsibility so thatSolution: Assign aresponsibility so thatcoupling (linking classes) remains low.coupling (linking classes) remains low.
Low Coupling PatternLow Coupling Pattern
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
19/26
Whydoes the following designWhydoes the following designviolate Low Coupling?violate Low Coupling?
Why is a better idea to leaveWhy is a better idea to leave
getSquare responsibility in Board?getSquare responsibility in Board?
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
20/26
Benefits & ContraindicationsBenefits & Contraindications
Understandability: Classes are easierUnderstandability: Classes are easierto understand in isolationto understand in isolation
Maintainability: Classes arentaffected byMaintainability: Classes arentaffected bychanges in other componentschanges in other components
Reusability: easier to grab hold of classesReusability: easier to grab hold of classes
But:But: Dont sweat coupling to stable classes (inDont sweat coupling to stable classes (in
libraries or pervasive, welllibraries or pervasive, well--tested classes)tested classes)
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
21/26
Controller patternController pattern
Name:Name: ControllerController(see Model(see Model--ViewView--Controllerarchitecture)Controllerarchitecture)
Problem: Who should be responsible for UIProblem: Who should be responsible for UI
events?events?
Solution: Assign responsibility forreceiving orSolution: Assign responsibility forreceiving orhandling a system event in one of two ways:handling a system event in one of two ways:
Represent the overall system (Represent the overall system (faadefaade pattern)pattern) Representa use case scenario within which theRepresenta use case scenario within which the
system event occurs (asystem event occurs (a sessionsession controller)controller)
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
22/26
Who is the controller of playGame operation?Who is the controller of playGame operation?
Figure 17.9, p. 288Figure 17.9, p. 288
What class represents the overall systemWhat class represents the overall systemorrelevant use case scenario?orrelevant use case scenario?
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
23/26
Cohesion measures how stronglyrelatedandCohesion measures how stronglyrelatedandfocusedare the responsibilities ofan elementfocusedare the responsibilities ofan element
Name:Name: High CohesionHigh Cohesion Problem: How to keep classes focusedandProblem: How to keep classes focusedand
manageable?manageable?
Solution: Assign responsibility so that cohesionSolution: Assign responsibility so that cohesion
remains high.remains high.
High Cohesion patternHigh Cohesion pattern
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
24/26
How does the design on right promoteHow does the design on right promotehigh cohesion?high cohesion?
Delegate responsibility & coordinate workDelegate responsibility & coordinate work
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
25/26
Benefits & ContraindicationsBenefits & Contraindications
Understandability, maintainabilityUnderstandability, maintainability
Complements Low CouplingComplements Low Coupling
But:But: Avoid grouping ofresponsibilities or code into oneAvoid grouping ofresponsibilities or code into one
class or component to simplify maintenance byclass or component to simplify maintenance byone person.one person. Why?Why?
Sometimes desirable to create less cohesive serverSometimes desirable to create less cohesive serverobjects that provide an interface for manyobjects that provide an interface for manyoperations, due to performance needs associatedoperations, due to performance needs associatedwith remote objects andremote communicationwith remote objects andremote communication
-
8/7/2019 09 Grasp-OBJECT ORIENTED ANALYSIS AND DESIGN
26/26
SummarySummary
Skillful assignment ofresponsibilities isSkillful assignment ofresponsibilities isextremely important in objectextremely important in object--orienteddesignorienteddesign(CRC cards are one technique)(CRC cards are one technique)
Patterns are named problem/solution pairs thatPatterns are named problem/solution pairs thatcodify goodadvice and principles related tocodify goodadvice and principles related toassignment ofresponsibilitiesassignment ofresponsibilities
GRASP identifies five patterns or principles:GRASP identifies five patterns or principles:
Creator, Information Expert, Controller,Creator, Information Expert, Controller,Low Coupling andHigh CohesionLow Coupling andHigh Cohesion