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

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

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

Assigning Responsibilities

ADVANCED SOFTWARE DESIGN LECTURE 7 GRASP

Information Expert (or Expert)

GRASP Design Patterns A.A. 2018/2019

17. GRASP: Designing Objects with Responsibilities

Software Engineering

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

Assigning Responsibilities by Larman

COMP 6471 Software Design Methodologies

GRASP: Patterns for. chapter18

References: Applying UML and patterns Craig Larman

ADVANCED SOFTWARE DESIGN LECTURE 4 GRASP. Dave Clarke

Principles of Software Construction: Objects, Design, and Concurrency

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

On to Object-oriented Design

2 GRASP Patterns and basic OO Design. Roel Wuyts OASS

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

Assigning Responsibilities (Patterns of Responsibility Assignment Principles: GRASP)

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

Object-Oriented Design

Object-Oriented Design II - GRASP

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

Object-Oriented Design

Principles of Software Construction: Objects, Design and Concurrency. Just enough UML. toad

Tecniche di Progettazione: Design Patterns

GRASP 2. CSC 440: Software Engineering Slide #1

Patterns and Testing

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

Principles of Software Construction: Objects, Design and Concurrency. Introduction to Design. toad

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

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

COMP 6471 Software Design Methodologies

Object-Oriented Design I

Mapping Designs to Code

Design patterns. Jef De Smedt Beta VZW

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

OO Design2. Design Artifacts

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

Domain Model and Domain Modeling

OODP OOAD Session 7a

Review Software Engineering October, 7, Adrian Iftene

Object-Oriented Design I - SOLID

Software Modeling & Analysis

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

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

Principles of Software Construction: Objects, Design, and Concurrency

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

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

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

Software Design and Analysis CSCI 2040

Logical Architecture & Design Preliminaries

Software Design and Analysis CSCI 2040

What is a Model? Copyright hebley & Associates

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

Object- Oriented Design with UML and Java Part I: Fundamentals

Software Design and Analysis CSCI 2040

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

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

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

OBJECT ORIENTED ANALYSIS AND DESIGN SYLLABUS

Object Oriented. Analysis and Design

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

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Object-Oriented Design

Topics in Object-Oriented Design Patterns

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

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

Object-Oriented Design

Chapter 5 Object-Oriented Programming

Introduction to Software Engineering: Object Design I Reuse & Patterns

Design Engineering. Overview

Chapter 8: Class and Method Design

Web Application Design. Husni Husni.trunojoyo.ac.id

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

The Design Patterns Matrix From Analysis to Implementation

Recap: CRC Cards Process

Principles of Software Construction: Objects, Design, and Concurrency

CS6502- OBJECT ORIENTED ANALYSIS AND DESIGN UNIT I

Chapter 1: Principles of Programming and Software Engineering

Operations Contracts and Preliminaries on Design

A formal design process, part 2

From designing to coding

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS2353-OBJECT ORIENTED ANALYSIS AND DESIGN. Unit-I. Introduction to OOAD

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

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

Chapter 1: Programming Principles

Object-Oriented Systems Analysis and Design Using UML

Object Oriented Software Development CIS Today: Object Oriented Analysis

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Object-Oriented Design and Modeling Using the UML

Final Exam CISC 475/675 Fall 2004

Study About the Relationship Between the Model-View-Controller Pattern and Usability

Object Oriented Paradigm

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

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

CS6502-OBJECT ORIENTED ANALYSIS AND DESIGN Two Marks Question with Answers Unit-I Introduction to OOAD

1. Write two major differences between Object-oriented programming and procedural programming?

Transcription:

Principles of Software Construction: Objects, Design and Concurrency 15-214 toad Object-Oriented Design: Assigning Responsibilities Christian Kästner Charlie Garrod School of Computer Science With slides from Klaus Ostermann 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis

