ADVANCED SOFTWARE DESIGN LECTURE 7 GRASP

Similar documents
ADVANCED SOFTWARE DESIGN LECTURE 4 GRASP. Dave Clarke

Responsibilities. Using several specific design principles to guide OO design decisions.

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

GRASP 2. CSC 440: Software Engineering Slide #1

GRASP Design Patterns A.A. 2018/2019

Assigning Responsibilities by Larman

2 GRASP Patterns and basic OO Design. Roel Wuyts OASS

References: Applying UML and patterns Craig Larman

Information Expert (or Expert)

17. GRASP: Designing Objects with Responsibilities

GRASP: Patterns for. chapter18

COMP 6471 Software Design Methodologies

Principles of Software Construction: Objects, Design, and Concurrency. Assigning Responsibilities to Objects. toad. Jonathan Aldrich Charlie Garrod

Principles of Software Construction: Objects, Design and Concurrency. Object-Oriented Design: Assigning Responsibilities.

Four More GRASP Principles CSSE 574: Session 5, Part 2

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

Assigning Responsibilities (Patterns of Responsibility Assignment Principles: GRASP)

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

On to Object-oriented Design

Object Analysis & Design in the textbook. Introduction to GRASP: Assigning Responsibilities to Objects. Responsibility-Driven Design

Object-Oriented Design

Object-Oriented Design

GRASP: MORE PATTERNS FOR

GRASP ing at the First 5 Patterns Principles CSSE 574: Session 3, Part 4

2009 Shawn A. Bohner. Shawn Bohner Office: Moench Room F212 Phone: (812)

Mapping Designs to Code

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

Object-Oriented Design II - GRASP

PART 4 Elaboration Iteration 2 More Patterns

Software Engineering

Patterns and Testing

OO Design2. Design Artifacts

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

COMP 6471 Software Design Methodologies

Tecniche di Progettazione: Design Patterns

Constantinos Constantinides Computer Science and Software Engineering Concordia University Montreal, Canada

CTIS 359 Principles of Software Engineering SOFTWARE DESIGN OO(A)D

Logical Architecture & Design Preliminaries

Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3

Object-Oriented Design I

OODP Session 5a. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Domain Modeling. CSSE 574: Week 1, Part 3. Steve Chenoweth Phone: Office (812) Cell (937)

Assigning Responsibilities

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

CSSE 374: Logical Architecture. Shawn Bohner Office: Moench Room F212 Phone: (812)

BDSA08 Advanced Architecture

What is a Model? Copyright hebley & Associates

VEL TECH HIGH TECH Dr. RANGARAJAN Dr. SAKUNTHALA ENGINEERING COLLEGE UNIT 1 UML DIAGRAMS

OODP OOAD Session 7a

Design Engineering. Overview

ADVANCED SOFTWARE DESIGN LECTURE 4 SOFTWARE ARCHITECTURE

Avancier Methods. Very basic design patterns. It is illegal to copy, share or show this document

On to Object-oriented Design

Design patterns. Jef De Smedt Beta VZW

Object-Oriented Design II

A4 Explain how the Visitor design pattern works (4 marks)

Software Modeling & Analysis

Design. Furthermore, it is natural to discover and change some requirements during the design and implementation work of the early iterations.

Software Engineering

Domain Model and Domain Modeling

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

18.1 Definitions and General OO Principles

Final Exam. Final Exam Review. Ch 1: Introduction: Object-oriented analysis, design, implementation. Exam Format

Applying UML & Patterns (3 rd ed.) Chapter 15

Goal: build an object-oriented model of the realworld system (or imaginary world) Slicing the soup: OOA vs. OOD

Domain Modeling- 2. Generalization

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Object Oriented Analysis and Design CS6502

4 Software models. often more than what people can handle not necessary to know all details at all times

Object Oriented. Analysis and Design

Object-Oriented Design

SE203b: OO Design for Software Engineers. Office: TEB349, Ext

