Principles of Object-Oriented Design

Slides:



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

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
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
Common mistakes Basic Design Principles David Rabinowitz.
Common mistakes Basic Design Principles Amit Shabtay.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object Oriented Programming Development
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
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.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
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,
Object Oriented Programming
1 Design by Principles (Robert Martin) Software Engineering.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
1 Good Object-Oriented Design Radu Marinescu Lecture 3 Principles of Object-Oriented Design Part II 19 th November 2002.
Lecture 12 Implementation Issues with Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCE 240 – Intro to Software Engineering Lecture 3.
NCCUCS Software Engineering 補充投影片 April 14, 2014.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
1 Advanced Object-oriented Design – Principles and Patterns OO Design Principles.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming Concepts
Software Design Principles
Inheritance and Polymorphism
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Behavioral Design Patterns
OOP What is problem? Solution? OOP
Copyright © by Curt Hill
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
TIM 58 Chapter 8: Class and Method Design
Lecture 23 Polymorphism Richard Gesick.
Object Oriented Practices
Software Design Principles
Objects First with Java
MSIS 670 Object-Oriented Software Engineering
lecture 08, OO Design Principle
Design Tips.
Software Design Lecture : 12.
Principles of Object-Oriented Design
Fundaments of Game Design
Principles of Object-Oriented Design Part II
Object Oriented Analysis and Design
Principles of High-Level Design
Chapter 8 - Design Strategies
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Presentation transcript:

Principles of Object-Oriented Design Lecture 3

Bibliography Lecture 3

Martin’s Signs of Rotting Design Rigidity code difficult to change (Continuity) management reluctance to change anything becomes policy Fragility code breaks in unexpected places (Protection) even small changes can cause cascading breaks Immobility code is so tangled that it's impossible to reuse anything lots of semantical (or even syntactical) duplication Composability Viscosity much easier to hack than to preserve original design “easy to do the wrong thing, but hard to do the right thing” (R.Martin) Lecture 3

Causes of Rotting Design Changing Requirements is inevitable both better designs and poor designs have to face the changes; good designs are stable Dependency Management the issue of coupling and cohesion It can be controlled! create dependency firewalls see DIP example All systems change during their life-cycles. This must be borne in mind when developing systems expected to last longer than the first version. I. Jacobson, OOSE, 1992 Lecture 3

Example of Rigidity and Immobility enum OutputDevice {printer, disk}; void Copy(OutputDevice dev){ int c; while((c = ReadKeyboard())!= EOF) if(dev == printer) WritePrinter(c); else WriteDisk(c); } Copy Read Keyboard Write Printer Write Disk void Copy(){ int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); } Lecture 3

Open-Closed Principle (OCP) Software entities should be open for extension, but closed for modification B. Meyer, 1988 / quoted by R. Martin, 1996 Be open for extension module's behavior can be extended Be closed for modification source code for the module must not be changes Modules should be written so they can be extended without requiring them to be modified Lecture 3

Open the door ... How to make the Car run efficiently with a TurboEngine? Only by changing the Car! ...in the given design Lecture 3

... But Keep It Closed! A class must not depend on a concrete class! It must depend on an abstract class ... ...using polymorphic dependencies (calls) Lecture 3

R.Martin, “The Open-Closed Principle,” 1996 Strategic Closure No significant program can be 100% closed R.Martin, “The Open-Closed Principle,” 1996 Closure not complete but strategic Use abstraction to gain explicit closure provide class methods which can be dynamically invoked to determine general policy decisions e.g. draw Squares before Circles design using abstract ancestor classes Use "Data-Driven" approach to achieve closure place volatile policy decisions in a separate location e.g. a file or a separate object minimizes future change locations Lecture 3

Make all object-data private OCP Heuristics Make all object-data private No Global Variables! Changes to public data are always at risk to “open” the module They may have a rippling effect requiring changes at many unexpected locations; Errors can be difficult to completely find and fix. Fixes may cause errors elsewhere. Non-private members are modifiable Case 1: "I swear it will not change" may change the status of the class Case 2: the Time class may result in inconsistent times Lecture 3

RTTI is Ugly and Dangerous! OCP Heuristics (2) RTTI is Ugly and Dangerous! RTTI is ugly and dangerous RTTI = Run-Time Type Information If a module tries to dynamically cast a base class pointer to several derived classes, any time you extend the inheritance hierarchy, you need to change the module recognize them by type switch or if-else-if structures Not all these situations violate OCP all the time when used only as a "filter" Lecture 3

Example of Rigidity and Immobility enum OutputDevice {printer, disk}; void Copy(OutputDevice dev){ int c; while((c = ReadKeyboard())!= EOF) if(dev == printer) WritePrinter(c); else WriteDisk(c); } Copy Read Keyboard Write Printer Write Disk void Copy(){ int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); } Lecture 3

Dependency Inversion Principle I. High-level modules should not depend on low-level modules. Both should depend on abstractions. II. Abstractions should not depend on details. Details should depend on abstractions R. Martin, 1996 A base class in an inheritance hierarchy should not know any of its subclasses Modules with detailed implementations are not depended upon, but depend themselves upon abstractions OCP states the goal; DIP states the mechanism; LSP is the insurance for DIP Lecture 3

Procedural vs. OO Architecture Object-Oriented Architecture Lecture 3

DIP Applied on Example Copy Reader Writer Disk Writer Keyboard Reader class Reader { public: virtual int read()=0; }; class Writer { virtual void write(int)=0; void Copy(Reader& r, Writer& w){ int c; while((c = r.read()) != EOF) w.write(c); } Copy Reader Writer Disk Writer Keyboard Reader Printer Writer Lecture 3

Interface (abstract class) Implementation (concrete class) DIP Related Heuristic Design to an interface, not an implementation! Use inheritance to avoid direct bindings to classes: Interface (abstract class) Client Implementation (concrete class) Lecture 3

Design to an Interface Abstract classes/interfaces: Exceptions tend to change less frequently abstractions are ‘hinge points’ where it is easier to extend/modify shouldn’t have to modify classes/interfaces that represent the abstraction (OCP) Exceptions Some classes are very unlikely to change; therefore little benefit to inserting abstraction layer Example: String class In cases like this can use concrete class directly as in Java or C++ Lecture 3

DIP Related Heuristic (2) Avoid Transitive Dependencies Avoid structures in which higher-level layers depend on lower-level abstractions: In example below, Policy layer is ultimately dependant on Utility layer. Policy Layer depends on Mechanism Layer depends on Utility Layer Lecture 3

Solution to Transitive Dependencies Use inheritance and abstract ancestor classes to effectively eliminate transitive dependencies: …also a matter of interface ownership Policy Layer Policy Service Interface depends on Mechanism Layer Mechan. Service Interface depends on Utility Layer Lecture 3

DIP - Related Heuristic When in doubt, add a level of indirection If you cannot find a satisfactory solution for the class you are designing, try delegating responsibility to one or more classes: Problem Holder Problem Solver Lecture 3

When in doubt ... It is generally easier to remove or by-pass existing levels of indirection than it is to add them later: Blue class’s indirect message calls to red class fail to meet some criteria (e.g. real-time constraints, etc.) X So, Blue class re-implements some or all of green class’s responsibilities for efficiency and calls red object directly Lecture 3