Object-Oriented Design After identifying your requirements and creating a domain model, then add methods to the software classes, and define the messaging between the objects to fulfill the requirements. But how? How should concepts be implemented by classes? What method belongs where? How should the objects interact? This is a critical, important, and non-trivial task toad 3

Responsibilities Responsibilities are related to the obligations of an object in terms of its behavior. Two types of responsibilities: knowing doing Doing responsibilities of an object include: doing something itself, such as creating an object or doing a calculation initiating action in other objects controlling and coordinating activities in other objects Knowing responsibilities of an object include: knowing about private encapsulated data knowing about related objects knowing about things it can derive or calculate toad 4

Design Goals - Summary 5 design goals Design for division of labor Design for understandability and maintenance Design for change Design for reuse Design for robustness 5 design strategies (for now) Explicit interfaces (clear boundaries) Information hiding(hide likely changes) Low coupling (reduce dependencies) High cohesion (one purpose per class) Low repr. gap (align requirements and impl.) toad 5

GRASP Patterns GRASP = General Responsibility Assignment Software Patterns (introduced by Craig Larman) Patterns of assigning responsibilities reason about design trade-offs when assigning methods and fields to classes The GRASP patterns are a learning aid to help one understand essential object design apply design reasoning in a methodical, rational, explainable way lower level and more local reasoning than most design patterns toad 6

Patterns/Principles aid Communication Fred: "Where do you think we should place the responsibility for creating a SalesLineltem? I think a Factory." Wilma: "By Creator, I think Sale will be suitable." Fred: "Oh, right - I agree." toad 7

Nine GRASP Pattern: Creator Information Expert Low Coupling High Cohesion Controller Polymorphism Indirection Pure Fabrication Protected Variations toad 8

Information Expert (GRASP Pattern 1) Who should be responsible for knowing the grand total of a sale? gettotal( )??? toad 9

Information Expert (GRASP Pattern 1) Who should be responsible for knowing the grand total of a sale? time Sale 1 Sales LineItem quantity Contains 1..* * Captured-on Paid by id Described-by Register Customer name Product Description description price itemid toad 10 1

Information Expert Problem: What is a general principle of assigning responsibilities to objects? Solution: Assign a responsibility to the class that has the information necessary to fulfill the responsibility Start assigning responsibilities by clearly stating responsibilities! Typically follows common intuition Design Classes (Software Classes) instead of Conceptual Classes If Design Classes do not yet exist, look in Domain Model for fitting abstractions (-> low representational gap) toad 11

Information Expert What information is needed to determine the grand total? Line items and the sum of their subtotals Sale is the information expert for this responsibility. gettotal() toad 12

Information Expert To fulfill the responsibility of knowing and answering the sale's total, three responsibilities were assigned to three design classes of objects toad 13

Information Expert S a le t im e... t = g e tt o ta l 1 * : s t = g e ts u b to ta l lin e Ite m s [ i ] : : S a le S a le s L in e Ite m g e t T o t a l() 1.1 : p := g e tp r ic e () S a le s L in e I t e m q u a n t it y : P r o d u c t D e s c r ip tio n g e t S u b t o t a l () P r o d u c t D e s c r ip t io n d e s c r ip t io n p r ic e it e m I D N e w m e th o d g e t P r ic e () toad 14

Information Expert -> "Do It Myself Strategy" Expert usually leads to designs where a software object does those operations that are normally done to the inanimate real-world thing it represents a sale does not tell you its total; it is an inanimate thing In OO design, all software objects are "alive" or "animated," and they can take on responsibilities and do things. They do things related to the information they know. toad 15

Information Expert: Discussion of Design Goals/Strategies Explicit and small interfaces, information hiding Does not expose how sale is computed Low coupling Client does not need to know about LineItem and ProductDescription Low representational gap Assign responsibilities similar to responsibilities of realworld abstractions May conflict with cohesion Example: Who is responsible for saving a sale in the database? Adding this responsibility to Sale would distribute database logic over many classes low cohesion -> Design for Reuse, Understanding, Change, toad 16

