More Design Heuristics

Slides:



Advertisements
Similar presentations
Chapter 11 Component-Level Design
Advertisements

1 Object-Oriented Reengineering © R. Marinescu Lecture 5 Radu Marinescu Principles of Object-Oriented Design.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
Common mistakes Basic Design Principles David Rabinowitz.
Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.
Common mistakes Basic Design Principles Amit Shabtay.
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
CSc 335: Three More OO Design Principles
Design II Today: Design Principles…can we define them intro.12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Principles of Object-Oriented Design SEI, SJTU WEB APPS and SERVICES.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Principles iwongu at gmail dot com.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
1 Design by Principles (Robert Martin) Software Engineering.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
Principles of Object Oriented Design
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
Course information Old exam Resit Report Result and walkthrough
Copyright © Jim Fawcett Spring 2017
Object-Oriented Programming Concepts
Software Design Principles
Inheritance ITI1121 Nour El Kadri.
Chapter 5:Design Patterns
Copyright © by Curt Hill
Review: Two Programming Paradigms
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
EECE 310: Software Engineering
Object Oriented Practices
Component-Level Design
Software Design Principles
Object Oriented Practices
Questions First On class? On project? On material we’ve discussed?
An Introduction to Software Architecture
CS 350 – Software Design Principles and Strategies – Chapter 14
Principles of Object-Oriented Design
Principles of Object-Oriented Design
Informatics 122 Software Design II
Object Oriented Design & Analysis
Chapter 8 - Design Strategies
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Chapter 10 – Component-Level Design
Presentation transcript:

More Design Heuristics Which heuristic to use… Hard stop at 10:50 so we can play Design Jeopardy!!!! CSE 403, Spring 2008, Alverson

Let’s recap There is no “right answer” with design Applying effective heuristics can provide insights and lead to a good design Identify objects Form consistent abstractions Encapsulate implementation details Favor composition over inheritance Hide information Keep coupling loose and cohesion strong Identify areas likely to change Use design patterns Consider testability Other common principles Code Complete 1-9 We’ll explore 4 today CSE 403, Spring 2008, Alverson

1. Interface Segregation Principle The dependency of one class to another one should depend on the smallest possible interface Clients should not be forced to depend on methods that they do not use Example: Dogs jump but don’t sing CSE 403, Spring 2008, Alverson

ISP Example: Timed door class Door { public: virtual void Lock() = 0; virtual void Unlock() = 0; virtual bool IsDoorOpen() = 0; }; TimedDoor needs to sound an alarm when the door has been left open for too long. To do this, the TimedDoor object communicates with another object called a Timer. Refrigerators do this CSE 403, Spring 2008, Alverson

ISP Example: Timed door class Timer { public: void Register(int timeout, TimerClient* client); }; class TimerClient virtual void TimeOut() = 0; How should we connect the TimerClient to a new TimedDoor class so it can be notified on a timeout? time of timeout object to invoke TimeOut() on when timeout occurs TimeOut method CSE 403, Spring 2008, Alverson

InterfaceSegregationPrinciple Solution: yes or no? No, because now all Doors need to export a TimeOut call CSE 403, Spring 2008, Alverson

InterfaceSegregationPrinciple Solution: yes or no? No, because now all Doors need to export a TimeOut call No, as it’s polluting the Door interface by requiring all doors to have a TimeOut() method CSE 403, Spring 2008, Alverson

ISP Solution: yes or no? Use multiple inheritence CSE 403, Spring 2008, Alverson

ISP Solution: yes or no? Yes, separation through multiple inheritance Use multiple inheritence Yes, separation through multiple inheritance CSE 403, Spring 2008, Alverson

ISP solution: yes or no? When the Timer sends the TimeOut message to the TimedDoorAdapter, the TimedDoorAdapter delegates the message back to the TimedDoor. Composition May not have multiple inheritenc CSE 403, Spring 2008, Alverson

ISP solution: yes or no? Yes, separation through delegation When the Timer sends the TimeOut message to the DoorTimerAdapter, the DoorTimerAdapter delegates the message back to the TimedDoor. Composition May not have multiple inheritenc Yes, separation through delegation CSE 403, Spring 2008, Alverson

2. Dependency Inversion Principle High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. Example: Separation of policy and mechanism CSE 403, Spring 2008, Alverson

DependencyInversionPrinciple: Example Copy characters from a printer to a keyboard Copy Write keyboard Read printer CSE 403, Spring 2008, Alverson

DIP: yes or no? void copy() { int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); } CSE 403, Spring 2008, Alverson

DIP: yes or no? void copy() { int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); } Not really. Copy (high level) is quite dependent on the keyboard and printer (low level). It cannot be easily reused for another domain. CSE 403, Spring 2008, Alverson

DIP: yes or no? CSE 403, Spring 2008, Alverson

DIP: yes or no? Yes. Reader and Writer provide an abstraction through which the high and low modules interact. Copy is now more widely usable. CSE 403, Spring 2008, Alverson

3. Liskov Substitution Principle Subtypes must be substitutable for their base types Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Example: Anyone? Do we need a translator for this one? CSE 403, Spring 2008, Alverson

LSP: Example – Rectangle & Square class Rectangle { public: void SetWidth(double w) {itsWidth=w;} void SetHeight(double h) {itsHeight=w;} double GetHeight() const {return itsHeight;} double GetWidth() const {return itsWidth;} private: double itsWidth; double itsHeight; }; Reasonable to derive a square from a rectangle, right? CSE 403, Spring 2008, Alverson

