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.

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.
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:
February Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m.
Low Coupling High Cohesion
Object-Oriented Analysis and Design
Software Design Yashvardhan Sharma.
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.
CSSE 374: More GRASP’ing and Use Case Realization Steve Chenoweth Office: Moench Room F220 Phone: (812) These.
Object-Oriented Design. From Analysis to Design Analysis Artifacts –Essential use cases What are the problem domain processes? –Conceptual Model What.
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.
1 SAD2 - UML 4 th Lecture Class Diagram in Construction Phase Patterns Case Study Lecturer: Dr Dimitrios Makris
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.
INFO 620Lecture #61 Information Systems Analysis and Design More Class Modeling; Patterns INFO 620 Glenn Booker.
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.
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
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
Next Gen POS Example GRASP again. Same Patterns Different Example!
Object-Oriented Design Principles and Patterns. © 2005, James R. Vallino2 How Do You Design? What principles guide you when you create a design? What.
GRASP: Designing Objects with Responsibilities
IntellAgile Copyright © 2002 Craig Larman. All rights reserved. Object Design and Use- Case Realizations with GRASP Patterns.
Object-Oriented Analysis and Design Mar 9, 2008.
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:
GRASP: Designing Objects With Responsibilities
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.
TK2023 Object-Oriented Software Engineering
Object Design Examples with GRASP
GRASP – Designing Objects with Responsibilities
TK2023 Object-Oriented Software Engineering
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
Presentation on GRASP Patterns Submitted by
Apply Expert, Creator, Controller, Low Coupling, High Cohesion
GRASP : Designing Objects with Responsibilities
GRASP Design Patterns: Designing Objects with Responsibilities
CONTENTS Object-Oriented Modeling Using UML
Next Gen POS Example GRASP again.
Object Oriented System Design Responsibilities
Presentation transcript:

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 to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” – Christopher Alexander (building architect)

Object Technology Patterns A pattern is a named description of a problem and solution that can be applied in new contexts. Patterns codify the principles and idioms that guide experienced developers in their creation of software. What is and isn’t a pattern depends on your point of view.

Object Technology Patterns (2) Design patterns are not about structures such as vectors that can be encoded in classes and reused as is, nor are they complex domain specific designs. Ideally, patterns should include guidance for how they can be applied in novel situations. Many patterns provide guidance for how responsibilities should be assigned to objects.

Responsibilities Doing –Doing something yourself –Initiating action in others –Controlling and coordinating action in others Knowing –About private encapsulated data –About related objects –About things you can calculate or derive

Pattern Names Naming facilitates communication: Fred: “Where do you think we should place the responsibility for printing a Sale? I think Model-View Separation would work well – how about a SaleReportView? Wilma: “I think Expert is a better choice since it is a simple print and the Sale has all the data required in the printout – let the Sale do it.” Fred: “OK, I agree.”

Responsibility Patterns Expert Creator High Cohesion Low Coupling Controller Polymorphism Pure fabrication Indirection Don’t talk to strangers

Expert Solution: Assign a responsibility to the information expert – the class that has the information necessary to fulfill the responsibility. Problem: What is the basic principle by which responsibilities are assigned in object-oriented design? Example: Who should be responsible for knowing the grand total of a sale? According to the expert pattern, we look for the class that has the information needed to determine the total.

Expert (2) SaleLineItem Product Specification date time quantity upc price description 1..*1*1 Who has all the information needed for the grand total? Who has the information for the line item subtotals?

Expert (3) :Sale :LineItem l*: [for each] li := next( ) 2*: [for each] subtotal( ) li {local} total( ) :Product Specification 2.1: price ( ) Responsibilities for knowing total, subtotal, and price. :LineItem

Expert (4) Most used pattern for assignment of responsibility. Leads to designs where software objects do operations that are normally done to the corresponding real world object. When information is spread around, leads to collaboration among partial experts. Encourages more cohesive “lightweight” classes due to distributed behavior – high cohesion. Encapsulation is maintained – low coupling.

Creator Solution: Assign class B the responsibility to create an instance of class A if one of the following is true: –B aggregates or contains A objects –B records instances of A objects –B closely uses A objects –B has the initializing data for A objects (B is an Expert with respect to creating A objects). Problem: Who should be responsible for creating a new instance of some class? Goals are to support low coupling, increased clarity, encapsulation, and reusability.