Modeling Dynamic Behavior

Object-Oriented Design I - SOLID

Software Design and Analysis CSCI 2040

CSSE 374: GRASP ing at the First Five Patterns Principles. Shawn Bohner Office: Moench Room F212 Phone: (812)

From designing to coding

Appendix A - Glossary(of OO software term s)

Final Exam CISC 475/675 Fall 2004

Software Design and Analysis CSCI 2040

CS6502- OBJECT ORIENTED ANALYSIS AND DESIGN UNIT I

Chapter 8: Class and Method Design

Modeling Dynamic Behavior

Sequence Diagram. A UML diagram used to show how objects interact. Example:

Sequence Diagram. r: Register s: Sale

Chapter 15 UML Interaction Diagrams. Larman, C. Applying UML and Patterns. 3rd Ed. Ed. Prentice-Hall: 2005.

Application Architectures, Design Patterns

Software Architecture With ColdFusion: Design Patterns and Beyond Topics Outline Prepared by Simon Horwith for CFUnderground 6

CSE 70 Final Exam Fall 2009

Summary of the course lectures

PROCESSI DI PRODUZIONE E GESTIONE DEL SOFTWARE. Analysis and Design with UML. Paola Turci

1 Software Architecture

Review Software Engineering October, 7, Adrian Iftene

Object-Oriented Design

02291: System Integration

OBJECT ORIENTED ANALYSIS AND DESIGN SYLLABUS

SE310 Analysis and Design of Software Systems

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Transcription:

ADVANCED SOFTWARE DESIGN LECTURE 7 GRASP Dave Clarke 1

TODAY S LECTURE We will discuss 7 of the GRASP design patterns cohesion and coupling were covered earlier. These provide principles for evaluating and improving designs. 2

Understanding responsibilities is key to object-oriented design. Martin Fowler 3

RESPONSIBILITY-DRIVEN DESIGN RDD is a metaphor for thinking about object-oriented design. Think of software objects similar to people with responsibilities who collaborate with other people to get work done. RDD leads to viewing an OO design as a community of collaborating responsible objects. 4

GRASP General Responsibility Assignment Software Patterns or Principles (GRASP) A learning aid for OO Design with responsibilities. A collection of patterns/principles for achieving good design patterns of assigning responsibility. One such collection others exist, e.g., SOLID 5

REALM OF GRASP GRASP patterns refer to software objects. Inspiration can of course be drawn from domain objects. 6

RESPONSIBILITIES AND METHODS Responsibilities are mostly to do with methods. Applying GRASP can be done when drawing interaction diagrams and while coding. 7

LIST OF GRASP PATTERNS Low coupling (already covered) High cohesion (already covered) Creator Information expert Controller Polymorphism Indirection Pure fabrication Protected variations 8

CREATOR 9

SCENARIO: POS APPLICATION time Sale 1 Contains 1..* Sales LineItem quantity * Described-by 1 Product Description description price itemid Who should be responsible for creating a SalesLineItem instance? 10

CREATING A SALELINEITEM :Register :Sale makelineitem(quantity) create(quantity) :SalesLineItem What about the ProductDescription? 11

DISCUSSION Basic idea is to find a creator that needs to be connected to the created object in any event. Also need initialisation data to be nearby sometimes requires that it is passed into client. e.g., ProductDescription needs to be passed in. 12

CREATOR (SUMMARY) Who creates? (Related to Factory design pattern.) Assign class B the responsibility to create an instance of class A if one of these is true: 1. B contains A 2. B aggregates A 4. B records A 5. B closely uses A 3. B has the initialising data for A 13

VARIATIONS Consider a LinkedList. A client creates the objects stored in the list, but the LinkedList object creates the links of the list. For complex construction, or when instance depends upon a specific external value, other options may need to be considered. Concrete Factory or AbstractFactory patterns. 14

INFORMATION EXPERT 15

