CSSE 374: More GRASP’ing for Object Responsibilities

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

CSSE 374: Object-Oriented Design Exercise Steve Chenoweth Office: Moench Room F220 Phone: (812) These slides and.
Object-Oriented Analysis and Design CHAPTER 17, 25: GRASP PATTERNS 1.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
CSSE 374: Introduction to Object- Oriented Analysis and Design Q1 Steve Chenoweth Office: Moench Room F220 Phone: (812)
GRASP The other four What are the first five? What is the goal/purpose of using patterns?
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
NJIT More GRASP Patterns Chapter 22 Applying UML and Patterns Craig Larman Prepared By: Krishnendu Banerjee.
Chapter 25 More Design Patterns. Polymorphism Issue: Conditional variation –If-then-else or switch statements –New variation or case: Conditional statements.
Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns.
Chapter 25 More Design Patterns.
CSc 335: Three More OO Design Principles
CSSE 374: More GRASP’ing and Use Case Realization Steve Chenoweth Office: Moench Room F220 Phone: (812) These.
CSSE 374: Design Class Diagrams Steve Chenoweth Office: Moench Room F220 Phone: (812) These slides and others.
CSSE 374: Domain Model Refinements and Iteration 3 Preparations Q1 These slides and others derived from Shawn Bohner, Curt Clifton, Alex Lo, and others.
CSSE 374: Even More Object Design with Gang of Four Design Patterns These slides derived from Shawn Bohner, Curt Clifton, and others involved in delivering.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
CSSE 374: Final Perspectives on Software Architecture and Design These slides derived from Shawn Bohner, Curt Clifton, Alex Lo, and others involved in.
1/19 Component Design On-demand Learning Series Software Engineering of Web Application - Principles of Good Component Design Hunan University, Software.
CSSE 374: GRASP’ing at the First Five Patterns Principles Steve Chenoweth Office: Moench Room F220 Phone: (812)
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
CSSE 374: More Object Design with Gang of Four Design Patterns Steve Chenoweth Office: Moench Room F220 Phone: (812)
Chapter 17. GRASP General Responsibility Assignment Software Patterns (Principles) OOD: after identifying requirements, create domain model, define responsiblities.
1 Chapter 17 GRASP Design Patterns: Designing Objects with Responsibilities.
Chandan Rupakheti Office: Moench Room F203 Phone: (812) These slides and others derived from Shawn Bohner, Curt.
CSSE 374: Introduction to Gang of Four Design Patterns
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Chapter 17. Initial Object Design Inputs: requirements meetings various Use Cases – 10% complete Key risks addressed with preliminary programming System.
SOFTWARE DESIGN.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
GRASP: Designing Objects with Responsibilities
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Single Responsibility Principle Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office:
OO Methodology Elaboration Iteration 2 - Design Patterns -
Thanks for Coming l WEB. Principles of Good Design SEI, SJTU WEB APPS and SERVICES.
OO Design Roshan Chitrakar. Analysis to Design Do the RIGHT thing Do the RIGHT thing Requirement Analysis Requirement Analysis Domain Modeling with addition.
Five design principles
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
Design. Practices Principles Patterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
GRASP: More Patterns for Assigning Responsibilities Presented By Dr. Shazzad Hosain.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Advanced Software Architecture Muhammad Bilal Bashir PhD Scholar (Computer Science) Mohammad Ali Jinnah University.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
GRASP.
Elaboration: Iteration 2. Elaboration: Iteration 2 Basics Iteration 1 ends with : All the software has been tested: The idea in the UP is to do early,
CSSE 374: UML Activity Diagrams
CompSci 280 S Introduction to Software Development
BTS530: Major Project Planning and Design
GoF Patterns (GoF) popo.
Conception OBJET GRASP Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Copyright © by Curt Hill
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Chapter 25 GRASP The other four.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Chapter 25 GRASP The other four.
GRASP.
GoF Patterns Ch. 26.
Presentation transcript:

CSSE 374: More GRASP’ing for Object Responsibilities Left – How far should you “reach” before you “GRASP”? The tool shown is the opposite of kid-proofing your house. CSSE 374: More GRASP’ing for Object Responsibilities Steve Chenoweth Office: Moench Room F220 Phone: (812) 877-8974 Email: chenowet@rose-hulman.edu Chandan Rupakheti Office: Moench Room F203 Phone: (812) 877-8390 Email: rupakhet@rose-hulman.edu Q1. List three of the five GRASP patterns that we’ve used so far. Image from http://umeandbabymakesthree.blogspot.com/2011_07_01_archive.html These slides and others derived from Shawn Bohner, Curt Clifton, Alex Lo, and others involved in delivering 374. Q1