Creator (GRASP Pattern 2) Who is responsible for creating SalesLineItem objects? Sale Captured-on Register time 1 Sales LineItem quantity Contains 1..* * Paid by id Described-by Customer name Product Description description price itemid toad 17 1

Creator Pattern (GRASP Pattern 2) Problem: Assigning responsibilities for creating objects Who creates Nodes in a Graph? Who creates instances of SalesItem? Who creates Rabbit-Actors in a Game? Who creates children if rabbits breed? Who creates Tiles in a Monopoly game? AI? Player? Main class? Board? Meeple (Dog)? toad 18

Creator Pattern Problem: Who creates an A? Solution: Assign class responsibility of creating instance of class A to B if B aggregates A objects B contains A objects B records instances of A objects B closely uses A objects B has the initializing data for creating A objects the more the better; where there is a choice, prefer B aggregates or contains A objects Key idea: Creator needs to keep reference anyway and will frequently use the created object toad 19

Creator : Example Who is responsible for creating SalesLineItem objects? Creator pattern suggests Sale. Interaction diagram: toad 20

Creator: Discussion of Design Goals/Strategy Promotes low coupling, design for reuse class responsible for creating objects it needs to reference creating the objects themselves avoids depending on another class to create the object Information hiding, design for change Object creation is hidden, can be replaced locally But creation may require significant complexity using recycled instances for performance reasons conditionally creating an instance from one of a family of similar classes based upon some external property value Sometimes desired to outsource object wiring ( dependency injection ) -> Several more complex creation strategies; many design patterns toad 21

Controller (GRASP Pattern 3) What first object receives and coordinates a system operation (events)? endsale( )??? enteritem( )??? 22 toad 22

Controller (GRASP Pattern 3) What first object receives and coordinates a system operation (events)? a user clicking on a button a network request arriving a database connection dropped endsale( )??? nextround( )??? 23 toad 23

Controller (GRASP Pattern 3) Problem: What first object receives and coordinates a system operation (events)? Solution: Assign the responsibility to an object representing the overall system, device, or subsystem (facade controller) or a use case scenario within which the system event occurs (use case controller) 24 toad 24

Controller: Example By the Controller pattern, here are some choices: Register, POSSystem: represents the overall "system," device, or subsystem ProcessSaleSession, ProcessSaleHandler: represents a receiver or handler of all system events of a use case scenario toad 25

Controller: Discussion Controller delegates to other objects; coordinates or controls the activity; does not much work itself Facade controllers suitable when not "too many" system events -> one overall controller for the system Use case controller suitable when façade controller "bloated" with excessive responsibilities (low cohesion, high coupling) -> several smaller controllers for specific tasks Closely related to Façade design pattern toad 26

Controller: Discussion of Design Goals/Strategies Design for Reuse Reuse entire subsystem through small explicit interface Information hiding for entire subsystem, e.g. hide that operations must be performed in specific sequence Separation of application logic from GUI Design for Change Application logic changeable without changing GUI GUI changeable without changing application logic Design for Understandability Dedicated place to understand interaction with environment events But, bloated controllers increase coupling and decrease cohesion; split if applicable toad 27

Other GRASP Patterns Low Coupling Decide between two designs for the one with lower coupling High Cohesion Decide between two designs for the one with higher cohesion Polymorphism Support alternatives with dynamic dispatch (multiple implementations of an interface) instead of case analysis See also Strategy Design Pattern Pure Fabrication If domain model provides no reasonable concept to assign responsibility without violating cohesion/coupling -> create a new abstraction (e.g., PersistantStorage) toad 28

Summary Assigning Responsibilities to Classes GRASP Patterns for first design considerations Information Expert Creator Controller Reason with Design Goals Patterns facilitate communication toad 29

Literature Craig Larman, Applying UML and Patterns, Prentice Hall, 2004 Chapter 16+17+22 introduce GRASP toad 30