Copyright © Craig Larman. 2000 All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference:

Slides:



Advertisements
Similar presentations
GRASP: Designing Objects with Responsibilities
Advertisements

Object-Oriented Analysis and Design
Object Design Examples with GRASP
Object-Oriented Analysis and Design CHAPTER 17, 25: GRASP PATTERNS 1.
GRASP Patterns M Taimoor Khan
Oct 2, Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
Feb R. McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
© 2005 Prentice Hall8-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
NJIT More GRASP Patterns Chapter 22 Applying UML and Patterns Craig Larman Prepared By: Krishnendu Banerjee.
February Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m.
NJIT 1 GRASP: Designing Objects with Responsibilities Chapter 17 Applying UML and Patterns Craig Larman.
Object-Oriented Analysis and Design
GRASP : Designing Objects with Responsibilities
October 20, 2005Architectural Design, ECEN Architectural Design Architecture Business Cycle Design for Maintainability ECEN 5543 / CSCI 5548 SW Eng.
Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation.
Feb 4, Ron McFadyen1 founded on principles of good OO design idea was first put forth by Christopher Alexander (1977) in their work concerning.
CSSE 374: More GRASP’ing and Use Case Realization Steve Chenoweth Office: Moench Room F220 Phone: (812) These.
GRASP Design Patterns: Designing Objects with Responsibilities
GRASP Pattern Zhen Jiang West Chester University
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
GRASP Patterns Presented By Dr. Shazzad Hosain. Patterns A pattern describes a problem and solution, and given a name. Examples are Singleton, Adapter,
Chapter 18 Object Design Examples with GRASP. Objectives Design use case realizations –A use-case realization describes how a particular use case is realized.
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.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Architecture GRASP Realization of use cases in interaction diagrams Design class diagram Design ( how )
Chapter 7: Object Design Examples with GRASP. Objective Design use case realizations. Apply GRASP to assign responsibilities to classes. Apply UML to.
BTS430 Systems Analysis and Design using UML Design Patterns.
Chapter 17. Initial Object Design Inputs: requirements meetings various Use Cases – 10% complete Key risks addressed with preliminary programming System.
GRASP: Designing Objects With Responsibilities Chapter 17 Applying UML and Patterns -Craig Larman.
Chapter 18 Object Design Examples with GRASP 1CS6359 Fall 2011 John Cole.
GRASP: Designing Objects With Responsibilities
Object Design Examples with GRASP (Ch. 18)
Object-Oriented Analysis and Design Mar 11, 2008.
17. GRASP—Designing Objects with Responsibilities III CSE5324 Lecture Quiz 17 due at 5 PM Thursday, 8 October 2015.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
Design Patterns. Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
Copyright © Craig Larman All Rights Reserved Responsibility-Driven Design with the GRASP Patterns.
Object-Oriented Design Part 2
GRASP: Designing Objects with Responsibilities
IntellAgile Copyright © 2002 Craig Larman. All rights reserved. Object Design and Use- Case Realizations with GRASP Patterns.
What to remember from Chap 13 (Logical architecture)
Object-Oriented Analysis and Design Mar 9, 2008.
TK2023 Object-Oriented Software Engineering CHAPTER 12 Introduction to Responsibility-Driven Design.
1 By Rick Mercer with help from Object-Oriented Design Heuristics, Arthur Riel Coupling / Cohesion.
OO Design Roshan Chitrakar. Analysis to Design Do the RIGHT thing Do the RIGHT thing Requirement Analysis Requirement Analysis Domain Modeling with addition.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
GRASP: Designing Objects With Responsibilities
Chapter 17 Designing with Responsibilities. Fig
References: Applying UML and patterns Craig Larman
OO Methodology Elaboration Phase Iteration 1- Part 3.
Design. 2 The Need for Software Blueprints Knowing an object-oriented language and having access to a library is necessary but not sufficient in order.
Oct 3, Ron McFadyen1 GRASP Patterns 1.Expert 2.Creator 3.Controller 4.Low Coupling 5.High Cohesion.
General Principles in Assigning Responsibilities Responsibilities Responsibility-Driven Design CRC Cards GRASP.
OO Methodology Elaboration Phase Iteration 1- Part 2.
TK2023 Object-Oriented Software Engineering CHAPTER 13d GRASP Patterns: High Cohesion.
Object Design Examples with GRASP
GRASP – Designing Objects with Responsibilities
TK2023 Object-Oriented Software Engineering
Chapter 12: Collaboration Diagram - PART2
Conception OBJET GRASP Patterns
DESIGN MODEL: USE-CASE REALIZATIONS WITH GRASP PATTERNS
TK2023 Object-Oriented Software Engineering
Apply Expert, Creator, Controller, Low Coupling, High Cohesion
GRASP : Designing Objects with Responsibilities
GRASP Design Patterns: Designing Objects with Responsibilities
GRASP (General Responsibility Assignment Software Patterns)
Object Oriented System Design Responsibilities
Presentation transcript:

Copyright © Craig Larman All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference: Larman chapter 17