Learning Outcomes: Patterns, Tradeoffs Identify criteria for the design of a software system and select patterns, create frameworks, and partition software to satisfy the inherent trade-offs. Four more GRASP Patterns: Polymorphism Pure Fabrication Indirection Protected Variation Q3

GRASP II – And Furthermore… All these are very general OO principles: Polymorphism Indirection Pure Fabrication Protected Variations Result in systems that are more easy to maintain easier

Polymorphism Problem: Solution: How do we handle alternatives based on type? How do we create pluggable software components? Solution: When related alternatives vary by type, assign responsibility to the types for which the behaviors varying. Use subtypes and polymorphic methods Eliminates lots of conditional logic based on type Corollary: Avoid instanceof tests How do we handle alternatives based on type? Chained ifs and lots of switch statements are a bad code smell → new types require finding conditions and editing How do we create pluggable software components Pluggable components require swapping one module for another without changing surrounding design Q2: Briefly, what problem does the Polymorphism principle solve? Q3: Briefly, what solution does the Polymorphism principle suggest? Q2,3

Polymorphism Example 1/6 Bad: switch (square.getType()) { case GO: … case INCOME_TAX: … case GO_TO_JAIL: … default: … } What happens when we need to add other sorts of squares in future iterations? Sketch figure 25.2 on board. (Design class diagram showing abstract Square class with subclasses for RegularSquare, GoSquare, IncomeTaxSquare, and GoToJailSquare. Also show Player with attribute location pointing to Square)) Leave room for adding the landedOn method, a JailSquare, fields and associations. Solution: Replace switch with polymorphic method call

Polymorphism Example 2/6 Guideline: Unless there is a default behavior in a superclass, declare a polymorphic operation in the superclass to be {abstract} Each type of square exhibits variable behavior when a player lands on it - Superclass’s polymorphic method is abstract unless there is default behavior in subclasses

Polymorphism Example 3/6 Walk through diagram briefly, point out the multiple occurrences of p and loc. landedOn() msg sent to each square at end of takeTurn() It applies a square’s rules to the player Player passes itself to the square so square can ‘talkback’ to player Add landedOn(p:Player) {abstract} to DCD on board in Square. Make abstract unless clear default behavior Details of polymorphic method drawn separately

Polymorphism Example 4/6 landedOn() for the GoSquare Players gets $200 for ‘landing on’ Go square landedOn() for Regular square Nothing happens so the method is empty (NO-OP) What about that pesky “Go to jail!” square?

Polymorphism Example 5/6 landedOn() for IncomeTaxSquare Square needs to know player’s net worth to determine tax due Is reduceCash() well named? Why not payTax()?

Polymorphism Example 6/6 landedOn() for GoToJailSquare

Polymorphism Observations Using polymorphism indicates that Piece class not needed since it’s a proxy for the Player A design using Polymorphism can be easily extended for new variations When should supertype be an interface? Don’t want to commit to a class hierarchy Need to reduce coupling Contraindication: Polymorphism can be over- used – “speculative future-proofing” Remember subclassing is a very strong form of coupling. Q4: How does Polymorphism support future extension of a system? Q5: What is a contraindication for the Polymorphism principle? Don’t be too clever! Q4,5

Pure Fabrication The OO rule to “follow reality” only gets us so far… Problem: What object should have responsibility when solutions for low representation gap (like Info. Expert) lead us astray (i.e., into high coupling and low cohesion) Solution: Assign a cohesive set of responsibilities to a fictitious class (not in the domain model) This is really our first pattern that argues for violating other principles. Q6: Briefly, what solution does the Pure Fabrication principle suggest? Q6

Pure Fabrication Example 1/2 Die Face Value Role getFV Cup 1 Cup Roll getTotal Player Dice * {ordered} Pure Fabrication How do we model the player rolling the dice? If Player rolls dice, then dice rolling behavior not very reusable How do we provide something that would be more reusable? >>>Cup is a Pure Fabrication responsible for rolling the dice

