grasp: designing objects with responsibilities applying uml and patterns craig larman chapter 17...

26
GRASP: Designing GRASP: Designing Objects with Objects with Responsibilities Responsibilities Applying UML and Patterns Applying UML and Patterns Craig Larman Craig Larman Chapter 17 Chapter 17 Glenn D. Blank CSE432, Lehigh Glenn D. Blank CSE432, Lehigh University University

Upload: amari-dodgen

Post on 28-Mar-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

GRASP: Designing GRASP: Designing Objects with Objects with

ResponsibilitiesResponsibilities

Applying UML and PatternsApplying UML and PatternsCraig LarmanCraig LarmanChapter 17Chapter 17

Glenn D. Blank CSE432, Lehigh Glenn D. Blank CSE432, Lehigh UniversityUniversity

Page 2: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Chapter Learning ObjectivesChapter Learning Objectives

Learn about design patternsLearn about design patterns Learn how to apply five GRASP patterns Learn how to apply five GRASP patterns

You’ve learned about static class diagrams You’ve learned about static class diagrams and dynamic interaction diagramsand dynamic interaction diagrams

UML is just notation; now you need to learn UML is just notation; now you need to learn how 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

Page 3: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Design patterns in architectureDesign patterns in architecture A A patternpattern is a is a recurringrecurring solutionsolution to a standard to a standard

problemproblem, , in a in a contextcontext..

Christopher Alexander, professor of architecture…Christopher Alexander, professor of architecture…– Why is what a prof of architecture Why is what a prof of architecture

says relevant to software?says relevant to software?– ““A pattern describes a problem A pattern describes a problem

which occurs over and over again which occurs over and over again in our environment, and then in our environment, and then describes the core of the solution describes the core of the solution to that problem, in such a way that to that problem, in such a way that you can use this solution a million you can use this solution a million times over, without ever doing it times over, without ever doing it the same way twice.”the same way twice.”

Page 4: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

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 to I could tell you how to

make a dress by specifying make a dress by specifying the route of a scissors the route of a scissors through a piece of cloth through a piece of cloth in terms of angles and in terms of angles and lengths of cut. Or, I could lengths of cut. Or, I could give you a pattern. give you a pattern. Reading the specification, Reading the specification, you would have no idea you would have no idea what was being built or if you had built the right thing what was being built or if you had built the right thing when you were finished. The pattern foreshadows the when you were finished. The pattern foreshadows the product: 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.”

Page 5: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Patterns in engineeringPatterns in engineering How do other engineers find and use patterns?How do other engineers find and use patterns?

– Mature engineering disciplines have Mature engineering disciplines have handbookshandbooks describing successful solutions to known problemsdescribing successful solutions to known problems

– Automobile designers don't design cars from scratch Automobile designers don't design cars from scratch using the laws of physicsusing the laws of physics

– Instead, they Instead, they reusereuse standard designs with successful standard designs with successful track 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 pattern Be sure that you make everything according to the pattern

I 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 expensive Developing software from scratch is also expensive

– Patterns support Patterns support reusereuse of software architecture design of software architecture design

Page 6: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Definitions and names Definitions and names

Alexander: “A Alexander: “A patternpattern is a is a recurringrecurring solutionsolution to a standard to a standard problemproblem, in a , in a contextcontext.”.”

Larman: “In OO design, a Larman: “In OO design, a patternpattern is a named is a named description of a problem and solution that can be description of a problem and solution that can be applied in new contexts; ideally, applied in new contexts; ideally, a pattern advises us on how to apply the solution a pattern advises us on how to apply the solution

in varying circumstances and considers in varying circumstances and considers the forces and trade-offs.”the forces and trade-offs.”

How is Larman’s definition similar to How is Larman’s definition similar to Alexander’s?Alexander’s?

How are these definitions significantly different?How are these definitions significantly different?

Page 7: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Patterns have suggestive names:Patterns have suggestive names:– Arched Columns Pattern, Easy Toddler Dress Arched Columns Pattern, Easy Toddler Dress

Pattern, etc.Pattern, etc. Why is naming a pattern or principle helpful?Why is naming a pattern or principle helpful?

– It supports chunking and incorporating that It supports chunking and incorporating that concept into our understanding and memoryconcept into our understanding and memory

– It facilitates communicationIt facilitates communication

Naming Patterns—Naming Patterns—important!important!

Star and Plume Quilt

Page 8: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

GRASPGRASP Name chosen to suggest the importance of Name chosen to suggest the importance of

graspgrasping fundamental principles to ing fundamental principles to successfully design object-oriented softwaresuccessfully design object-oriented software

Acronym for Acronym for GGeneral eneral RResponsibility esponsibility AAssignment ssignment SSoftware oftware PPatternsatterns

Describe fundamental principles of Describe fundamental principles of object design and responsibility object design and responsibility

Expressed as patternsExpressed as patterns

Page 9: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Five GRASP patterns:Five GRASP patterns:

Creator Creator Information ExpertInformation Expert Low CouplingLow Coupling ControllerController High CohesionHigh Cohesion

Page 10: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Creator patternCreator patternName: Name: CreatorCreator

Problem: Who creates an instance of A?Problem: Who creates an instance of A?

Solution: Assign class B the responsibility Solution: Assign class B the responsibility to create an instance of class A if one of to create an instance of class A if one of these is true (the more the better): these is true (the more the better):

