GRASP: Designing Objects With Responsibilities

Slides:



Advertisements
Similar presentations
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Advertisements

Object-Oriented Analysis and Design
Object Design Examples with GRASP
Object-Oriented Analysis and Design CHAPTER 17, 25: GRASP PATTERNS 1.
UNIFIED PROCESS.
Oct 2, Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
March Ron McFadyen1 Ch 17: Use Case Realizations with GRASP Patterns Assigning responsibilities to objects to achieve user goals Section 17.4.
Feb R. McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
NJIT Object Design Examples with GRASP Chapter 18 Applying UML and Patterns Craig Larman Presented By : Ajay Alegonda.
February Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m.
Object-Oriented Software Engineering Practical Software Development using UML and Java Design Patterns Sources: Chapter 6: Using Design Patterns, and Chapter.
Object-Oriented Analysis and Design
GRASP : Designing Objects with Responsibilities
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.
Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative Development Part III Elaboration Iteration I – Basic1.
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 )
1 Ch 18. Object Design Examples With Grasp Objectives Design use case realizations. Apply GRASP to assign responsibilities to classes. Apply UML to illustrate.
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.
GRASP: Designing Objects With Responsibilities Chapter 17 Applying UML and Patterns -Craig Larman.
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.
Use Case Model Operation Contracts Chapter 11 Applying UML and Patterns Craig Larman.
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.
Review ♦ System sequence diagram ♦ Domain model
Object-Oriented Design Part 2
1 Lecture 6: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations.
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.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Fall 2009ACS-3913 Ron McFadyen1 Use Case Realizations with GRASP Patterns “The assignment of responsibilities and design of collaborations are very important.
TK2023 Object-Oriented Software Engineering CHAPTER 12 Introduction to Responsibility-Driven Design.
OO Design Roshan Chitrakar. Analysis to Design Do the RIGHT thing Do the RIGHT thing Requirement Analysis Requirement Analysis Domain Modeling with addition.
Copyright © Craig Larman All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference:
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.
1 Chapter 9: Operation Contracts Chapter 13 in Applying UML and Patterns Book.
Use-Case Model: Adding Detail with Operation Contracts.
1 Design Model Use-Case realizations with GRASP Larman chapter 17.
Oct 3, Ron McFadyen1 GRASP Patterns 1.Expert 2.Creator 3.Controller 4.Low Coupling 5.High Cohesion.
1 Object Oriented Analysis and Design System Events & Contracts.
OO Methodology Elaboration Phase Iteration 1- Part 2.
Object Design Examples with GRASP
GRASP – Designing Objects with Responsibilities
System Sequence Diagrams and Operation Contracts
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
Presentation transcript:

GRASP: Designing Objects With Responsibilities Chapter # 16 Applying UML and Patterns -Craig Larman

Designing Objects With Responsibilities “Identify requirements, create a domain model and define dynamic behaviour , define messages to meet requirements , add methods to the software classes …” Too Simple! How do we assign responsibilities to classes? What methods belong where?

Object Design : Input Use case text System sequence diagram Defining the behavior System sequence diagram Identifying the system operation messages Supplementary specification Defines non-functional goals Glossary Data format, data related with UI and database Domain model initial attempt of software object in the domain layer of software architecture The operation contract Stating events to design for, and detailed post-condition to satisfy

Fig. 17.1 UP artifacts influencing OO design

Responsability Driven Design-RDD Design of behavior implies assigning responsibilities to software classes. Responsibilities are assigned to classes of objects during object design. Responsibility is a contract or obligation of a class What must a class “know”? [knowing responsibility] What must a class “do”? [doing responsibility] Obligation : Duty , Responsibility

Doing Responsability What must a class “do”? [doing responsibility] Take action (create an object, do a calculation) Initiate action in other objects Control/coordinate actions in other objects Doing responsibilities are implemented by means of methods Methods fulfill responsibilities alone or through collaboration with other objects and methods. Ex: A Sale is responsible for creating SalesLineItems” (doing)