INFORMATION EXPERT What is general principle for assigning responsibilities to objects? Consider that there may be 100s or 1000s of classes. To which ones do we assign particular functionality? Choose well and design will be easier to understand, maintain, extend, and reuse. 16

SOLUTION Assign responsibility to the information expert the class that has the information to fulfil the responsibility. Start by clearly stating the responsibility. Next, look to Design Model. If that is not helpful, look to Domain Model and attempt to use (or expand) its classes to inspire the create of design classes. 17

EXAMPLE: POS Who should be responsible for knowing the grand total of a sale? time Sale 1 Contains 1..* Sales LineItem quantity * Described-by 1 Product Description description price itemid 18

RESPONSIBILITIES Design Class Responsibility Sale knows sale total SaleLineItem knows line item subtotal ProductDescription knows product price 19

SOLUTION Sale time gettotal() gettotal getsubtotal lineitems[i] : getprice :Sale SaleLineItem :ProductDescription Sales LineItem quantity getsubtotal() Product Description description price itemid getprice() 20

DISCUSSION Notice that it required information spread across different classes of objects. Partial information experts collaborate in the task. Sometimes other considerations overrule. For instance, should a Sale object save itself in database? If so, then all database code would be scattered across codebase. 21

CONTROLLER 22

PROBLEM Which first object beyond the UI later receives and coordinates ( controls ) a system operation? Examples: End sale button, Spell check button. A Controller is the first object beyond the UI later that is responsible for receiving and handling a system operation message. 23

SOLUTION Assign the responsibility to a class representing one of the following choices: represents the overall system, a root object, a device that the software is running within, or a major subsystem. represents the use case scenario within which the system event occurs. use the same controller class for all system events in the same use case scenario. think in terms of sessions instances of conversations with an actor. Note that view, window, document classes are not on this list! 24

EXAMPLE System endsale() enteritem() makenewsale() makepayment() makereturnitem() enterreturnitem(). system operations discovered during behavioural analysis 25

ALLOCATION #1 System endsale() enteritem() makenewsale() makepayment() makereturnitem() enterreturnitem(). Register endsale() enteritem() makenewsale() makepayment() makereturnitem() enterreturnitem(). system operations discovered during behavioural analysis allocation of system operations during design, using one facade controller 26

ALLOCATION #2 System endsale() enteritem() makenewsale() makepayment() makereturnitem() enterreturnitem(). ProcessSale Handler endsale() enteritem() makenewsale() makepayment() allocation of system operations during design, using several use case controllers HandleReturns Handler makereturnitem() enterreturnitem(). 27

DISCUSSION This is simply a delegation pattern the UI should not contain application logic. Increases potential for reuse and pluggable interfaces. Creates opportunity to reason about state of use case, for example, to ensure that operations occur in a legal sequence. 28

PROBLEM Problem: Bloated Controllers a single controller that receives all system events, does too much of the work handling events, has too many attributes (duplicating information found elsewhere), etc. Remedies: Add more controllers. Design controller so that it primarily delegates the fulfilment of each system operation to other objects. 29

POLYMORPHISM 30

PROBLEM How to handle alternatives based on types? if-then-else means variation, but nonextensible when new variations arise How to create pluggable software components? view objects in client-server relationship: how can you replace one server component with another without affecting the client? 31

SOLUTION When related alternatives or behaviours vary by type, assign responsibility for the behaviour using polymorphic operations to types for which the behaviour varies. Do not test for the type of an object and use conditional logic to perform varying alternatives based on type. 32

EXAMPLE: SUPPORTING THIRD-PARTY TAX CALCULATORS? Different possible tax calculators. Each has a different interface. Each supports different communication protocols (TCP/IP socket, SOAP, Java RMI). What objects should be responsible for handling these varying external tax calculator interfaces? 33

