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

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

Assigning Responsibilities

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

ADVANCED SOFTWARE DESIGN LECTURE 7 GRASP

Information Expert (or Expert)

GRASP: Patterns for. chapter18

References: Applying UML and patterns Craig Larman

Assigning Responsibilities (Patterns of Responsibility Assignment Principles: GRASP)

COMP 6471 Software Design Methodologies

2 GRASP Patterns and basic OO Design. Roel Wuyts OASS

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

Assigning Responsibilities by Larman

ADVANCED SOFTWARE DESIGN LECTURE 4 GRASP. Dave Clarke

GRASP Design Patterns A.A. 2018/2019

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

Principles of Software Construction: Objects, Design, and Concurrency

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

On to Object-oriented Design

Software Engineering

Mapping Designs to Code

17. GRASP: Designing Objects with Responsibilities

Object-Oriented Design

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

Object-Oriented Design

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

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

Software Modeling & Analysis

OO Design2. Design Artifacts

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

Object-Oriented Design

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

An Introduction to Patterns

GRASP 2. CSC 440: Software Engineering Slide #1

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

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

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

ADVANCED SOFTWARE DESIGN LECTURE 4 SOFTWARE ARCHITECTURE

The Strategy Pattern Design Principle: Design Principle: Design Principle:

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

Modeling Dynamic Behavior

Design Patterns. Decorator. Oliver Haase

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

What is a Model? Copyright hebley & Associates

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

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

From designing to coding

Tecniche di Progettazione: Design Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns Design patterns advantages:

On to Object-oriented Design

Patterns and Testing

OODP OOAD Session 7a

Relationships amongst Objects

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

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Modeling Dynamic Behavior

COMP 6471 Software Design Methodologies

Domain Model and Domain Modeling

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

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

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

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

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

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Review Software Engineering October, 7, Adrian Iftene

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

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

Topics in Object-Oriented Design Patterns

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

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

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

A Reconnaissance on Design Patterns

Logical Architecture & Design Preliminaries

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Object-Oriented Design I - SOLID

Design Engineering. Overview

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

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

Principles of Object-Oriented Design

Software Design and Analysis CSCI 2040

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Design patterns. Jef De Smedt Beta VZW

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Design Patterns (Part 2) CSCE Lecture 18-10/25/2016 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

Using Design Patterns in Java Application Development

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Lecture 20: Design Patterns II

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

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

SOFTWARE PATTERNS. Joseph Bonello

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

CSE 70 Final Exam Fall 2009

CS342: Software Design. November 21, 2017

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

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

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

The Design Patterns Matrix From Analysis to Implementation

Requirements and Design Overview

Transcription:

Principles of Software Construction: Objects, Design, and Concurrency Assigning Responsibilities to Objects toad Fall 2014 Jonathan Aldrich Charlie Garrod School of Computer Science

Key concepts from Thursday toad 2

Requirements and Design Overview Requirements Engineering Requirements Elicitation (see 15-313) Functional Requirements (often as Use Cases) Quality Attributes (often as Quality Attribute Scenarios) (Object-Oriented) Requirements Analysis Domain Modeling System Specification System Sequence Diagrams Behavioral Contracts (Object-Oriented) Software Design Architectural Design (mostly in 15-313) Responsibility Assignment Object sequence diagrams Object model (class diagrams) GRASP heuristics for assigning responsibilities Method specifications / code contracts toad 3

Today s Lecture: Learning Goals Review high-level design goals Understand design principles such as coupling, cohesion, and correspondence, and how these support design goals Understand how to apply GRASP guidelines such as Creator, Expert, and Controller to promote these design principles Introduce the idea of design patterns by examining the Decorator pattern toad 4

Software Design Goals For any program specification, multiple programs fulfill it What are the differences between the programs? Which program should we choose? Of course, we usually synthesize a program, not choose it How can we design a program with the right properties? toad 5

Goals, Principles, Guidelines Design Goals Desired quality attributes of software Driven by cost/benefit economics Examples: Evolvability, separate development, reuse, performance, robustness, Design Principles Guidelines for designing software Heuristics Patterns Support one or more design goals Examples: Low coupling, high cohesion, high correspondence, Design Heuristics Rules of thumb for low-level design decisions Promote design principles, and ultimately design goals Example: Creator, Expert, Controller Design Patterns General solutions to recurring design problems Promote design goals, but may add complexity or involve tradeoffs Examples: Composite, Decorator, Strategy Goals, principles, heuristics, patterns may conflict Use high-level goals of project to resolve Goals Principles X toad 6

Principles for Assigning Responsibilities: Coupling Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 7