B contains or aggregates A (in a B contains or aggregates A (in a collection)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

Page 11: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Who creates the Squares?Who creates the Squares?

Figure 17.3, page 283Figure 17.3, page 283

Page 12: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

How does Create pattern lead to How does Create pattern lead to this partial Sequence diagram?this partial Sequence diagram?

Figure 17.4, page 283Figure 17.4, page 283

Page 13: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

How does Create pattern develop How 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

Page 14: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Discussion of Creator patternDiscussion of Creator pattern

Responsibilities for object creation are Responsibilities for object creation are commoncommon

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

Page 15: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Contraindications or caveatsContraindications or caveats

Creation may require significant Creation may require significant complexity: complexity: – recycling instances for performance recycling instances for performance

reasonsreasons– conditionally creating instances from a conditionally creating instances from a

family of similar classesfamily of similar classes In these instances, other patterns are In these instances, other patterns are

available…available…– We’ll learn about Factory and other patterns We’ll learn about Factory and other patterns

later…later…

Page 16: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Information Expert Information Expert pattern or principlepattern or principle

Name: Information ExpertName: Information ExpertProblem: How to assign responsibilities to objects?Problem: How to assign responsibilities to objects?Solution: Assign responsibility to the class that Solution: Assign responsibility to the class that

has the information needed to fulfill it?has the information needed to fulfill it? E.g., Board information needed to get a SquareE.g., Board information needed to get a Square

Page 17: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Benefits and Benefits and ContraindicationsContraindications

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 Encourages cohesive, lightweight class definitionsdefinitions

But:But: Information expert may contradict patterns of Information expert may contradict patterns of

Low Coupling and High CohesionLow Coupling and High Cohesion Remember separation of concerns principle Remember separation of concerns principle

for large sub-systemsfor large sub-systems I.e., keep “business” or application logic I.e., keep “business” or application 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.

Page 18: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Name: Name: Low CouplingLow Coupling Problem: How to reduce the impact of Problem: How to reduce the impact of

change and encourage reuse? change and encourage reuse? Solution: Assign a responsibility so that Solution: Assign a responsibility so that

coupling (linking classes) remains low.coupling (linking classes) remains low.

Low Coupling PatternLow Coupling Pattern

Page 19: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Why does the following design Why does the following design violate Low Coupling?violate Low Coupling?

Why is a better idea to leave Why is a better idea to leave getSquare responsibility in Board?getSquare responsibility in Board?

Page 20: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Benefits & ContraindicationsBenefits & Contraindications

Understandability: Classes are easier Understandability: Classes are easier to understand in isolationto understand in isolation

Maintainability: Classes aren’t affected by Maintainability: Classes aren’t affected by changes in other componentschanges in other components

Reusability: easier to grab hold of classesReusability: easier to grab hold of classes

But:But: Don’t sweat coupling to stable classes (in Don’t sweat coupling to stable classes (in

libraries or pervasive, well-tested classes) libraries or pervasive, well-tested classes)

Page 21: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Controller patternController pattern

Name: Name: Controller Controller (see Model-View-Controller architecture)(see Model-View-Controller architecture)

Problem: Who should be responsible for UI Problem: Who should be responsible for UI events?events?

Solution: Assign responsibility for receiving or Solution: Assign responsibility for receiving or handling 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 (façadefaçade pattern) pattern)– Represent a use case scenario within which Represent a use case scenario within which

the system event occurs (a the system event occurs (a sessionsession controller) controller)

Page 22: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Who is the controller of playGame Who is the controller of playGame operation?operation?

Figure 17.9, p. 288Figure 17.9, p. 288What class represents the overall system What class represents the overall system

or relevant use case scenario?or relevant use case scenario?

Page 23: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Cohesion measures how strongly related and Cohesion measures how strongly related and focused are the responsibilities of an focused are the responsibilities of an elementelement

Name: Name: High CohesionHigh Cohesion Problem: How to keep classes focused and Problem: How to keep classes focused and

manageable?manageable? Solution: Assign responsibility so that Solution: Assign responsibility so that

cohesion remains high.cohesion remains high.

High Cohesion patternHigh Cohesion pattern

Page 24: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

How does the design on right How does the design on right promote high cohesion?promote high cohesion?

Delegate responsibility & coordinate Delegate responsibility & coordinate workwork

Page 25: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

Benefits & Benefits & ContraindicationsContraindications

Understandability, maintainabilityUnderstandability, maintainability Complements Low CouplingComplements Low Coupling

But:But: Avoid grouping of responsibilities or code into Avoid grouping of responsibilities or code into

one class or component to simplify one class or component to simplify maintenance by one person. maintenance by one person. Why?Why?

Sometimes desirable to create less cohesive Sometimes desirable to create less cohesive server objects that provide an interface for server objects that provide an interface for many operations, due to performance needs many operations, due to performance needs associated with remote objects and remote associated with remote objects and remote communicationcommunication

Page 26: GRASP: Designing Objects with Responsibilities Applying UML and Patterns Craig Larman Chapter 17 Glenn D. Blank CSE432, Lehigh University

SummarySummary

Skillful assignment of responsibilities is Skillful assignment of responsibilities is extremely important in object-oriented extremely important in object-oriented design (CRC cards are one technique)design (CRC cards are one technique)

Patterns are named problem/solution pairs Patterns are named problem/solution pairs that codify good advice and principles that codify good advice and principles related to assignment of responsibilitiesrelated to assignment of responsibilities

GRASP identifies five patterns or GRASP identifies five patterns or principles:principles:– Creator, Information Expert, Controller, Creator, Information Expert, Controller,

Low Coupling and High CohesionLow Coupling and High Cohesion