Design Principles iwongu at gmail dot com.

Slides:



Advertisements
Similar presentations
1 Object-Oriented Reengineering © R. Marinescu Lecture 5 Radu Marinescu Principles of Object-Oriented Design.
Advertisements

Since 1995, we’ve been building innovative custom solutions specifically designed to meet the unique needs of America’s most recognized companies. If you.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
17.10 Java Beans Java beans are a framework for creating components in Java. AWT and Swing packages are built within this framework Made to fit in with.
Common mistakes Basic Design Principles David Rabinowitz.
Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.
Common mistakes Basic Design Principles Amit Shabtay.
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 1 פרק 3 OCP.
Design II Today: Design Principles…can we define them intro.12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
Advanced Principles I Principles of Object-Oriented Class Design Copyright  by Object Mentor, Inc All Rights Reserved Portions of this material.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Principles of Object-Oriented Design SEI, SJTU WEB APPS and SERVICES.
OODP Prudent OO Design. OODP-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
Software Design Principles
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
Subtype Polymorphism, Subtyping vs
1 Design by Principles (Robert Martin) Software Engineering.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
1 OO Class Design PrinciplesStefan Kluth 3OO Class Design Principles 3.1Dependency Management 3.2The Copy Program 3.3Class Design Principles.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
1 Advanced Object-oriented Design – Principles and Patterns OO Design Principles.
What is Agile Design? SRP OCP LSP DIP ISP
More Design Heuristics
Software Design Principles
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Copyright © by Curt Hill
Software Design Principles
Subtype Polymorphism, Subtyping vs
Principles of Object-Oriented Design
The SOLID Principles.
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Some principles for object oriented design
Presentation transcript:

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); }

Second Version Oh, no! Nobody said the requirements might change! bool gTapeReader = false; // remember to reset this flag void copy() { int ch; while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF) WritePrinter(ch); }

Third Version How unexpected! Requirements changed again! bool gTapeReader = false; bool gTapePunch = false; // remember to reset these flags void copy() { int ch; while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF) gTapePunch ? WritePunch(ch) : WritePrinter(ch); }

Wonderful Use Change Rot Smell Redesign

Design Smells The odors of rotting software It’s rigid. It’s fragile. It’s not reusable.

Rigidity Rigidity 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.

Fragility Software 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.

Immobility It'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 design First and only version! void copy(FILE* in, FILE* out) { int ch; 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 OO First 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;

CHANGE

“CHANGE” The one constant in software development

“Belady and Lehman’s Laws” Software will continually change “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 Unified Modeling Language

Class

Association

Generalization

Dependency

UML Class Diagram shows the relationships of Classes. Dependency Association Aggregation Composition Generalization Realization

Design Principles

Design Principles SRP Single Responsibility Principle OCP Open Closed Principle LSP Liskov Substitution Principle DIP Dependency Inversion Principle ISP Interface Segregation Principle

SRP Single Responsibility 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

Each responsibility is an axis of change.

Orthogonal class Y class X No need to change

Non-Orthogonal class Y class X Need to change

SRP Violation

SRP

But, it’s not easy to see SRP.

But, it’s not easy to see SRP.

Question?

OCP Open-Closed Principle

Software entities should be open for extension but closed for modification.

But how?

Abstraction is the key.

enum ShapeType { circle, square }; struct Shape { ShapeType itsType; }; struct Circle { ShapeType itsType; }; struct Square { ShapeType itsType; }; void DrawAllShapes(Shape* list[], int n) { for (int i = 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 (int i = 0; i < n; ++i) { Shape* s = list[i]; s->Draw(); } }

OCP Violation If new shapes are needed,

OCP Violation

OCP If new shapes are needed,

OCP

But, OCP is not just inheritance.

OCP is the root motivation behind many of the heuristics and conventions. For example,

Make All Member Variables Private.

No Global Variables – Ever.

RTTI is Dangerous.

Question?

LSP Liskov Substitution Principle

Subtypes should be substitutable for their base types.

Rectangle

Square IS-A rectangle.

Square

void Square::set_width(double w) {. Rectangle::set_width(w); void Square::set_width(double w) { Rectangle::set_width(w); Rectangle::set_height(w); } void Square::set_height(double h) { Rectangle::set_width(h); Rectangle::set_height(h); }

double g(Rectangle& r) { r.set_width(5); r.set_height(4); assert(r.area() == 20); }

Square IS-A rectangle. But, Square is NOT substitutable for rectangle.

Violating the LSP often results in the use of Run-Time Type Information (RTTI).

double g(Rectangle& r) {. r. set_width(5);. r. set_height(4); double g(Rectangle& r) { r.set_width(5); r.set_height(4); if (dynamic_cast<Square*>(&r) != 0) { assert(r.area() == 16); } else { assert(r.area() == 20); } }

It’s also a violation of OCP.

Using DBC, LSP means,

DBC? Design By Contract. Precondition Postcondition Invariant

A routine redeclaration may only replace the original precondition by one equal or weaker, and the original postcondition by one equal or stronger.

Base Derived

The postcondition of Square::set_width() is weaker than the postcondition of Rectangle::set_width().

Rectangle Square

Question?

DIP Dependency Inversion Principle

a. High-level modules should not depend on low-level modules a. High-level modules should not depend on low-level modules. Both should depend on abstractios.

b. Abstractions should not depend on details b. Abstractions should not depend on details. Details should depend on abstractions.

Structured Design

Dependency Inversion

Dependency Inversion

Depend on abstractions.

No variable should hold a pointer or reference to a concrete class No variable should hold a pointer or reference to a concrete class. No class should derive from a concrete class. No method should override an implemented method of any of its base classes.

But, it’s impossible. For example, someone has to create the instances of the concrete class, and whatever module does that will depend on them.

And, it might not be a problem to depend on concrete but non-volitile classes.

DIP Violation

DIP

Question?

ISP Interface Segregation Principle

Door And, we need a timed door.

Timer

The first solution What are problems?

Not all varieties of Door need timing. Violation of LSP.

The interface of Door has been polluted with a method that it does not require. Fat Interface.

ISP Interface Segregation Principle Clients should not be forced to depend on methods that they do not use.

Multiple inheritance

Delegation

Question?

References

http://objectmentor. com/ http://objectmentor http://objectmentor.com/ http://objectmentor.com/resources/omi_reports_index.html http://objectmentor.com/resources/publishedArticles.html → Robert C. Martin