POLYMORPHISM «interface» ITaxCalculatorAdapter gettaxes( Sale ) : List<TaxLineItems> TaxMasterAdapter ShonkyTaxFraudAdapter gettaxes( Sale ) : List<TaxLineItems> gettaxes( Sale ) : List<TaxLineItems> GoodAsGoldTaxProAdapter gettaxes( Sale ) : List<TaxLineItems> By Polymorphism, multiple tax calculator adapters have their own similar by varying behaviours for adapting to different external tax calculators. 34

REPRESENTING POLYMORPHISM IN UML 35

REPRESENTING POLYMORPHISM IN UML 36

DISCUSSION Interfaces or superclasses: Guideline: use interfaces when you want polymorphism without committing to a particular class hierarchy. Future-proofing: if variability at a particular point is very probably, then expend the effort to accommodate flexibility. Avoid adding flexibility just because it is possible. 37

PURE FABRICATION 38

PURE FABRICATION What objects should have the responsibility, when you do not want to violate High Cohesion and Low Coupling, or other goals, but solutions offered by Information Expert (for example) are not appropriate? 39

IN DETAIL Even though classes often correspond to real-world domain objects, there are often cases where assigning responsibility to domain layer software classes leads to problems in terms of poor cohesion or coupling, or low reuse potential. 40

SOLUTION Assign a highly cohesive set of responsibilities to a convenience class, not representing a problem domain concept. Fabrication made up. Pure keep it clean: high cohesion, low coupling. Pure fabrication English idiom that implies making something up. 41

EXAMPLE Need to save Sale instances in a relation database. Information Expert says assign functionality to Sale. Implications: Task needs large number of supporting database-oriented operations, none related to the concept of a Sale. Incohesion! Sale becomes coupled to data base interface, so coupling goes up. Saving objects in a database is a general task many classes will need it. 42

SOLUTION PersistentStorage insert( Object ) update( Object ) Understandable concept. Pure software concept. Not in domain model. Sale unaffected. Cohesive concept. Generic and reusable. 43

DISCUSSION Design of objects can be broadly divided into two categories: Representational decomposition. e.g., Sale Behavioural decomposition. e.g., TableOfContentsGenerator. Pure Fabrications are often the result of behavioural decomposition. Often highly cohesive with high reuse potential. Avoid overuse: functions and algorithms generally should not be represented by objects. 44

INDIRECTION 45

PROBLEM How to assign responsibility to avoid direct coupling between two (or more) things? How to decouple objects so that low coupling is supported and reuse potential remains higher? 46

SOLUTION Assign the responsibility to an intermediate object to mediate between other components or services so that they are not directly coupled. The intermediary creates indirection between the other components. 47

EXAMPLE s:sale :TaxMasterAdaptor t = gettotal taxes = gettaxes(s) xxx «actor» :TaxMasterSystem... The adaptor acts as a level of indirection to external systems. 48

PROTECTED VARIATIONS 49

PROBLEM How to design objects, subsystems, and systems so that the variations or instability in these elements does not have an undesirable impact on other elements? 50

SOLUTION Identify points of predicted variation or instability Assign responsibilities to create a stable interface around them. Interface in broadest sense not just Java interface. 51

EXAMPLE «interface» ITaxCalculatorAdapter gettaxes( Sale ) : List<TaxLineItems> TaxMasterAdapter ShonkyTaxFraudAdapter gettaxes( Sale ) : List<TaxLineItems> gettaxes( Sale ) : List<TaxLineItems> GoodAsGoldTaxProAdapter gettaxes( Sale ) : List<TaxLineItems> The ITaxCalculatorAdaptor interface (from Polymorphism) allows for future tax calculators that may not yet have been thought of. 52

OTHER APPROACHES TO PROTECTED VARIATIONS Core protected variation mechanisms: data encapsulation, interfaces, polymorphism, standards, virtual machines, operating systems. Service lookup: clients look up server with stable interface via technology such as Java JINI or UDDI for Web services. Law of Demeter: objects never talk to objects they are not directly connected to. 53

TODAY S LECTURE We covered 7 of the GRASP design patterns. 54