Presentation on GRASP Patterns Submitted by

Slides:



Advertisements
Similar presentations
GRASP: Designing Objects with Responsibilities
Advertisements

Object Design Examples with GRASP
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:
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
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.
Object-Oriented Software Engineering Practical Software Development using UML and Java Design Patterns Sources: Chapter 6: Using Design Patterns, and Chapter.
Low Coupling High Cohesion
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.
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.
GRASP PATTERNS Information Expert (Expert) Creator Controller © Lethbridge/Laganière 2001 Chapter 6: Using design patterns1.
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.
GRASP: Designing Objects With Responsibilities Chapter 17 Applying UML and Patterns -Craig Larman.
GRASP: Designing Objects With Responsibilities
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.
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
Object-Oriented Design Part 2
Next Gen POS Example GRASP again. Same Patterns Different Example!
GRASP: Designing Objects with Responsibilities
Object-Oriented Analysis and Design Mar 9, 2008.
OO Methodology Elaboration Iteration 2 - Design Patterns -
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:
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.
GRASP: More Patterns for Assigning Responsibilities Presented By Dr. Shazzad Hosain.
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.
TK2023 Object-Oriented Software Engineering
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,
Object Design Examples with GRASP
GRASP – Designing Objects with Responsibilities
TK2023 Object-Oriented Software Engineering
Presentation on GoF Design Patterns Submitted by WWW. ASSIGNMENTPOINT
GoF Patterns (GoF) popo.
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
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
GRASP Design Patterns: Designing Objects with Responsibilities
GRASP (General Responsibility Assignment Software Patterns)
GoF Design Patterns (Ch. 26)
Next Gen POS Example GRASP again.
Object Oriented System Design Responsibilities
Presentation transcript:

Presentation on GRASP Patterns Submitted by WWW.ASSIGNMENTPOINT.COM

Patterns A pattern describes a problem and solution, and given a name. Examples are Singleton, Adapter, Factory etc. A pattern Example Name: Information Expert Context/Problem: What is a basic principle by which to assign responsibilities to objects? Solution: Assign a responsibility to the class that has the information needed to fulfill it. www.assignmentpoint.com

Repeating Patterns The very term Pattern means a repeating thing. The point of pattern is not to express new design ideas Instead patterns codify existing tried-and-true knowledge, idioms, and principles: the more honed and widely use, the better. www.assignmentpoint.com

Naming Patterns Improves Communication Engineers can discuss among themselves a complex principle or design idea with a simple name. Fred: “Where do you think we should place the responsibility for creating a SalesLineItem? I think a Factory.” Wilma: “By Creator, I think Sale will be suitable.” Fred: “Oh, right – I agree”. www.assignmentpoint.com

GRASP Patterns Question: What is GRASP patters? Answer: They describe fundamental principles of object design and responsibility assignment, expressed as patterns. GRASP stands for General Responsibility Assignment Software Patterns. Information Expert Creator High Cohesion Low Coupling Controller www.assignmentpoint.com

1. Information Expert (or Expert) Context/Problem: What is a basic principle by which to assign responsibilities to objects? Solution: Assign a responsibility to the class that has the information needed to fulfill it. Example: In NextGen POS application, some class needs to know the grand total of a sale. Start by asking “Who should be responsible for knowing the grand total of a sale?” By Information Expert we should look for a class that has the information needed to determine the total. Where should we look, domain or design model? First look into design model, if not then look into domain model www.assignmentpoint.com

Domain Model vs. Design Model Let’s assume we are starting from domain model www.assignmentpoint.com

NextGen POS System Domain Model Who has the information to determine the sales total? www.assignmentpoint.com

Add Responsibilities So we add a software class into design model similarly called Sale, and give the responsibility of knowing its total, expressed with the method named getTotal (). A responsibility is not the same thing as a method, but methods are implemented to fulfill responsibilities. Responsibilities are implemented using methods that either act alone or collaborate with other methods and objects www.assignmentpoint.com