Responsibilities and methods : create Sale objects are given a responsibility to create Payments. The responsibility is invoked with a makePayment message

knowing responsibility What must a class “know”? [knowing responsibility] Private encapsulated data Related objects Things it can derive or calculate Knowing responsibilities are related to attributes, associations in the domain model. Domain model illustrates attributes and associations => inspires the “knowing” responsibilities. Ex : a Sale is responsible for knowing its total” (knowing)

Responsibilities and attribute

RDD and Collaboration Responsibilities are implemented by methods Some methods act alone and do a task Some collaborate with other objects to fulfill their responsibility. Example: Sale class has getTotal() method, the getTotal() collaborates with SalesLineItem objects to get the subtotal through getSubtotal() methods

Software pattern What is a software pattern? A design pattern is a general reusable and proven solution to a commonly occurring problem in software design. Craig Larman: “ a pattern is a named problem/solution pair that can be applied in new contexts, with advice on how to apply it in the situations”

Well‐known Pattern Families GRASP = General Responsibility Assignment Software Patterns Describe fundamental principles for assigning responsibilities to classes and for designing interactions between classes GoF: Gang of Four Design Patterns : 23 pattrens We’ll cover with GRASP

GRASP Patterns 9 GRASP patterns , but we start with the first five Information Expert Creator Controller Low Coupling High Cohesion Polymorphism Pure Fabrication. Indirection.

Creator

Creator principle B “records” A B “closely uses” A Problem: Who creates an A object Solution: Assign class B the responsibility to create an instance of class A if one of these is true B “contains or aggregate ” A B “records” A B “closely uses” A B “ has the Initializing data for ” A

Creator principle B “has the Initializing data for ” A that will be passed to A when it is created. Often initiation is done using a constructor with parameters. e.g. a Payment instance, when created needs to be initialized with the Sale total. Sale class knows Sale total. Good candidate for creating Payment is Sale. If more than one of the above applies, prefer a class B which aggregates or contains A.

Problem Who should create a SalesLineItem?

Creating a SalesLineItem Sale objects are given a responsibility to create SaleLineItem. The responsibility is invoked with a makeLineItem message

Creating a SalesLineItem

Information Expert

Information Expert Problem : What is a basic principle by which to assign responsibilities to objects? Solution (advice ) : Assign a responsibility to the information expert , that is the class with the information necessary to fulfill the responsibility. “Objects do things related to the information they have.” Customer Shopping cart items

Applying Expert in POS Application Start assigning responsibilities by clearly stating the responsibility. Who should be responsible for knowing the grand total of a sale?

Who should be responsible for knowing/getting the grand total of a sale?

Who responsible for knowing the grand total of a sale?

Partial interaction and class diagrams Add a Sale class to the Design Model. Express responsibility of knowing the total of a sale with the method named getTotal. What information do we need to know to determine the line item subtotal? Sale knows about neighbours (associations), SaleLineitems who is responsible for knowing its subtotal

SalesLineItem is Expert for Subtotal How does the SalesLineItem find out the product price? SaleLineItem knows about neighbours ( ProductDescription) to get the price.

Product Description is Expert for Price “Partial” information experts collaborate to fulfill the responsibility.

Low Coupling Principle

“Low Coupling” Principle Problem: How to support low dependency, Low change impact, and increased reuse? Solution: Assign responsibilities so that coupling remains low. Use this principle to evaluate alternatives. Coupling is a measure of how strongly one class is connected to, has knowledge of, or relies upon other classes.

What is a coupling ? Coupling between classes is dependency of one class on another class Common form of coupling from Class A to Class B are: Class A has an attribute (data member or instance variable) that refers to a Class B instance, or Class B itself.

