Common mistakes Basic Design Principles Amit Shabtay March
Common mistakes Basic Design Principles Amit Shabtay March 3 rd, 2004 Object Oriented Design Course
Tirgul Summery l l l l Basic design principles Advanced design principles (LSP, …) Intro to eclipse, unit testing, JUnit Generic programming (STL, Java generics) Aspect. Werkz- AOP framework ODBC, JDBC Exercise previews and reviews March 3 rd, 2004 Object Oriented Design Course 2
Course Requirement l l l Basic understanding of OOD Basic knowledge of C++, Java 3 programming exercises 2 theoretical exercises Exam March 3 rd, 2004 Object Oriented Design Course 3
Basic Design Principles March 3 rd, 2004 Object Oriented Design Course
Common Mistakes l l l Repeated often • Especially with the inexperienced Don’t you make them! How to recognize the danger signals? March 3 rd, 2004 Object Oriented Design Course 5
Danger Signals (1) public class Counter { public int how. Many. A(String s) { int conut = 0; for(int i = 0; i < s. length(); ++i) if(s. char. At(i) == 'a') ++count; return count; } } Is this a class? March 3 rd, 2004 Object Oriented Design Course 6
Danger Signals (2) Class City extends Place { … } Class Jerusalem extends City implements Capital { … } Class Tel. Aviv extends City { … } l What is wrong here? March 3 rd, 2004 Object Oriented Design Course 7
Danger Signals (3) Class Person { String get. Name(); void set. Name(String name); int get. Age(); void set. Age(int age); Car get. Car(); void set. Car(Car car); } l What do we see ? March 3 rd, 2004 Object Oriented Design Course 8
Basic Design Principles l l l The Open Closed Principle The Dependency Inversion Principle The Interface Segregation Principle The Acyclic Dependencies Principle These principles and more: http: //www. codeguru. com/forum/show post. php? p=1092794&postcount=1 March 3 rd, 2004 Object Oriented Design Course 9
The Open Closed Principle l l l Software entities (classes, modules, functions, etc. ) should be open for extension, but closed for modification. Existing code should not be changed – new features can be added using inheritance or composition. Which is preferred? March 3 rd, 2004 Object Oriented Design Course 10
Example enum Shape. Type {circle, square}; struct Shape { Shape. Type _type; }; struct Circle { Shape. Type _type; double _radius; Point _center; }; March 3 rd, 2004 struct Square { Shape. Type _type; double _side; Point _top. Left; }; void Draw. Square(struct Square*) void Draw. Circle(struct Circle*); Object Oriented Design Course 11
Example (cont. ) void Draw. All. Shapes(struct Shape* list[], int n) { int i; for (i=0; i<n; i++) { struct Shape* s = list[i]; switch (s->_type) { case square: Draw. Square((struct Square*)s); break; case circle: Draw. Circle((struct Circle*)s); break; } } Where is the violation? } March 3 rd, 2004 Object Oriented Design Course 12
Correct Form class Shape { public: virtual void Draw() const = 0; }; class Square : public Shape { public: virtual void Draw() const; }; class Circle : public Shape { public: void Draw. All. Shapes(Set<Shape*>& list) { virtualfor void Draw() const; (Iterator<Shape*>i(list); i; i++) }; (*i)->Draw(); March 3 rd, 2004 Object Oriented Design Course 13
The Dependency Inversion Principle A. B. High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. March 3 rd, 2004 Object Oriented Design Course 14
Example Where is the violation? void Copy() { int c; while ((c = Read. Keyboard()) != EOF) Write. Printer(c); } March 3 rd, 2004 Object Oriented Design Course 15
Example (cont. ) l Now we have a second writing device – disk enum Output. Device {printer, disk}; void Copy(output. Device dev) { int c; while ((c = Read. Keyboard()) != EOF) if (dev == printer) Write. Printer(c); else Write. Disk(c); } March 3 rd, 2004 Object Oriented Design Course 16
Correct form class 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); } March 3 rd, 2004 Object Oriented Design Course 17
The Interface Segregation Principle l The dependency of one class to another one should depend on the smallest possible interface. Avoid “fat” interfaces l Example: Word toolbars l March 3 rd, 2004 Object Oriented Design Course 18
The Interface Segregation Principle March 3 rd, 2004 Object Oriented Design Course 19
The Interface Segregation Principle March 3 rd, 2004 Object Oriented Design Course 20
Example class Timer { public: void Regsiter(int timeout, Timer. Client* client); }; class Timer. Client { public: virtual void Time. Out() = 0; }; class Door { public: virtual void Lock() = 0; virtual void Unlock() = 0; virtual bool Is. Door. Open() = 0; }; March 3 rd, 2004 Object Oriented Design Course 21
A Timed Door A violation? March 3 rd, 2004 Object Oriented Design Course 22
Correct Form l Two options: ? Adapter March 3 rd, 2004 In which language? Multiple Inheiritence Object Oriented Design Course 23
The Acyclic Dependencies Principle l The dependency structure between packages must not contain cyclic dependencies. March 3 rd, 2004 Object Oriented Design Course 24
Example March 3 rd, 2004 Object Oriented Design Course 25
Correct Form March 3 rd, 2004 Object Oriented Design Course 26
The Law Of Demeter l l 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. get. X()) • 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()) March 3 rd, 2004 Object Oriented Design Course 27
Example March 3 rd, 2004 Object Oriented Design Course 28
How to correct March 3 rd, 2004 Object Oriented Design Course 29
Example Code March 3 rd, 2004 Object Oriented Design Course 30
Resources l l A nice resources page for OOD: http: //www. objectmentor. com About the principles (same site): http: //www. objectmentor. com/mentorin g/OOPrinciples l March 3 rd, 2004 Object Oriented Design Course 31
- Slides: 31