Types of Responsibility Two types of responsibilities, knowing and doing. Knowing responsibilities of an object include: Knowing about private encapsulated data Knowing about related objects Knowing about things it can derive or calculate E.g. Sale object is responsible for knowing its total. Doing responsibilities of an object include: Doing something itself e.g. creating object or doing a calculation Initiating action in other objects Controlling and coordinating activities in other objects E.g. a Sale is responsible for creating SalesLineItems” (a doing) www.assignmentpoint.com

Associations of Sale www.assignmentpoint.com

Determining Sales Total Determining sales total needs SalesLineItem.quantity and ProductSpecification.price. By Information Expert SalesLineItem should determine the subtotal. Thus Sale should send getSubtotal messages to each of SalesLineItems and sum the results. www.assignmentpoint.com

Determining Sales Total ProductSpecification is an information expert on answering its price; Therefore, a message must be sent to it asking for its price. www.assignmentpoint.com

Implementation Classes Class ProductSpecification String desription; float price ; int itemID void setItemID (int itemID) { this.itemID = itemID ; } int getItemID () { return this.itemID ; void setPrice (float price) float getPrice () void setDescription (String desc) String getDescription () End Class Class SalesLineItem ProductSpecification ps ; int quantity ; void setProductSpecification (…) Pro..Spec.. getProductSpecification () void setQuantity (int qty) int getPrice () void setDescription (String desc) String getDescription () float getSubtotal (){ return quantity * ps.getPrice () } End Class www.assignmentpoint.com

LinkedList <SalesLineitem> sliList; Date date ; Time time ; Class Sale LinkedList <SalesLineitem> sliList; Date date ; Time time ; float getTotal (){ float total = 0 ; for each item in sliList total = total + item.getSubTotal () end For return total ; } End Class Class SalesLineItem ProductSpecification ps ; int quantity ; void setProductSpecification (…) Pro..Spec.. getProductSpecification () void setQuantity (int qty) int getPrice () void setDescription (String desc) String getDescription () float getSubtotal (){ return quantity * ps.getPrice () } End Class www.assignmentpoint.com

Contradictions Sometimes a solutions suggested by Information Expert is undesirable, usually because of problems in coupling and cohesion. Who should be responsible for saving a Sale in a database? Certainly Sale object contains much of the information so Sale object is the natural choice. But then Sale class mush have codes related to SQL, JDBC etc. Related Patterns Low Coupling High Cohesion www.assignmentpoint.com

GRASP Patterns Information Expert Creator Low Coupling High Cohesion Controller www.assignmentpoint.com

2. Creator Context/Problem: Who should be responsible for creating a new instance of some class? Solution: Assign class B the responsibility to create a class A, if: B aggregates A objects B contains A B records instances of A B closely uses A B has the initializing data that will be passed to A when it is created Example: In NextGen POS application, who should be responsible for creating a SalesLineItem instance? www.assignmentpoint.com

Creating a SalesLineItem www.assignmentpoint.com

Finding Creator Class Sometimes a creator is found by looking for the class that has the initializing data that will be passed in during creation. Example: A Payment instance needs to be initialized, when created, with the Sale total. A Payment object will create Sale object or A Sale object will create Payment object Since Sale knows the total, Sale is a candidate creator for Payment www.assignmentpoint.com

Contradictions Often creation requires significant complexity such as conditional creation based on external property, using recycled instances for performance reason etc. Delegate creation to a helper class called a Factory rather than suggested by Creator. Related Patterns Low coupling Factory www.assignmentpoint.com

GRASP Patterns Information Expert Creator Low Coupling High Cohesion Controller www.assignmentpoint.com

3. Low Coupling Context/Problem: How to support low dependency, low change of cost and increased reuse? Solution: Assign a responsibility so that coupling remains low Coupling is a measure of how strongly one element is connected to, has knowledge of, or relies on other elements. Low coupling means not dependent on too many elements e.g. classes, subsystems etc. High Coupling has the following problem Changes in related classes force local changes Harder to understand in isolation Harder to reuse because its use requires other classes on which it is dependent. www.assignmentpoint.com

Low Coupling Example Consider the following partial class diagram form POS study Assume we need to create a Payment instance and associate with Sale. What Class should be responsible for this? Creator pattern suggests Register as a candidate because Register “records” a Payment. UML notation www.assignmentpoint.com Register class is coupled with Payment class Design 1

Low Coupling Example Alternatively create Payment and associate with the Sale class Which design is best? In any case Sale must eventually be coupled to knowledge of a Payment. So design 2 is better. www.assignmentpoint.com

Suggestions In practice, the level of coupling alone can't be considered in isolation from other principles such as Information Expert and High Cohesion. Nevertheless, it is one factor to consider in improving design. There is no absolute measure of when coupling is too high. It depends on developer’s design skill. Even extremely low coupling is not desirable. OO systems means “a system of connected objects that communicate via messages.” Extreme low/zero coupling means single object with lot of responsibilities that is in-cohesive, bloated and complex. Moderate degree of coupling is normal and necessary in OOA/D www.assignmentpoint.com

Contradictions High coupling to stable elements and to pervasive elements is seldom a problem. For example, J2EE application can safely couple to the Java libraries, because they are stable and widespread. Pick your battles It is not high coupling per se that is the problem; it is high coupling to elements that are unstable in some dimension such as their interface, implementation, or mere presence. www.assignmentpoint.com

Pick the Battles Pick the battles between lowering coupling and encapsulating things. Focus on points of realistic high instability or evolution Example: In NextGen project, different third-party tax calculators (with unique interface) need to be connected to the system. Therefore designing for Low Coupling at this point is practical. Benefits Not affected by change in other components Simple to understand in isolation Convenient to reuse. Related patters Protected Variation www.assignmentpoint.com

GRASP Patterns Information Expert Creator Low Coupling High Cohesion Controller www.assignmentpoint.com

4. High Cohesion Context/Problem: How to keep complexity manageable? Solution: Assign a responsibility so that cohesion remains high. Cohesion: In object design cohesion is a measure of how strongly related and focused the responsibilities or an element are. Object with high cohesion does not do a tremendous amount of work. Low cohesion does many unrelated things. www.assignmentpoint.com

High Cohesion Example In this case probably its ok because there are only three classes. But if there are fifty system operations, all received by Register, then it would become bloated in-cohesive object. www.assignmentpoint.com

High Cohesion Example This Register class is highly cohesive. www.assignmentpoint.com

Cohesion and Coupling, Yin and Yang Bad cohesion usually begets bad coupling, and vice versa They have inter-dependant influence, thus the term “yin and yang of software engineering”. Example: consider a GUI widget class that represents and paints a widget, saves data to a database, and invokes remote object services. It is not only profoundly in-cohesive, but it is coupled to many disparate elements. www.assignmentpoint.com

GRASP Patterns Information Expert Creator Low Coupling High Cohesion Controller www.assignmentpoint.com

5. Controller Context/Problem: Who should be responsible for handling an input system event? Solution: Assign system handling responsibility to a class if: Represents the overall system, device or sub-system (façade controller) Represents a use case scenario within which the system event occurs, often named <UseCaseName>Handler, <UseCaseName>Coordinator, <UseCaseName>Sesssion www.assignmentpoint.com

Controller Example www.assignmentpoint.com

Choice of Controllers A controller is a non-user interface object responsible for receiving or handling a system event. First category of controller is a façade controller representing the overall system, device or a subsystem. Suitable when there are not too-many system events. Second category is a use-case controller, when there should be many controllers Façade Controller Use-case Controller www.assignmentpoint.com

Allocation of System Operations Façade Controller Use-case Controllers www.assignmentpoint.com

Desirable Coupling between Interface Layer and Domain Layer www.assignmentpoint.com Desirable Coupling between Interface Layer and Domain Layer

Less Desirable Coupling between Interface Layer and Domain Layer www.assignmentpoint.com Less Desirable Coupling between Interface Layer and Domain Layer