What is a coupling ? Class A has a method which references an instance of Class B, or Class B itself, by any means. These typically include a parameter or local variable of type Class B, or the object returned from a message being an instance of Class B. Class A is a direct or indirect subclass of Class B. Class B is an interface, and Class A implements that interface.

Low Coupling - POS Case Study What class should be responsible for creating a Payment instance and associating it with the Sale? Register? Sale? Creator pattern suggests Register should create the Payment. A register records a payment in the real world.

What if Register creates Payment Register is coupled to both Sale and Payment.

What if Sale creates Payment ? Assuming that the Sale must eventually be coupled to knowledge of a Payment, having Sale create the Payment does not increase coupling.

Controller Pattern

Controller Pattern UI layer does not contain any business logic Problem: How to connect UI layer to the business logic layer? Solution: If a program receive events from external sources other than its graphical interface, add an event class to decouple the event source(s) from the objects that actually handle the events.

Controller Pattern What first object beyond the UI layer receives and coordinates (“controls”) a system operation message? Solution: Assign the responsibility to a class that represents one of the following options:

Options for Control Responsibility Represents the overall system or a root object. e.g., an object called System or Register Suitable when there are not too many system events or when UI cannot choose between multiple controllers. A controller for each use case e.g. processSaleHandler

What should be Controller for enterItem?

Bad Design

Good Design Controller should representative for the work that needs to be done to other objects.

A use case controller handles system events for a single use case. Can maintain information about the state of the use case. Different controller for each use case. Not a domain object, but artificial construct to support the system. Use when there are many system events. Factors handling into separate classes.

Controller

Controller: Benefits Increased potential for reuse Ensures that the application logic is not handled in the interface layer. Thus, the external event raisers are independent of internal event handlers Plug & Play interfaces Since the interface is not bound to the controllers, it can be replaced or updated without much impact Verifying the reasoning of the use case Allows us to verify that the system operations occur in a logical sequence. For example: makePayment() is not called before endSale()

High Cohesion

High Cohesion A class with low cohesion does too much unrelated work and are: Hard to comprehend Hard to reuse. Hard to maintain. Delicate and constantly affected by change Cohesion is a measure of how strongly related the responsibilities of an element (classes, subsystems) are.

High Cohesion Problem How to keep complexity manageable? Solution Assign a responsibility so that cohesion remains high

Reduced cohesion of Register(creator pattern) Low cohesion: Register is taking part of the responsibility for fulfilling “makePayment” operation and many other unrelated responsibility ( 50 system operations all received by Register).then it will become burden with tasks and become incohesive

Better solution Higher Cohesion and Lower Coupling Delegate the payment creation responsibility to “Sale” to support high cohesion

Conclusion Like Low Coupling, High Cohesion is a principle to keep in mind during all design decisions It is important to evaluate design constantly with respect to GRASP principles.

Object Design Examples with GRASP Chapter 17 Applying UML and Patterns Craig Larman

Use-Case Realizations UML interaction diagrams are used to illustrate use-case realizations. Design operations of the SSD Principles and Patterns can be applied during this design work. Which object has which responsibility?

Domain Model and Use-Case Realization Some of the software objects that interact via messages in the interaction diagrams are inspired from the Domain Model. The existing domain model is not likely to be perfect. Errors and omissions are to be expected. You will discover new conceptual classes that were previously missed, ignore conceptual classes that were previously identified, and do likewise with associations and attributes.

Interaction Diagrams Your System Sequence Diagram list the system events. Create a separate diagram for each system operation(all events of the use case) under development in the current iterative step. If the diagram gets complex, split it into smaller diagrams.

