Good-Enough Design. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/17/2009

Similar documents
Good-Enough Design & Version Control. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/16/2010

Originality is Overrated: OO Design Principles

Originality is Overrated: OO Design Principles & Iterating And Testing

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 8 09/17/2009

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 8 09/18/2007

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 03. REQUIREMENTS CHANGE. I Love You, You re Perfect... Now Change!

Software Development

08. DESIGN PRINCIPLES. Originality is Overrated PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

COURSE 2 DESIGN PATTERNS

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

The first program: Little Crab

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

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

CS112 Lecture: Defining Instantiable Classes

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Making a PowerPoint Accessible

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

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

6.001 Notes: Section 6.1

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 11/29/11. University of Colorado, 2011

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

CS 1302 Chapter 9 (Review) Object & Classes

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

THE ULTIMATE SEO MIGRATION GUIDE

Basic Reliable Transport Protocols

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

An Overview of Visual Basic.NET: A History and a Demonstration

What s new in SketchUp Pro?

S T R U C T U R A L M O D E L I N G ( M O D E L I N G A S Y S T E M ' S L O G I C A L S T R U C T U R E U S I N G C L A S S E S A N D C L A S S D I A

Introduction to Object-Oriented Programming

CSE 70 Final Exam Fall 2009

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

Programming 2. Object Oriented Programming. Daniel POP

FACADE & ADAPTER CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 7 09/13/2011

Math Dr. Miller - Constructing in Sketchpad (tm) - Due via by Friday, Mar. 18, 2016

Computer Science for Engineers

Credit is where Credit is Due. Lecture 28: OO Design Heuristics (Part 2) This Lecture. Last Lecture: OO Heuristics. Rules of Thumb

Object-Oriented Software Engineering Practical Software Development using UML and Java

Understading Refactorings

A - 1. CS 494 Object-Oriented Analysis & Design. UML Class Models. Overview. Class Model Perspectives (cont d) Developing Class Models

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Programmazione. Prof. Marco Bertini

COURSE 11 DESIGN PATTERNS

How to set up SQL Source Control The short guide for evaluators

6.001 Notes: Section 15.1

XP: Backup Your Important Files for Safety

COSC 3351 Software Design. An Introduction to UML (I)

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

02291: System Integration

Refactoring, Part 2. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 12/01/09. University of Colorado, 2009

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

SECURITY AUTOMATION BEST PRACTICES. A Guide on Making Your Security Team Successful with Automation SECURITY AUTOMATION BEST PRACTICES - 1

MITOCW watch?v=flgjisf3l78

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

Introducing the UML Eng. Mohammed T. Abo Alroos

Object-Oriented and Classical Software Engineering

Introduction to UML What is UML? Motivations for UML Types of UML diagrams UML syntax Descriptions of the various diagram types Rational Rose (IBM.. M

Recap : UML artefacts. Black Box Requirements. Functional Specification. System. System. Test. Design. System. System. Development.

CS61C : Machine Structures

The State Design Pattern

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Security Automation Best Practices

16 Multiple Inheritance and Extending ADTs

Object Relationships

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Inheritance (Outsource: )

SECURITY AUTOMATION BEST PRACTICES. A Guide to Making Your Security Team Successful with Automation

Logistics. Final Exam on Friday at 3pm in CHEM 102

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

OO System Models Static Views

Let me begin by introducing myself. I have been a Progress Application Partner since 1986 and for many years I was the architect and chief developer

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 5: Modelling with Classes

CS112 Lecture: Working with Numbers

CSE 403: Software Engineering, Spring courses.cs.washington.edu/courses/cse403/15sp/ UML Class Diagrams. Emina Torlak

COMP19612 exam performance feedback 2014

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet?

THE OBJECT-ORIENTED DESIGN PROCESS AND DESIGN AXIOMS (CH -9)

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

Example: Fibonacci Numbers

Construction: High quality code for programming in the large

9. MATHEMATICIANS ARE FOND OF COLLECTIONS

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

5. Application Layer. Introduction

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 37 Resolution Rules

CSC 408F/CSC2105F Lecture Notes

And Even More and More C++ Fundamentals of Computer Science

Transcription:

Good-Enough Design Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/17/2009 University of Colorado, 2009 1

Goals 2 Review material from Chapter 5 of Pilone & Miles Software Design Need for Good OO A&D principles SRP: Single Responsibility Principle DRY: Don t Repeat Yourself Principle

iswoon in Trouble 3 The previous chapter presents a design for associating dates and events that was causing problems Date objects maintain a list of its planned events An Event object is a dumb data holder storing only a name It has no logic of its own Date objects provide methods that internally add events to a planned date; The Date object contains information about what events are allowed on a particular date The UML diagram is shown on the next slide

UML Diagram Date + seemovie() : void + gotorestaurant() : void + orderflowers() : void + goondate() : boolean - validateevent(event: Event) : boolean 4 FirstDate - validateevent(event: Event) : boolean SecondDate - validateevent(event: Event) : boolean ThirdDate - validateevent(event: Event) : boolean events * Event + getname(): String SeeMovieEvent - name : String = "SeeMovie" + getname() : String OrderFlowersEvent - name : String = "OrderFlowers" + getname() : String GoToRestaurantEvent - name : String = "GoToRestaurant" + getname() : String UML Primer: Each rectangle represents a class that can have attributes and methods. A + symbols refers to public visibility; - indicates private visibility. The * means zero or more. The large triangle indicates inheritance. The arrow head indicates one way navigation ; in the diagram above Dates know about Events while Events are blissfully unaware of Dates

:System «create» :FirstDate UML Sequence Diagram gotorestaurant(...) «create» :GoToRestaurantEvent 5 validateevent(...) return True getname() seemovie(...) «create» :SeeMovieEvent validateevent(...) goondate() return True getname()

Bad Design (I) 6 This design has a lot of problems The Event class is completely useless Why not have Date store an array of strings? Date s API is pretty bad Event creation methods are specified for all possible events; that means that some dates have event creation methods for events that are not valid for them! The Date class has a list of allowable events but doesn t show it on the diagram (or it doesn t show the list of planned events; either way it has two lists but only shows one)

Bad Design (II) 7 But those are relatively minor issues The main reason why this design is bad is that its inflexible with respect to the types of changes that occur regularly for this application domain It can t easily handle the addition of a new type of Event It can t easily handle changing the name of an existing Event It can t easily handle the changing of what events are valid for what dates

Good Design 8 A primary goal in OO A&D is producing a design that makes likely changes, straightforward typically by adding a new subclass of an existing class or by adding an object that implements a known interface no need to recompile the system or even it bring it down You can t anticipate arbitrary changes and there is no reason to invest time/$$ into planning for unlikely changes So use good OO A&D principles to handle likely changes

Single Responsibility Principle (SRP) (I) 9 The Date class has multiple responsibilities tracking the events planned for a date tracking the events allowed for a date It has multiple reasons to change The single responsibility principle says Every object in your system should have a single responsibility and all the object s services should be focused on carrying out that single responsibility This is also known as having high cohesion

SRP (II) 10 Granularity? When we say responsibility we are not talking about low level concerns, such as insert element e into array a at position i but design level concerns, such as classify documents by keyword store client details manage itinerary of Jack and Jill s second date

SRP (III) 11 The existing iswoon design is bad because each time we add a new event We need to add a new Event subclass Add a new method to Date Update each of Date s subclasses (cringe!) We need to migrate to a design, in which the addition of a new type of event results in the addition of a new Event subclass and nothing more

Textual Analysis (I) 12 One way of identifying high cohesion in a system is to do the following For each class C For each method M Write The C Ms itself Examples The Automobile drives itself The Automobile washes itself The Automobile starts itself

Textual Analysis (II) 13 Sometimes you need to include parameters in the sentence The CarWash washes the Automobile itself If any of these sentences doesn t make sense then investigate further You may have discovered a service that belongs to a different responsibility of the system and should be moved to a different class This may require first creating a new class before performing the move

Textual Analysis (III) 14 Textual analysis is a good heuristic While its useful for spot checking a design, its not perfect But the underlying principle is sound Each class in your design should pull its weight have a single responsibility with a nice balance of both data AND behavior for handling that responsiblity

Other Problems 15 The iswoon design also has problems with duplication of information (indeed duplication can often lead to classes with low cohesion that violate SRP The duplication in iswoon is related to Event Types The names of event types appear in Event subclass names The name attribute inside of each event subclass The method names in Date In addition, duplication occurs with validateevent() in each of the Date subclasses

Don t Repeat Yourself (I) 16 The DRY principle Avoid duplicate code by abstracting out things that are common and placing those things in a single location Basic Idea Duplication is Bad! At all levels of software engineering: Analysis, Design, Code, and Test

DRY (II) 17 We want to avoid duplication in our requirements, use cases, feature lists, etc. We want to avoid duplication of responsibilities in our code We want to avoid duplication of test coverage in our tests Why? Incremental errors can creep into a system when one copy is changed but the others are not Isolation of Change Requests: We want to go to ONE place when responding to a change request

DRY (II) 17 We want to avoid duplication in our requirements, use cases, feature lists, etc. We want to avoid duplication of responsibilities in our code We want to avoid duplication of test coverage in our tests Why? Incremental errors can creep into a system when one copy is changed but the others are not Isolation of Change Requests: We want to go to ONE place when responding to a change request

DRY (II) 17 We want to avoid duplication in our requirements, use cases, feature lists, etc. We want to avoid duplication of responsibilities in our code We want to avoid duplication of test coverage in our tests Why? Incremental errors can creep into a system when one copy is changed but the others are not Isolation of Change Requests: We want to go to ONE place when responding to a change request

Example (I) 18 Duplication of Responsibility BarkRecognizer recognize(bark: Bark) door DogDoor open: boolean open() close() isopen(): boolean getallowedbark(): Bark setallowedbark(bark: Bark) The dog door should automatically close 30 seconds after it has opened Where should this responsibility live? door It would be easy to put this responsibility in the clients But it really should live in DogDoor (which method?) Remote pressbutton()

Example (II) 19 DRY is really about ONE requirement in ONE place We want each responsibility of the system to live in a single, sensible place This applies at all levels of the project, including requirements Imagine a set of requirements for the dog door

Example (III) 20 The dog door should alert the owner when something inside the house gets too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system to make sure it doesn t activate when the dog door is open The dog door should make a noise if the door cannot open because of a blockage outside The dog door will track how many times the dog uses the door When the door closes, the house alarm will re-arm if it was active before the door opened Beware of Duplicates!!!

Example (IV) 21 The dog door should alert the owner when something inside the house gets too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system to make sure it doesn t activate when the dog door is open The dog door should make a noise if the door cannot open because of a blockage outside The dog door will track how many times the dog uses the door When the door closes, the house alarm will re-arm if it was active before the door opened

Example (V) 22 The dog door should alert the owner when something is too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system The dog door will track how many times the dog uses the door Duplicates removed!

Example (VI) 23 Ruby on Rails makes use of DRY as a core part of its design focused configuration files; no duplication of information for each request, often single controller, single model update, single view But prior to Ruby on Rails 1.2 there was duplication hiding in the URLs used by Rails applications POST /people/create # create a new person GET /people/show/1 # show person with id 1 POST /people/update/1 # edit person with id 1 POST /people/destroy/1 # delete person with id 1

Example (VII) 24 The duplication exists between the HTTP method name and the operation name in the URL POST /people/create Recently, there has been a movement to make use of the four major verbs of HTTP PUT/POST == create information (create) GET == retrieve information (read) POST == update information (update) DELETE == destroy information (destroy) These verbs mirror the CRUD operations found in databases Thus, saying create in the URL above is a duplication

Example (VIII) 25 In version 1.2, Rails eliminates this duplication for something called resources Now URLs look like this: POST /people GET /people/1 PUT /people/1 DELETE /people/1 And the duplication is logically eliminated Disclaimer: but not actually eliminated Web servers do not universally support PUT and DELETE out of the box. As a result, Rails uses POST POST /people/1 ; Post-Semantics: DELETE

Other OO Principles 26 Classes are about behavior Emphasize the behavior of classes over the data Encapsulate what varies Use classes to achieve information hiding in a design One reason to change Promotes high cohesion in a design Code to an Interface Promotes flexible AND extensible code Open-Closed Principle Take CSCI 5448 next Fall for more details! Classes should be open for extension and closed for modification

New iswoon Design 27 Date - datenumber: int + addevent(event e): boolean + goondate(): boolean events * Event - alloweddates : int[] - description : String Event(allowedDates : int[], description : String + datesupported(dateno : int) : boolean Subclasses eliminated; Events now keep track of what Dates they are allowed on; When you add an event to a Date, Date calls Event.dateSupported() to validate it You can easily add a new type of Event; just create a new instance of Event with a different description; nothing else changes! To add a new date, just increase the number

Impact on Tasks 28 With the right design, multiple tasks estimated to take days may take only one (or less than one) Task: Create Send Flowers Event Estimate: 2 days Task: Create a Book Restaurant Event Estimate: 3 days Task: Add Order Cab Event Estimate: 2 days A great design helps you be more productive!

Discussion 29 The underlying message of Chapter 5 is that everyone on your team needs to understand good OO A&D principles On a daily basis, you look for ways in which the design can be improved Small changes can occur via refactoring Large changes need to become tasks and tracked like all others You welcome such changes since they ll make life easier and more productive down the line

Review 30 The remainder of the lecture will be devoted to reviewing Question 3.2 Answers to Review 2 Answers to Homework 1

Question 3.2 31 3.2. ELEMENT = (up -> down -> ELEMENT) accepts an "up" action and then a "down" action. Using parallel composition and the ELEMENT process describe a model that can accept up to four "up" actions before a "down" action. Draw a structure diagram for your solution. Looks like a simple problem. Indeed it is simple if you DON T use parallel composition (LTS on next slide) ELEMENT = ELEMENT[0], ELEMENT[i:0..4] = (when (i < 4) up -> ELEMENT[i+1] when (i > 0) down -> ELEMENT[i-1]).

Simple Goal 32

Question 3.2 (II) 33 But, this question asks that we use multiple instances of this process ELEMENT = (up -> down -> ELEMENT). to create a parallel composition that can do the same thing that the FSP on the previous page can do. In order to do this, we ll need at least four instances of ELEMENT. Lets start with: ELEMENT = (up -> down -> ELEMENT). FOURUP = (a: ELEMENT b: ELEMENT c: ELEMENT d: ELEMENT).

Here we can see that four processes with two actions each (with no shared actions) produce an LTS with 16 states (2 * 2 * 2 * 2) and on the surface very complex behavior. 34

35 But the complexity is only skin deep. In actuality, each process can only go up followed by a down. The complexity comes from allowing these simple behaviors to arbitrarily interleave. What we need to do is relabel actions such that sharing occurs between the processes, reducing complexity, and moving us towards the behavior we want to see: 4 ups before a down.

Structure Diagram? 36 FOURUP c.up c.down a.up up up down up d.up A B C D a.down down up down down d.down b.up b.down No surprises here. Four instances of ELEMENT, no shared actions, all actions become part of FOURUP s alphabet

Approach 37 The approach we are going to take to solve this problem is to tie the up and down actions of the various ELEMENT instances such that one process cannot go up unless another process goes down To do this, lets start by tying a s down action with b s up action. ELEMENT = (up -> down -> ELEMENT). FOURUP = (a: ELEMENT b: ELEMENT c: ELEMENT d: ELEMENT) /{a.down/b.up} The results?

38 A slightly less complicated LTS since a.down and b.up still have to occur at the same time. Lets do the same thing with {b, c} and {c, d}. That is have b s down be shared with c s up, etc.

Sharing more actions 39 New FSP ELEMENT = (up -> down -> ELEMENT). FOURUP = (a: ELEMENT b: ELEMENT c: ELEMENT d: ELEMENT) /{a.down/b.up, b.down/c.up, c.down/d.up} Leads to a much simpler set of behaviors (next slide)

40 Now we are seeing a major reduction in complexity. Because b cannot go up until a comes down, c cannot go up until b comes down, etc. To see, load the spec and run the animation. Recall that underneath this composition, we still have four simple processes.

Structure Diagram? 41 FOURUP b.down c.down a.up up up up up A B C D down down down down d.down a.down Now we can see why the complexity has gone down. The alphabet of FOURUP has been reduced from 8 actions to 5

Now the magic happens 42 Run the animation very carefully and note that a can perform its up operation four times before d is forced to run its down operation But that s exactly the behavior we are looking for! Four up actions before a down action occurs So...

Up and Down 43 We are almost finished; our second to last step is to rename a.up to up and rename d.down to down ELEMENT = (up -> down -> ELEMENT). FOURUP = (a: ELEMENT b: ELEMENT c: ELEMENT d: ELEMENT) /{up/a.up, a.down/b.up, b.down/c.up, c.down/d.up, down/d.down}. We get the same LTS as the last time but now in the animation we see the actions up and down. But, we have all these other actions getting in the way. What to do?

Time to Hide 44 The last thing we need to do is hide all of the actions except up and down ; Our composed process will simply have an alphabet of size 2 ELEMENT = (up -> down -> ELEMENT). FOURUP = (a: ELEMENT b: ELEMENT c: ELEMENT d: ELEMENT) /{up/a.up, a.down/b.up, b.down/c.up, c.down/d.up, down/d.down} @ {up,down}. This produces the same LTS with a lot of tau actions So, minimize that graph and you get

45 The exact same LTS that we achieved without using parallel composition! Problem solved.

Final Structure Diagram 46 FOURUP up up up up up A B C D down down down down down The final process just has up and down as its alphabet; we depend on the fact that a.up can run four times before d.down has to be executed to achieve the desired behavior.

Coming Up 47 Lecture 12: Monitors and Condition Synchronization Chapter 5 of Magee and Kramer Lecture 13: Version Control Chapter 6 of Pilone & Miles Lecture 14 will be a review for the Midterm Chapters 1-6 of Pilone & Miles Chapters 1-5 of Magee and Kramer