Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Design Heuristics

Similar presentations


Presentation on theme: "More Design Heuristics"— Presentation transcript:

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

2 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

3 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

4 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

5 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

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

7 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

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

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

10 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

11 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

12 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

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

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

15 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

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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 Today’s references CSE 403, Spring 2008, Alverson

28 Design Jeopardy CSE 403, Spring 2008, Alverson

29 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

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

31 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

32 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

33 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

34 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

35 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)

36 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

37 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


Download ppt "More Design Heuristics"

Similar presentations


Ads by Google