Design Principle: Coupling A module should depend on as few other modules as possible Enhances understandability evolvability Little context necessary to make changes Reduces the cost of change evolvability When a module interface changes, few modules are affected Enhances reuse Fewer dependencies, easier to adapt to a new context toad 8

Coupling Example Create a Payment and associate it with the Sale. Register Sale Payment toad 9

Coupling Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale toad 10

Coupling Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale makepayment() 1: makepayment() : Register :Sale 1.1. create() :Payment toad 11

Coupling Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale makepayment() 1: makepayment() : Register :Sale 1.1. create() Second solution has less coupling Register does not know about Payment class toad 12 :Payment

Common Forms of Coupling in OO Languages Type X has a field of type Y Method m in type X refers to type Y e.g. a method argument, return value, local variable, or static method call Type X is a direct or indirect subclass of Type Y Type Y is an interface, and Type X implements that interface toad 13

Coupling: Discussion Subclass/superclass coupling is particularly strong protected fields and methods are visible subclass is fragile to many superclass changes e.g. change in method signatures, added abstract methods Guideline: prefer composition to inheritance, to reduce coupling Not all coupling is equal Are you coupled to a stable interface? A stable interface is unlikely to change, and likely well-understood Therefore this coupling carries little evolveability cost Coupling is one principle among many Consider cohesion, correspondence, and other principles Extreme low coupling one class does everything poor cohesion (discussed later!) toad 14

Design Heuristics: Controller Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 15

Controller (GRASP heuristic 1) What first object receives and coordinates a system operation (event)? endsale( )??? enteritem( )??? 16 toad 16

Controller (GRASP heuristic 1) What first object receives and coordinates a system operation (event)? a user clicking on a button a network request arriving a database connection dropped endsale( )??? nextround( )??? toad 17

Controller (GRASP heuristic 1) Problem: What object receives and coordinates a system operation (event)? Solution: Assign the responsibility to an object representing the overall system, device, or subsystem (façade controller), or a use case scenario within which the system event occurs (use case controller) Controller is a GRASP heuristic General Responsibility Assignment Software Patterns pattern is a misnomer here they are more heuristics than patterns Craig Larman, Applying UML and Patterns, Prentice Hall, 2004 Chapter 16+17+22 introduce GRASP toad 18

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 operations in a use case scenario toad 19

Controller: Discussion A Controller is a coordinator does not do much work itself delegates to other objects Façade 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 (future lecture) toad 20

Controller: Discussion of Design Goals/Strategies Decrease coupling User interface and domain logic are decoupled from each other Understandability: can understand these in isolation, leading to: Evolvability: both the UI and domain logic are easier to change Both are coupled to the controller, which serves as a mediator This coupling is less harmful The controller is a smaller and more stable interface Changes to the domain logic affect the controller, not the UI The UI can be changed without knowing the domain logic design Support reuse Controller serves as an interface to the domain logic Smaller, explicit interfaces support evolvability But, bloated controllers increase coupling and decrease cohesion; split if applicable toad 21

Principles for Assigning Responsibilities: Cohesion Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 22

Design Principle: Cohesion A module should have a small set of related responsibilities Enhances understandability evolvability A small set of responsibilities is easier to understand Enhances reuse A cohesive set of responsibilities is more likely to recur in another application toad 23

Cohesion Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale Register responsibilities Accept makepayment event from UI Coordinate payment among domain objects toad 24

Cohesion Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale Register responsibilities, generalized Accept all events from UI Coordinate all interactions among domain objects toad 25

Cohesion Example makepayment() : Register 1: create() p : Payment 2: addpayment(p) :Sale makepayment() 1: makepayment() : Register :Sale 1.1. create() Register responsibilities, generalized Accept all events from UI :Payment toad 26

Cohesion in Graph Implementations class Graph { Node[] nodes; boolean[] isvisited; } class Algorithm { } Graph is tasked with not just data, but also algorithmic responsibilities int shortestpath(graph g, Node n, Node m) { for (int i; ) if (!g.isvisited[i]) { g.isvisited[i] = true; } } return v; } toad 27

Monopoly Example class Player { Board board; /* in code somewhere */ getsquare(n); Square getsquare(string name) { for (Square s: board.getsquares()) if (s.getname().equals(name)) return s; return null; }} class Player { Board board; /* in code somewhere */ board.getsquare(n); } class Board{ List<Square> squares; Square getsquare(string name) { for (Square s: squares) if (s.getname().equals(name)) return s; return null; }} toad 28 Which design has higher cohesion?

Principles for Assigning Responsibilities: Correspondence Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 29

