970 likes | 1.12k Views
Design Principles. iwongu at gmail dot com. What is Object-Oriented design?. Dependency Management. First Version All designs start well. void copy() { int ch ; while ( ( ch = ReadKeyboard ()) != EOF) WritePrinter ( ch ); }.
E N D
Design Principles iwongu at gmail dot com
First VersionAll designs start well void copy() { intch; while ( (ch = ReadKeyboard()) != EOF) WritePrinter(ch); }
Second VersionOh, no! Nobody said the requirements might change! boolgTapeReader = false; // remember to reset this flag void copy() { intch; while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF) WritePrinter(ch); }
Third VersionHow unexpected! Requirements changed again! boolgTapeReader = false; boolgTapePunch = false; // remember to reset these flags void copy() { intch; while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF) gTapePunch ? WritePunch(ch) : WritePrinter(ch); }
Design SmellsThe odors of rotting software • It’s rigid. • It’s fragile. • It’s not reusable.
RigidityRigidity is the inability to be changed • The impact of a change cannot be predicted. • If not predicted, it can not be estimated. • Time and cost can not be qualified. • Managers become reluctant to authorize change.
FragilitySoftware changes seem to exhibit non-local effects. • A single change requires a cascade of subsequent changes. • New errors appear in areas that seem unconnected to the changed areas • Quality is unpredictable. • The development team loses credibility.
ImmobilityIt's not reusable. • Desirable parts of the design are dependent on undesirable parts. • The work and risk of extracting the desirable part may exceed the cost of redeveloping from scratch.
Example of a good designFirst and only version! void copy(FILE* in, FILE* out) { intch; while ( (ch = fgetc(in)) != EOF) fputc (ch, out); } But, wait! Aren't we supposed to be learning OO design? This isn't OO, is it?
… Is it?It's a small program based on abstraction! • FILE is an abstraction. • It represented some kind of byte stream. • It has many variations. • It has methods. • The methods are dynamically bound. FILE is a class, just implemented differently.
Rephrased in OOFirst and only version! interface Reader { char read(); } interface Writer { void write(char c); } public class Copy { Copy(Reader r, Writer w) { itsReader = r; itsWriter = w; } public void copy() { int c; while ( (c = itsReader.read()) != EOF ) itsWriter.write(c); } Reader itsReader; Writer itsWriter; }
“Belady and Lehman’s Laws”Software will continually change. Software will become increasingly unstructured as it is changed.
Ities of Software Quality • Reliability • Efficiency • Readability • Understandability • Modifiability, Maintainability • Testability • Portability
UML Class Diagram shows the relationships of Classes. Dependency Association Aggregation Composition Generalization Realization
Design Principles • SRP Single Responsibility Principle • OCP Open Closed Principle • LSP Liskov Substitution Principle • DIP Dependency Inversion Principle • ISP Interface Segregation Principle
There should never be more than one reason for a class to change.
In the context of the SRP,a responsibility means "a reason for change."
Orthogonal class Y class X No need to change
Non-Orthogonal class Y class X Need to change
Software entities should be open for extension but closed for modification.
enumShapeType { circle, square }; struct Shape { ShapeTypeitsType; }; struct Circle { ShapeTypeitsType; }; struct Square { ShapeTypeitsType; }; void DrawAllShapes(Shape* list[], int n) { for (inti = 0; i < n; ++i) { Shape* s = list[i]; switch (s->itsType) { case square: DrawSquare((Square*)s); break; case circle: DrawCircle((Circle*)s); break; } } }
struct Shape { virtual void Draw() const = 0;};struct Square : Shape { virtual void Draw() const;};struct Circle : Shape { virtual void Draw() const;};void DrawAllShapes(Shape* list[], int n) { for (inti = 0; i < n; ++i) { Shape* s = list[i]; s->Draw(); }}
OCP is the root motivationbehind many of the heuristics and conventions.For example,