Pure Fabrication Example 2/2 Using Cup to roll the dice Note the lack of loop interaction frame – cup takes the combination of dice and creates total of dice values

Common Design Strategies Representational decomposition Based on what they represent in domain Lowering the representation gap (noun-based) Behavioral decomposition Based on what they do! Centered around behaviors (verb-based) Representation decomposition Design objects based on what they represent in the domain What we’ve been doing to date Behavioral decomposition Design objects based on what they do Function-centric, encapsulate an algorithm or special method - break code into objects focusing on lowering the representation gap (noun-based) - break code into objects centered around behaviors (verb-based) Behavioral decomposition is more abstract, harder to reason about, but sometimes other advantages make it worth the cost. Pure Fabrications are often “behavioral decompositions”

Pure Fabrication Observations Benefits: Higher cohesion Greater potential for reuse Contraindications: Can be abused to create too many behavior objects Watch for data being passed to other objects for calculations Q7: True or False (circle one): Over use of Pure Fabrication can lead to too many behavior objects. Downsides: Commonly used to encapsulate an algorithm that doesn’t fit well in other classes When overused, model has lots of classes that encapsulate a single method Keep operations with data unless you have a good reason not to Q7

Cartoon of the Day Used with permission. http://notinventedhe.re/on/2009-10-13

Indirection Problem: Solution: How to assign responsibility in order to avoid direct coupling that is undesirable? Solution: Assign responsibility to an intermediate object to mediate between the other components Problem: How to assign responsibility in order to avoid direct coupling that is undesirable? Especially when server object is highly unstable or could result in vendor lock-in (e.g., proprietary API) Solution: Assign to an intermediate object that mediates between components like client-server Q8: Briefly, what problem does the Indirection principle solve? Q9: Briefly, what solution does the Indirection principle suggest? The late David Wheeler was a computer science pioneer and professor at Cambridge University in England. There is no problem in computer science that cannot be solved by an extra level of indirection. — David Wheeler Q8,9

Indirection & Polymorphism Example Adapters serve as a layer of indirection between NextGen POS and service providers. One might use TCP socket communication, another SOAP, another some other protocol - Controllers are a special case of indirection Each tax calculator has a unique API NextGen (client) should be immune to any specific calculator Abstract interface has getTaxes() method Specific adapters inherit from interface. Each concrete adapter calls the actual tax calculator

Key Concept Protected Variation Problem: How do we design objects and systems so that instability in them does not have undesirable effects on other elements? Solution: Identify points of predicted instability (variation) and assign responsibilities to create a stable interface around them “Find what varies and encapsulate it” --- Design Patterns Explained, Trott & Shalloway A foundational design principle Used in conjunction with Polymorphism & Indirection Q10: Briefly, what solution does the Protected Variations principle suggest? Why is ITaxCalculatorAdaptor an example of PV? - Indirection protects against changes in existing providers. - Polymorphism protects against adding new providers Q10

Protected Variations: Observations When to use it? Variation point – a known area where clients need to be protected from variable servers Evolution point – an area where future variation may occur Should we invest in protecting against future variation? How likely is it to occur? If it is, then should probably use PV now If unlikely, then should probably defer using PV Benefits (if we guessed variation points correctly): Extensions easy to add Can plug in new implementations Lower coupling Lower cost of change Risk: watch out for speculative future-proofing

Protected Variations by Other Names Information hiding [David Parnas ‘72] “… a list of difficult design decisions which are likely to change. Each module is then designed to hide such a decision from the others.” Liskov Substitution Principle [Barbara Liskov ‘87] Methods of a subtype must have (at least) the expected behavior of overridden methods Open-Closed Principle [Bertrand Meyer ‘88] Modules should be both open for extension and closed to modification[s] that affect clients LSP: coined by Turing winner Barbara Liskov - methods of a subtype must have (at least) the expected behavior of overridden methods I.e., you can plug a subtype instance in where a supertype is expected without “breaking anything”

Law of Demeter Don’t talk to strangers who seem unstable Special case of PV Don’t talk to strangers who seem unstable Within a method, messages should only be sent to: “this”, a parameter, a field of “this”, an element in collection of field of “this”, or new objects What variation does this protect against? • Build in photo and caption It isn’t such a sin to chain calls into stable library code. This guideline warns against code like: sale.getPayment().getAccount().getAccountHolder()