Creator (2) Example: Who should be responsible for creating a LineItem of a sale? Since a Sale aggregates LineItem objects, the Creator pattern suggests that Sale is a good candidate to have the responsibility of creating LineItem instances. :Sale:LineItem makeLineItem(upc, quantity )create(upc, quantity )

Creator (3) Find a creator which needs to be connected to the created object anyway. Supports low coupling. The class of the created object is likely to be already visible to the creator class. If the creator class is chosen based on having the initializing data, this is an example of the Expert pattern.

Low Coupling Solution: Assign a responsibility so that coupling remains low. Problem : How to support low dependency and increased reuse? A class with too many strong connections to other classes: –May need change when related classes change –Is harder to understand in isolation –Is harder to reuse because it requires the presence of classes on which it is dependent Example : Who should be responsible for creating a Payment object and associating it with a Sale?

Low Coupling (2) In the real world, POST “records” a Payment, so Creator suggests: :POST :Payment :Sale makePayment( ) 2: addPayment(p) 1:p := create ( )

Low Coupling (3) :POST:Sale :Payment makePayment( )1: makePayment( ) 1.1: create ( ) But Low Coupling suggests:

Low Coupling (4) Low Coupling is an evaluative pattern applied by a designer while evaluating designs. In OO languages, common forms of coupling include: –Class X has a method that references an instance of Y (parameter, local variable, return value) –Class X has an attribute that refers to an instance of Y –Class X is a subclass of Y –Class X is an implementation of interface Y

High Cohesion Solution: Assign a responsibility so that cohesion remains high. Problem : How to keep complexity manageable? A class that has too many responsibilities or unrelated responsibilities is: –Hard to comprehend –Hard to reuse –Hard to maintain –Fragile, constantly effected by change Low cohesion classes often represent a very “large-grain” of abstraction, or have taken on responsibilities that should have been delegated to other classes.

High Cohesion (2) Example: :POST :Payment :Sale makePayment( ) 2: addPayment(p) 1:p := create ( ) :POST:Sale :Payment makePayment( )1: makePayment( ) 1.1: create ( )

High Cohesion (3) Assignment of responsibility for Payment creation to POST may be acceptable, but if a trend of assigning system operations to POST develops it may become a bloated and incohesive class. The second design delegates responsibility to Sale, which supports greater cohesion in POST.

Controller Solution: Assign the responsibility for handling a system event message to a class representing one of the following choices: –Represents the overall “system” (facade controller) –Represents the overall business or organization (facade controller) –Represents something active in the real world that might be involved in the task (role controller) –Represents an artificial handler of all system events of a use case (use case controller)

Controller (2) Problem: Who should be responsible for handling a system event? –System events are high level events generated by external actors. –Controllers are non-user interface objects responsible for handling a system event. They define methods for system operations.

Controller (3) Example: During system behavior analysis, system operations were assigned to the class System: Who should control system events such as enterItem? :??? enterItem(upc, quantity ) System endSale ( ) enterItem ( ) makePayment ( )

Controller (4) The Controller pattern suggests one of four possibilities: 1. POST– represents the overall system 2. Store – represents the overall business 3. Cashier – represents something active in the real world that might be involved in the task 4. BuyItemHandler – represents an artificial handler of all system events of a use case The choice is influenced by other factors such as cohesion and coupling.

Controller (5) System events should be controlled in the domain layer of objects, not in the interface or presentation layer. The same controller class should be used for all system events of a given use case. Controllers should normally delegate the work to other objects while coordinating the activity.

Controller (6) Facade controllers are suitable when there are only a few system events, or when it is impossible to redirect event messages to multiple controllers. Use case controllers are suitable when the other choices lead to low cohesion or high coupling: –When an existing controller becomes bloated –When there are many system events across different processes – factor into separate manageable classes.

Controller (7) Role controllers are acceptable when the designer is careful to avoid creating an incohesive controller that does not delegate. –Don’t create “person-like” objects to do all the work. Message handling applications may combine the facade and command patterns –For each message type there is a corresponding command class with an execute method

Key Points Patterns capture the essential elements of the best existing designs. Patterns should include guidance for how they can be applied in novel situations. Patterns that provide guidance for how responsibilities should be assigned to objects include: –Expert, Creator, High Cohesion, Low Coupling, and Controller