Your SlideShare is downloading. ×
Stoop ed-some principles
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Stoop ed-some principles

659
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
659
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Some Principles Stéphane Ducasse --- 2005
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Open-Close • Software entities (classes, • modules, functions, etc.) should • be open for extension, but closed • for modification.
  • 4. S.Ducasse 4 The open-closed principle • 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.
  • 5. S.Ducasse 5 One kind of application enum 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*);
  • 6. S.Ducasse 6 Example (II) 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; } } } Adding a new shape requires adding new code to this method.
  • 7. S.Ducasse 7 Correct Form class 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();
  • 8. S.Ducasse 8 Some Principles • Dependency Inversion Principle • Interface Segregation Principle • The Acyclic Dependencies Principle
  • 9. S.Ducasse 9 Dependency Inversion Principle • 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.
  • 10. S.Ducasse 10 Example void Copy() { int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); }
  • 11. S.Ducasse 11 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); else WriteDisk(c); }
  • 12. S.Ducasse 12 Solution 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); }
  • 13. S.Ducasse 13 Some Principle
  • 14. S.Ducasse 14 Interface Segregation Principle • The dependency of one class to another one should depend on the smallest possible interface. • Avoid “fat” interfaces
  • 15. S.Ducasse 15 Examples
  • 16. S.Ducasse 16 Solutions • One class one responsibility • Composition?
  • 17. S.Ducasse 17 The Acyclic Dependency Principle • The dependency structure between packages must not contain cyclic dependencies.
  • 18. S.Ducasse 18 Example...Ez
  • 19. S.Ducasse 19 Solutions • Layering? • Separation of domain/applicatin/UI
  • 20. S.Ducasse 20 Packages, Modules and other • The Common Closure Principle • Classes within a released component should share common closure.That is, if one needs to be changed, they all are likely to need to be changed. • The Common Reuse Principle • The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.
  • 21. S.Ducasse 21 Summary Build your own taste Analyze what you write and how?