Copyright © Craig Larman All Rights Reserved 2 Object Design by Responsibilities n “Identify requirements, create a domain model, add methods to the software classes, define messages to meet requirements…” n Too Simple! – What methods belong where? – How do we assign responsibilities to classes? n Object-oriented design is not about UML. It is primarily about assigning Responsibilities to objects.

Copyright © Craig Larman All Rights Reserved 3 Responsibility-Driven Design (RDD) n Responsibility- Contract or obligation of a class n Two types – What must a class “know”? [knowing responsibility] Know about private encapsulated data Know about related objects Know about things it can derive or calculate n What must a class “do”? [doing responsibility] – Do something itself: e.g. create an object, do a calculation – Initiate action in other objects – Control/coordinate actions in other objects n Responsibilities are fulfilled by object methods

Copyright © Craig Larman All Rights Reserved 4 Examples of object responsibilities n “A Sale object is responsible for creating SalesLineItem objects”(doing) n “A Sale object is responsible for knowing its total” (knowing)

Copyright © Craig Larman All Rights Reserved 5 Responsibilities n Responsibilities are an abstraction. – The responsibility for persistence. Large-grained responsibility. – The responsibility for the sales tax calculation. More fine-grained responsibility. n They are implemented with methods in objects. – 1 method in 1 object – 5 methods in 1 object – 50 methods across 10 objects

Copyright © Craig Larman All Rights Reserved 6 Responsibilities in Interaction Diagrams When drawing interaction diagrams, you are implicitly assigning Responsibilities to particular objects.

Copyright © Craig Larman All Rights Reserved 7 Responsibility-Driven Design: GRASP n GRASP: General Responsibility Assignment Software Patterns (or Principles). n GRASP provides basic principles for assignment of Responsibilities to objects. n Think GRASP when drawing interaction diagrams.

Copyright © Craig Larman All Rights Reserved 8 GRASP for Responsibility-Driven Design

Copyright © Craig Larman All Rights Reserved 9 Workflow

Copyright © Craig Larman All Rights Reserved 10 GRASP Patterns Which class, in the general case is responsible? You want to assign a responsibility to a class n You want to avoid or minimize additional dependencies n You want to maximise cohesion and minimise coupling n You want to increase reuse and decrease maintenance n You want to maximise understandability n …..etc.

Copyright © Craig Larman All Rights Reserved 11 Five Fundamental GRASP Patterns 1. Creator: who should be responsible for creating a specific object? 2. Information Expert: who should be responsible for knowing about a particular object? 3. High Cohesion: how should responsibilities be grouped within classes? 4. Low Coupling: how strongly should elements be tied to others? 5. Controller: who should be the first to receive a message in the domain layer?

Copyright © Craig Larman All Rights Reserved Creator n Problem – Who (i.e. what class) creates a new instance of a class A n Solution – Assign class B the responsibility to create an instance of class A if one of these is true: B “contains” A B records A B closely uses A B has the Initializing data for A

Copyright © Craig Larman All Rights Reserved 13 Creator: example n In Monopoly, who creates the Square object n Start point: Look at the Domain Model

Copyright © Craig Larman All Rights Reserved 14 Creator: Solution The Board object contains Square objects, so it is a good candidate. This also gives us a clue that the relationship should be a composition:

Copyright © Craig Larman All Rights Reserved Information Expert (or Expert) n Problem – Who (i.e. what class) should be responsible for knowing about instances of a class A n Solution – Assign a knowing responsibility to the class that has the information needed to fulfill it

Copyright © Craig Larman All Rights Reserved 16 Note n When assigning “knowing” or information responsibilities, first look in the Design model for an appropriate class. n If not found in the design model, then look in the Domain model. n This approach helps in clarifying the Design Model

Copyright © Craig Larman All Rights Reserved 17 Information Expert: example n What class should know about (e.g. status of) Square objects? – Alternatively, who should be the receiver of message getSquare(name) n Solution: – Board knows about all squares, i.e. Board has the information necessary to fulfill this responsibility

Copyright © Craig Larman All Rights Reserved Low Coupling n Problem – How to reduce the impact of change, to support low dependency, and increase reuse? n Solution – Assign a responsibility so that coupling remains low

Copyright © Craig Larman All Rights Reserved 19 Coupling n How strongly one element (e.g. class) is connected to, has knowledge of, or depends on other elements n Illustrated as dependency relationship in UML class diagram

Copyright © Craig Larman All Rights Reserved 20 Coupling In object oriented languages, common form of coupling from TypeX to TypeY include: n TypeX has an attribute that refers to a TypeY instance. n TypeX has a method which references an instance of TypeY, by any means. These typically include a parameter or local variable of type TypeY, or the object returned from a message being an instance of TypeY. n TypeX is a direct or indirect subclass of TypeY. n TypeY is an interface, and TypeX implements that interface.

Copyright © Craig Larman All Rights Reserved 21 Low coupling n Classes are easier to maintain n Easier to reuse n Changes are localised