Design Principle: Correspondence Correspondence: align object model with domain model Class names, attributes, associations Also called low representational gap Enhances evolvability Small changes in domain model yield small changes in code The best we could hope to do! Code is more understandable: knowledge of domain carries over Enhances separate development Experts in different domain concepts can work on different code toad 30

Correspondence/Low Representational Gap toad 31

Design Heuristics: Expert Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 32

Information Expert (GRASP heuristic 2) Who should be responsible for knowing the grand total of a sale? gettotal( )??? toad 33

Information Expert (GRASP heuristic 2) Who should be responsible for knowing the grand total of a sale? gettotal( )??? Register Sale LineItem Product Decr. toad 34

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

Information Expert Heuristic: 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 Software classes instead of Domain Model classes If software classes do not yet exist, look in Domain Model for fitting abstractions (-> correspondence) toad 36

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 37

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 38

Information Expert Sale t = gettotal : Sale 1 *: st = getsubtotal lineitems[ i ] : SalesLineItem time... gettotal() 1.1: p := getprice() SalesLineItem quantity :Product Description getsubtotal() Product Description description price itemid New method getprice() toad 39

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 40

Information Expert: Discussion of Design Goals/Strategies Cohesion understandability evolvability Code lives with the data it manipulates Low coupling evolvability Client does not need to know about LineItem and ProductDescription they can change independently Client does not know how total is computed that can change Correspondence evolvability Total is associated with a sale in the real world coordinated changes to the sale and total computation are easily managed 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 toad 41

Design Heuristics: Creator Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 42

Creator (GRASP heuristic 3) 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 43 1

Creator Pattern (GRASP heuristic 3) Problem: Assigning responsibilities for creating objects Who creates Nodes in a Graph? Who creates instances of SalesItem? Who creates Children in a simulation? Who creates Tiles in a Monopoly game? AI? Player? Main class? Board? Meeple (Dog)? toad 44

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 45

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

Creator: Discussion of Design Goals/Strategy Promotes low coupling, high cohesion class responsible for creating objects it needs to reference creating the objects themselves avoids depending on another class to create the object Promotes evolvability Object creation is hidden, can be replaced locally Contra: sometimes objects must be created in special ways complex initialization instantiate different classes in different circumstances then cohesion suggests putting creation in a different object see design patterns such as builder, factory method toad 47

Design Heuristics: Creator Design Principles: guidelines for software design Coupling (low) Cohesion (high) Correspondence (high) Design Heuristics: rules of thumb Controller Expert Creator Design Patterns: solutions to recurring problems Decorator toad 48

Problem: Scrollable Windows Imagine you are building a GUI application Various windows show different views Some views are too big to fit on the screen we need scrolling Design considerations Cohesion: put scrolling functionality in its own class Coupling: don t treat scrolling windows specially Clients of Window generally shouldn t know which windows scroll Reuse: reuse scrolling across multiple windows «interface» Window Preliminary design: draw() How do we add scrolling? Similar: window chrome AppWindow1 draw() AppWindow2 draw() toad 49

The ScrollableWindow Decorator ScrollableWindow class A separate class Avoids making Window incohesive Implements Window Clients are not coupled to scrolling Wraps/decorates another window Any window can be (re)used Implements draw() to add a scrollbar, then asks the wrapped window to draw itself «interface» Window draw() AppWindow{1,2} draw() ScrollableWindow draw() 1 scrolledwin void draw() { draw scrollbar move viewport scrolledwin.draw(); } 1 toad 50

Decorator is a Design Pattern A general design problem Need to add new functionality Need to add it dynamically to different instances of an abstraction Want to treat enhanced object just like the original object We can generalize the solution as well Create a new class that implements the abstraction s interface Have the new class refer to a wrapped instance of the abstraction Implement new behavior, requesting behavior from the wrapped object where appropriate toad 51

The Decorator Pattern Component + operation() component 1 ConcreteComponent + operation() Decorator + operation() component.operation() newbeforebehavior() super.operation() newafterbehavior() ConcreteDecoratorA addedstate + operation() ConcreteDecoratorB # newafterbehavior() # newbeforebehavior() + operation() toad 52

Structural: Decorator Applicability To add responsibilities to individual objects dynamically and transparently For responsibilities that can be withdrawn When extension by subclassing is impractical Consequences More flexible than static inheritance Avoids monolithic classes Breaks object identity Lots of little objects toad 53

Toad s Take-Home Messages Design is driven by quality attributes Evolvability, separate development, reuse, performance, Design principles provide guidance on achieving qualities Low coupling, high cohesion, high correspondence, GRASP design heuristics promote these principles Creator, Expert, Controller, Applying principles to common problems yields design patterns Decorator, toad 54