Object Oriented Design Principles Arnon Rotem-Gal-Oz Product Line Architect.

Slides:



Advertisements
Similar presentations
Design Principles & Patterns
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
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.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
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”.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
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.
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.
CSSE 374: More GRASP’ing for Object Responsibilities
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
Design Principles iwongu at gmail dot com.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Module 3. Smells Between Classes Course: Refactoring.
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.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
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.
SOLID PHP & Code Smell Wrap-Up
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
Principles and examples
Mantas Radzevičius ifm-2/2
Course information Old exam Resit Report Result and walkthrough
CompSci 280 S Introduction to Software Development
CHAPTER 5 GENERAL OOP CONCEPTS.
Software Architecture & Difference from Design
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Chapter 11 Object-Oriented Design
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.
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
Principles of Package Architecture
Code Smells 1.
Object-Oriented Design
Design Tips.
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Object-Oriented PHP (1)
European conference.
Object Oriented Design & Analysis
Dependency Inversion principle
Refactoring.
Liskov Substitution Principle (LSP)
Chapter 10 – Component-Level Design
Presentation transcript:

Object Oriented Design Principles Arnon Rotem-Gal-Oz Product Line Architect

Preface Nothing really new here Just a summary of other peoples work: D.L. Parnas M. Fowler R.C. Martin, B. Meyer, B. Liskov W.J. Brown, R.C. Malveau, H.W. McCormick, T.J. Mowbray GoF N. Malik A. Holub And probably others I forgot

Agenda 7 Deadly Sins of Design The Rules Basic Stuff Evil Stuff More Stuff

7 Deadly Sins of Design Rigidity – make it hard to change Fragility – make it easy to break Immobility – make it hard to reuse Viscosity – make it hard to do the right thing Needless Complexity – over design Needless Repetition – error prone Not doing any

The Rules Apply Common Sense Dont get too dogmatic / religious Every decision is a tradeoff All other principles are just that Guidelines best practices Consider carefully if you should violate them - but, know you can.

Basic Stuff OCP open-closed principle SRP single responsibility principle ISP interface segregation principle LSP Liskov substitution principle DIP dependency inversion principle Dependency Injection

Open Closed Principle Software entities ( Classes, Modules, Methods, etc. ) should be open for extension, but closed for modification. Also Known As Protected Variation What Parnas meant when he coined Information hiding

Why OCP If not followed a single change to a program results in a cascade of changes to dependent modules. The program becomes fragile, rigid, unpredictable and un-reusable.

OCP Example TemplateMethod

OCP implications (examples) Use private modifier for class members Refrain from adding Getters automatically Use abstractions Extend by inheritance, delegation Inversion of Control (IoC)

Single Responsibility Principle A Class should have one reason to change A Responsibility is a reasons to change Can be tricky to get granularity right

Why SRP Single Responsibility = increased cohesion Not following results in needless dependencies More reasons to change. Rigidity, Immobility

SRP Example (1/2) The Rectangle has 2 responsibilities Algorithm Client Application Rectangle Draw() Area() DirectX Suddenly the Algorithm needs DirectX Dependency

SRP Example (2/2) Rectangle Area() Client Application Rectangle Renderer Draw() DirectX Algorithm Dependency

Interface Segregation Principle Many client specific interfaces are better than one general purpose interface Create an interface per client type not per client Avoid needless coupling to clients

Why ISP Otherwise – increased coupling between different clients Basically a variation on SRP

ISP example Client Application Rectangle Impl. DirectX Algorithm Dependency IRectangle Area() IRectangle Renderer Draw() Realization

Liskov Substitution Principle What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T. (Barbara Liskov, 1988)

Or in English Any subclass should always be usable instead of its parent class. Corollary - All derived classes must honour the contracts of their base classes IS A = same public behavior Pre-conditions can only get weaker Post-conditions can only get stronger

Why LSP Failing to follow LSP result in a mess OCP violations (if/then to identify types) Superclass unit test will fail Strange behavior

Trivial Example Why is that an LSP violation?

Real-Life Example.NET creates a transparent proxy and intercepts all calls to ContextBoundObjects Implementing IContextObjectSink On a Implementing IContextObjectSink On a class derived from ContrextAttribute lets you add a sink in the chain

Real-Life Example ServicedComponents violates LSP: violates LSP: You cannot add your own sinks (it only uses its own)

Dependency Inversion Principle Higher level modules should not depend on lower level modules Both should depend on abstractions Interfaces or Abstract classes Abstractions should not depend on details (Not to be confused with Dependency Injection and Inversion of Control) (Not to be confused with Dependency Injection and Inversion of Control)

Why DIP Increase loose coupling Abstract interfaces don't change Concrete classes implement interfaces Concrete classes easy to throw away and replace Increase mobility Increase isolation decrease rigidity decrease rigidity Increase testability Increase maintainablity Closely related to LSP

Reminder - Procedural Design

DIP

DIP implications Layers Interface based programming Separated Interface put interface in separate package than implementation put interface in separate package than implementation Dependency Injection

Old Way public class MyApp { public MyApp() { authenticator = new Authenticator(); database = new Database(); logger = new Logger(); errorHandler = new ErrorHandler(); } // More code here... }

Dependency Injection DIP says we should depend on an interface how do we get the concrete instance

New Way? public class MyApp { public MyApp() { authenticator = new IAuthenticator(); database = new IDatabase(); logger = new ILogger(); errorHandler = new IErrorHandler(); } // More code here... } OOPS

Dependency Injection Option 1 – Factory User depends on factory Factory depends on destination Option 2 – Locator/Registry/Directory The component still controls the wiring Instantiation Sequence Dependency on the Locator Option 3 – Dependency Injection An assembler controls the wiring

DI Options Setter Injection The component is passive Someone injects the dependency dependency

DI Options Setter Injection Constructor Injection Always initialized Better dependency visibility

Other DI Options Interface Injection Variation on setter injection Getter Injection Needs AOP (not clean)

Evil Stuff Switch statements If (type()) Singletons / Global variables Getters Helper Classes

More Stuff Package principles Not the core of this presentation Smells Anti-Patterns

Package Principles Reuse-Release Equivalency Principle Common Closure Principle Common Reuse Principle Acyclic Dependencies Principle Stable Dependencies Principle Stable Abstractions Principle

CodeSmells Something that's quick to spot Indication for a possible problem Not always the problem it self May not be a problem at all CodeSmell example – Long Method

DesignSmell (1) Many CodeSmells can also apply to design Long Parameter List Large Class (Swiss Army knife / Blob) Type Embedded in Name Uncommunicative Name Data Class Refused Bequest (LSP violations)

DesignSmell (2) Inappropriate Intimacy Lazy Class Feature Envy (Managers) Shotgun Surgery Parallel Inheritance Hierarchies Message Chains Component Without Interface Singletons

Design Related Anti-Patterns The Blob Functional Decomposition Poltergeist Golden Hammer Swiss Army Knife Kevorkian Component (Dead End) (Other Anti-Patterns deal with Architecture, Management, Code)

Thank you… Arnon Rotem-Gal-Oz