Copyright © Craig Larman All Rights Reserved 22 Low Coupling: Example Who has responsibility to create a payment (and associate with a sale)?

Copyright © Craig Larman All Rights Reserved 23 Two possibilities: 1. Post 2. Sale Low coupling suggests Sale because Sale has to be coupled to Payment anyway (Sale knows its total).

Copyright © Craig Larman All Rights Reserved Controller n Problem – What object (beyond the UI layer) handles events from external actors – E.g. startup(), playSongs(), etc? n Solution – Assign the responsibility to an object representing one of these choices A facade controller which represents the overall system, root object, device or subsystem Example: Jukebox A use case controller which which represents a use case scenario within which the system operations occurs Example: makeSaleHandler. makeSaleCoordinator, etc. – These classes often don’t do the work, but delegate it to others.

Copyright © Craig Larman All Rights Reserved 25 Controller n A Controller is a non-user interface object responsible for receiving or handling a system event. n A Controller defines the method for the system operation.

Copyright © Craig Larman All Rights Reserved 26 Controller: example

Copyright © Craig Larman All Rights Reserved 27 Note n A common defect in the design of controllers is to give them too much responsibility n Normally, a controller should delegate to other objects the work that needs to be done; it coordinates or controls the activity. It does not do much work itself.

Copyright © Craig Larman All Rights Reserved 28 Types of Controllers 1. Facade controller 2. Use case controller

Copyright © Craig Larman All Rights Reserved 29 Façade Controller n represents the overall system, device, or a subsystem. n a cover, or facade, over the other layers of the application n provides the main point of service calls from the UI layer down to other layers. n could be an abstraction of the overall physical unit, such as a Register n E.g. a class representing the entire software system, such as POSSystem, ChessGame

Copyright © Craig Larman All Rights Reserved 30 When to use Façade controller n when there are not "too many" system events n When it is not possible for the user interface (UI) to redirect system event messages to alternating controllers n E.g. in a message processing system

Copyright © Craig Larman All Rights Reserved 31 Use case controllers n If a use-case controller is chosen, then there is a different controller for each use case. n Note that this is not a domain object; it is an artificial construct to support the system n For example, if the NextGen POS application contains use cases such as Process Sale and Handle Returns, then there may be a ProcessSaleHandler class and so forth

Copyright © Craig Larman All Rights Reserved 32 When to use Use case controller n when placing the responsibilities in a facade controller leads to designs with low cohesion or high coupling n typically when the facade controller is becoming "bloated" with excessive responsibilities. n A use-case controller is a good choice when there are many system events across different processes n it factors their handling into manageable separate classes n provides a basis for knowing and reasoning about the state of the current scenario in progress.

Copyright © Craig Larman All Rights Reserved 33 Controller: Class Activity

Copyright © Craig Larman All Rights Reserved 34 n By the Controller pattern, here are some choices: 1. Register, POSSystem – represents the overall "system," device, or subsystem – Façade controller 2. ProcessSaleHandler, ProcessSaleSession – represents a receiver or handler of all system events of a use case scenario – Use case controller

Copyright © Craig Larman All Rights Reserved 35

Copyright © Craig Larman All Rights Reserved 36 Bloated Controller n Poorly designed, a controller class will have low cohesion- unfocused and handling too many areas of responsibility; this is called a bloated controller. n Signs of bloating include: – only a single controller class receiving all system events in the system, and there are many of them. – The controller itself performs many of the tasks necessary to fulfill the system event, without delegating the work. – A controller has many attributes, and maintains significant information about the system or domain, which should have been distributed to other objects, or duplicates information found elsewhere.

Copyright © Craig Larman All Rights Reserved 37 Cure to Bloated Controller n Add more controllers- system does not have to have only one. Instead of facade controllers, use use-case controllers. n For example, consider an application with many system events, such as an airline reservation system. It may contain the following controllers: n Design the controller so that it primarily delegates the fulfillment of each system operation responsibility on to other objects.

Copyright © Craig Larman All Rights Reserved High Cohesion n Cohesion is a measure of how strongly related the responsibilities of a class are n A class with low cohesion does many unrelated things. It is hard to understand, hard to reuse, and hard to maintain n A class with high cohesion has a relatively small number of highly-related methods n It collaborates with other objects n Modular design

Copyright © Craig Larman All Rights Reserved 39 Example of Good and Bad Cohesion

Copyright © Craig Larman All Rights Reserved 40 Cohesion: example n Example: for the POS system, when we design for the makePayment() system operation Reduces the cohesion of Register.

Copyright © Craig Larman All Rights Reserved 41 n By delegating Payment creation to Sale, we not only decrease coupling, but we also increase the cohesion of Register. Since Register has fewer responsibilities, it is more cohesive.

Copyright © Craig Larman All Rights Reserved 42 Wrap-up n No ‘magic’ to assigning responsibilities n If you don’t have a reason for placing a method in a class, …it shouldn’t be there! n You should be able to say: ‘I placed method X in class Y based on GRASP Z’