Multiple Sequence Diagrams ( Multiple sequence diagrams and system event message handling.

Use-case Realizations for NextGen Iteration Explore the choices and decisions made while designing a use-case realization, with objects based on the GRASP patterns. No magic in the creation of well designed ID’s. Construction of ID’s is made on justifiable principles.

System Operations of POS Application

Interaction Diagrams and System Events In the current iteration of PoS application, we are considering two use-cases and their associated system events: Process Sale makeNewSale enterItem endSale makePayment Start Up !!!!!!! startUp

Object Design : Contract Name : makeNewSale() Responsibilities : Make a new sale for a cashier to request to start a new sale, after a customer has arrived with things to buy. Cross-References: Use Cases : Process Sale Pre-Conditions : None Post-Conditions : A sale instance was created. The sale instance was associated with the register . Attributes of the sale instance were initialized.

Choosing the Controller Class Our design choice involves choosing the controller for the system operation message Here are some choices: Represents the overall system, device, or subsystem: Register, Represents a handler of all system events of a use-case scenario : ProcessSaleHandler. From those choices, we choose the register as our controller

Creating a New Sale A software sale object must be created, and the GRASP creator pattern suggests assigning the responsibility for creation to a class that aggregates, contains, or records the object to be created. The register is a good choice to create the sale object by the creator pattern.

Creating a New Sale

Object Design : enterItem Name: enterItem(itemID : itemID,quantity : integer) Responsibilities : Enter sale of an item and add it to the sale. Display the item description and price. Cross References : Use-Cases : ProcessSale. Pre-Conditions : There is an underway sale.

Object Design : enterItem Post-Conditions sli was associated with current sale.(association formed) sli.quantity became quantity.(attribute modification) sli was associated with a ProductSpecification, based on itemID match.(association formed), that is also should find its ProductSpecification

Creating a new SalesLineItem The contract post condition indicate a responsibility to create a SalesLineItem instance. A Sale contains SalesLineItem objects. By creator pattern, the sale is an appropriate candidate for creating Lineitems. GRASP creator pattern Assign the responsibility for creation to a class that aggregates, contains or records.

Finding a product specification The SalesLineItem needs to be associated with the ProductSpecification that matches the incoming itemID. Who should be responsible for looking up the ProductSpecification based on the itemID match? By the Expert pattern, ProductCatalog is a good candidate for the responsibility, since it logically contains all the ProductSpecifications.

Visibility to a product catalog Who should send the Specification message to the ProductCatalog to ask for a ProductSpecification? Assumption: A Register and a ProductCatalog instances were created during the initial Start Up use-case, and there is a permanent connection between them. Based on this assumption, then it is possible to the Register to send the specification message (using reference to productCatalog) to the ProductCatalog.

Object Design : endSale Contract Name : endSale() Responsibilities : Record that it is the end of entry of sale items, and display sale total. Cross References : Use Cases : Process Sale Exceptions : If a sale is not underway, indicate that it was an error. Pre-Conditions : There is an underway sale. Post-Conditions : sale.isComplete became true.

Choosing the Controller Class Based on the Controller Pattern, as for enterItem, we will continue to use register as a controller.

Setting the Sale.isComplete attribute We will continue to use register as a controller The contract post-condition state: Sale.isComplete became true. As always, Expert should be the first pattern considered unless it is a controller or creation problem (which is not). Who should be responsible for setting the Sale.isComplete attribute of the Sale to true. By Expert, it should be the Sale itself.

Communication Diagram : endSale Setting the Sale.isComplete attribute (2)

Constraints and Notes

Object Design:makePayment Choosing the Controller Class Based on the Controller GRASP pattern, as for enterItem, we will continue to use Register as a controller. It is common to use the same controller throughout a use case.

Creating the Payment Register – makePayment Communication diagram

Logging the Sale Who should be responsible for knowing the complete sale.

Logging a completed sale Perhaps we did not think of a sale ledger early, but now we have. If it is not chosen, it would be ideally added to the domain model as well. Fortunately, iterative development provides a life-cycle for continual change. This kind of discovery and change during design work is to be expected.

Logging a Completed Sale

Calculating the Balance Who is responsible for knowing the balance? To calculate the balance, the Sale and Payment cash tendered are required. Therefore, Sale and Payment are partial experts for solving the problem.

Calculating the Balance