LSP: any problems with this? class Square : public Rectangle { … }; void Square::SetWidth(double w) { Rectangle::SetWidth(w); Rectangle::SetHeight(w); } void Square::SetHeight(double h) Rectangle::SetHeight(h); Rectangle::SetWidth(h); Waste of space if a lot of squares. Use two vars (h, w) vs one. CSE 403, Spring 2008, Alverson

LSP: what about now? void morph(Rectangle& r) { r.SetWidth(32);//calls Rectangle::SetWidth } Could we call morph with a Square? What would happen? Liskov Substitution Principle: Subtypes must be substitutable for their base types CSE 403, Spring 2008, Alverson

LSP: but, but, but Couldn’t we fix this with virtual functions, requiring setWidth and setHeight to be implemented by the subclasses? void g(Rectangle& r) { r.SetWidth(5); r.SetHeight(4); assert(r.GetWidth() * r.GetHeight()) == 20); } Problem here as developer assumed true but not if sent a square. CSE 403, Spring 2008, Alverson

4. Open Closed Principle Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Reasonable? Why? CSE 403, Spring 2008, Alverson

OpenClosedPrinciple: yes or no? Resource allocator allocates different objects, based on their type. class ResourceAllocator { public: int Allocate(int resourceType) { int resourceId; switch (resourceType) { case TIME_SLOT: resourceId = FindFreeTimeslot(); MarkTimeslotBusy(resourceId); break; case SPACE_SLOT: resourceId = FindFreeSpaceSlot(); MarkSpaceslotBusy(resourceId); break; default: Trace(ERROR, "Attempted to allocate invalid resource\n"); break; } … No. If you had a new resource type, you’d need to modify the routine. (Modify vs extend) CSE 403, Spring 2008, Alverson

OpenClosedPrinciple: yes or no? class ResourcePool { public: virtual int FindFree() = 0; virtual int MarkBusy() = 0; virtual Free(int resourceId) = 0; }; class TimeslotPool : public ResourcePool{…}; class SpaceslotPool: public ResourcePool{…}; class ResourceAllocator { ResourcePool *rpool[MAX_RESOURCE_POOLS]; int Allocate(int resourceType) { int resourceId; resourceId= rpool[resourceType]->FindFree(); rpool[resourceType]->MarkBusy(resourceId)]; } Can extend without recompile up to the size of the array (unless ou realloc) CSE 403, Spring 2008, Alverson

The list goes on… We could spend a whole quarter talking about design Single responsibility principle (a class should have only 1 reason to change) No redundancy principle Make the common case fast and the uncommon case correct Fail early, gracefully, and transparently Follow good practices, read good code, read good books, get feedback on your designs, iterate, iterate, iterate! CSE 403, Spring 2008, Alverson

Today’s references http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign CSE 403, Spring 2008, Alverson

Design Jeopardy CSE 403, Spring 2008, Alverson

Ground rules Project teams act as teams (min 4 players) All try to answer First with answer up times out everyone All with correct answer get points Grand winner gets Ice Cream Bars Winning impaired teams get Popsicles CSE 403, Spring 2008, Alverson

Practice with Presenter Write your names in the Answer box Submit it Answer CSE 403, Spring 2008, Alverson

Design for 100 One 403 project is clearly implementing a FAÇADE Team: One 403 project is clearly implementing a FAÇADE Which one is it? Answer CSE 403, Spring 2008, Alverson

Design for 100 Team: Describe 2 distinct advantages of providing an iterator for your data structure? Answer Multiple people can be iterating over it Hides the implementation so you can change it CSE 403, Spring 2008, Alverson

Design for 200 A database is a reasonable example of what pattern? Team: A database is a reasonable example of what pattern? What characteristic of the pattern is not typical with the database? Answer Singleton Can be dynamically created, only if needed (vs db typically static) CSE 403, Spring 2008, Alverson

Design for 200 Team: The public switched telephone network is an example of what type of design pattern? Focus on the fact that there are many resources such as dial tone generators, ringing generators, and digit receivers that must be shared between all subscribers. Flyweight Immutable Answer CSE 403, Spring 2008, Alverson

Design for 200 Team: Answer (+ draw changes on code above) void process(String[] words) { // Loop through the array of Strings for(int i=0; i<words.length; i++) { String argument = ""; // Reverse the characters in each String for(int j=words[i].length(); j>0; j--) { argument += words[i].substring(j-1,j); } System.out.println(argument); } // Test for two particular Strings if(words.length == 2){ if(words[0].toLowerCase().equals("mighty") && words[1].toLowerCase().equals("mouse")) System.out.println("...here he comes to save the day."); } } 1. Why is the process() method not “cohesive”? 2. What changes would you make to make it so? Because its doing all sorts of processing To make cohesive, divide into multiple routines. Cohesion refers to how much (or how little) the internal parts of something are working on the same issue, and how well they relate to each other. It is the quality of single mindedness or singleness of purpose, and it makes entities (classes, methods) easier to name and understand. Answer (+ draw changes on code above)

Design for 200 Team: From a design perspective, would it be appropriate to implement a class House via inheritance from class Building in order to reuse the implementation of the many useful methods already defined in Building even if Building has other methods that have no meaning in the context of House? 1. What is your answer? 2. What pattern/principle/heuristic guided you? No Liskov substitution principle – you need to substitute child for parent and this isn’t possible Answer CSE 403, Spring 2008, Alverson

Applause, applause, applause – great game everyone! Design for 500!!!!!! Just kidding …. who’s our winner? Applause, applause, applause – great game everyone! CSE 403, Spring 2008, Alverson