Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28,...
Transcript of Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28,...
![Page 1: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/1.jpg)
Common mistakes andBasic Design Principles
David Rabinowitz
![Page 2: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/2.jpg)
May 28, 2008 Object Oriented Design Course 2
Common Mistakes
Repeated often Don’t you make them! How to recognize the danger signals?
![Page 3: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/3.jpg)
May 28, 2008 Object Oriented Design Course 3
Danger Signals (1)public class Counter { public int howManyA(String s) { int conut = 0; for(int i = 0; i < s.length(); ++i) if(s.charAt(i) == 'a') ++count; return count; } }
Is this a class?
![Page 4: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/4.jpg)
May 28, 2008 Object Oriented Design Course 4
Danger Signals (2)
Class City extends Place { … }Class Jerusalem extends City implements Capital { … }
Class TelAviv extends City { … }
What is wrong here?
![Page 5: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/5.jpg)
May 28, 2008 Object Oriented Design Course 5
Danger Signals (3)
Class Person {String getName(); void setName(String name);
int getAge(); void setAge(int age);Car getCar(); void setCar(Car car);
}
What do we see ?
![Page 6: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/6.jpg)
May 28, 2008 Object Oriented Design Course 6
Basic Design Principles (abridged)
The Open Closed Principle The Dependency Inversion Principle The Interface Segregation Principle The Acyclic Dependencies Principle
![Page 7: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/7.jpg)
May 28, 2008 Object Oriented Design Course 7
The Open Closed Principle Software entities (classes, modules,
functions, etc.) should be open for extension, but closed for modification.
In the OO way:• A class should be open for extension, but
closed for modification. Existing code should not be changed –
new features can be added using inheritance or composition.
![Page 8: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/8.jpg)
May 28, 2008 Object Oriented Design Course 8
Exampleenum ShapeType {circle, square};
struct Shape { ShapeType _type;};struct Circle {ShapeType _type;double _radius;Point _center;
};
struct Square {ShapeType _type;double _side;Point _topLeft;
};void DrawSquare(struct Square*)
void DrawCircle(struct Circle*);
![Page 9: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/9.jpg)
May 28, 2008 Object Oriented Design Course 9
Example (cont.)void DrawAllShapes(struct Shape* list[], int n) {
int i;for (i=0; i<n; i++) {
struct Shape* s = list[i];switch (s->_type) {
case square:DrawSquare((struct Square*)s);break;case circle:DrawCircle((struct Circle*)s);break;
}}
}Where is the violation?
![Page 10: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/10.jpg)
May 28, 2008 Object Oriented Design Course 10
Correct Formclass Shape {public:
virtual void Draw() const = 0;};class Square : public Shape {public:
virtual void Draw() const;};class Circle : public Shape {public:
virtual void Draw() const;};
void DrawAllShapes(Set<Shape*>& list) {for (Iterator<Shape*>i(list); i; i++)
(*i)->Draw();
![Page 11: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/11.jpg)
May 28, 2008 Object Oriented Design Course 11
The Dependency Inversion Principle
A. High level modules should not depend upon low level modules. Both should depend upon abstractions.
B. Abstractions should not depend upon details. Details should depend upon abstractions.
![Page 12: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/12.jpg)
May 28, 2008 Object Oriented Design Course 12
Example
void Copy() {int c;while ((c = ReadKeyboard()) != EOF)WritePrinter(c);
}
Where is the violation?
![Page 13: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/13.jpg)
May 28, 2008 Object Oriented Design Course 13
Example (cont.) Now we have a second writing device – disk
enum OutputDevice {printer, disk};
void Copy(outputDevice dev) {int c;while ((c = ReadKeyboard()) != EOF)
if (dev == printer)WritePrinter(c);
elseWriteDisk(c);
}
![Page 14: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/14.jpg)
May 28, 2008 Object Oriented Design Course 14
Correct formclass Reader { public: virtual int Read() = 0;};class Writer { public: virtual void Write(char)=0;};void Copy(Reader& r, Writer& w) { int c; while((c=r.Read()) != EOF) w.Write(c);}
![Page 15: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/15.jpg)
May 28, 2008 Object Oriented Design Course 15
The Interface Segregation Principle
The dependency of one class to another one should depend on the smallest possible interface.
Avoid “fat” interfaces
![Page 16: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/16.jpg)
May 28, 2008 Object Oriented Design Course 16
The Interface Segregation PrincipleClientA
ClientB
ClientC
Service
clientAMethods()clientBMethods()clientCMethods()
<<Interface>>
![Page 17: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/17.jpg)
May 28, 2008 Object Oriented Design Course 17
The Interface Segregation Principle
ClientA
ClientB
ClientC
ClientAService
clientAMethods()
<<Interface>>
ClientBService
clientBMethods()
<<Interface>>
ClientCService
clientCMethods()
<<Interface>>
ServiceImpl
clientAMethods()clientBMethods()clientCMethods()
![Page 18: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/18.jpg)
May 28, 2008 Object Oriented Design Course 18
Example
class Door { public: virtual void Lock() = 0; virtual void Unlock() = 0; virtual bool IsDoorOpen() = 0;};
class Timer { public: void Regsiter(int timeout, TimerClient* client);};
class TimerClient { public: virtual void TimeOut() = 0;};
![Page 19: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/19.jpg)
May 28, 2008 Object Oriented Design Course 19
A Timed Door
![Page 20: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/20.jpg)
May 28, 2008 Object Oriented Design Course 20
Correct Form Two options:
Adapter Multiple Inheritance
![Page 21: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/21.jpg)
May 28, 2008 Object Oriented Design Course 21
The Acyclic Dependencies Principle
The dependency structure between packages must not contain cyclic dependencies.
![Page 22: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/22.jpg)
May 28, 2008 Object Oriented Design Course 22
Example
gui
comm
modem protocol
comm_error
process
file
![Page 23: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/23.jpg)
May 28, 2008 Object Oriented Design Course 23
Correct Formgui
comm
modem protocol
comm_error
process
file
![Page 24: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/24.jpg)
May 28, 2008 Object Oriented Design Course 24
Example 2
A
B
X
Y
A
B
X
YB I n t e r f a c ei m p l e m e n t s
![Page 25: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/25.jpg)
May 28, 2008 Object Oriented Design Course 25
The Law Of Demeter
Only talk to your immediate friends. In other words:
• You can play with yourself. (this.method())
• You can play with your own toys (but you can't take them apart). (field.method(), field.getX())
• You can play with toys that were given to you. (arg.method())
• And you can play with toys you've made yourself. (A a = new A(); a.method())
![Page 26: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/26.jpg)
May 28, 2008 Object Oriented Design Course 26
Example
![Page 27: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/27.jpg)
May 28, 2008 Object Oriented Design Course 27
How to correct
![Page 28: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/28.jpg)
May 28, 2008 Object Oriented Design Course 28
Example Code
![Page 29: Common mistakes and Basic Design Principles€¦ · The Acyclic Dependencies Principle. May 28, 2008 Object Oriented Design Course 7 The Open Closed Principle](https://reader033.fdocuments.us/reader033/viewer/2022060705/6070f00d644fae330900b4c7/html5/thumbnails/29.jpg)
May 28, 2008 Object Oriented Design Course 29
Resources
Our resources page http://www.objectmentor.com/
resources/articleIndex• Don’t be afraid from “old” articles