Bruno Bossola SOLID Design Principles

Similar documents
Principles of Object-Oriented Design

17.11 Bean Rules persistent

Principles of OO Design

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

Common mistakes and Basic Design Principles. David Rabinowitz

Outline. Software Rots

OO Class Design Principles

S.O.L.I.D. Principles of

Single Responsibility Principle (SRP)

CSC207H: Software Design SOLID. CSC207 Winter 2018

Open Closed Principle (OCP)

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

SOLID: Principles of OOD

Highlights of Previous Lecture

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Liskov Substitution Principle

OOD Smells and Principles

Design Principles: Part 2

CMPS 115 Winter 04. Class #10 (2004/02/05) Changes/Review Programming Paradigms Principles of OOD <break> Design Patterns

Produced by. Agile Software Development. Eamonn de Leastar

Software Engineering

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Object Oriented Software Design - I

Design Principles: Part 2

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

Conception Orientée Objets. Programmation SOLID

Object design. François Schwarzentruber ENS Cachan Antenne de Bretagne

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

S.O.L.I.D: Software Engineering Principles

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Intro to: Design Principles

Object-oriented design principles

Object design. François Schwarzentruber ENS Cachan Antenne de Bretagne

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Stability. Introduction

11/4/15. Review. Objec&ves. Refactoring for Readability. Review. Liskov Subs&tu&on Principle (LSP) LISKOV SUBSTITUTION PRINCIPLE

Build Testable Client and Service Applications

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

COURSE 2 DESIGN PATTERNS

Principles of Object-Oriented Design

1 Introduction 2 Complex Systems 3 Object Model 4 Dependency Management 5 Class Design. Object Oriented Programming in Physics

Component-Level Design

Lecture: Modular Design

Course 2 October, 16, Adrian Iftene

Object Design Guidelines

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

20 Years of. Improve the Design of your Code. Dr Dimitris Dranidis JAVA Meetup Group, Thessaloniki May 2015

Single Responsibility Principle

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

Class Design Principles

CS 520 Theory and Practice of Software Engineering Fall 2017

Clean Code * * Or why is more important how we write code rather what we write. Assoc. prof. Catalin Boja, Asist. Bogdan Iancu, Lect.

Tecniche di Progettazione: Design Patterns

Objec&ves. Review. Liskov Subs&tu&on Principle Refactoring for Extensibility. What are reasons that we refactor our code?

CS 520 Theory and Practice of Software Engineering Fall 2018

Why Use Object-Oriented Programming in the First Place?

CSC207 Week 3. Larry Zhang

Smart formatting for better compatibility between OpenOffice.org and Microsoft Office

Separation of Concerns

C++ Modern and Lucid C++ for Professional Programmers

Agile Software Development

Designing with patterns - Refactoring. What is Refactoring?

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

Software Design and SOLID Principles

Cursul 6 27 Martie 2017

Animations involving numbers

Programming 2. Object Oriented Programming. Daniel POP

Object-Oriented Design

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

How to approach a computational problem

Object-Oriented Design

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Introduction to Testing and Maintainable code

CS 320 Introduction to Software Engineering Spring March 06, 2017

Lessons Learned. Johnny Bigert, Ph.D., Skype/Microsoft October 26, 2011

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Helping the Compiler Help You. Thomas Dy

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

The following content is provided under a Creative Commons license. Your support

Summary of the course lectures

Software Engineering /48

Contents. Unpleasant Code Smells. Refactoring

Building level Rail embankments and correcting heights with XML. (V1.0) (Tangram)

OO Design Principles

9 th CA 2E/CA Plex Worldwide Developer Conference 1

How To Test Your Code A CS 1371 Homework Guide

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Information Expert (or Expert)

BBC Learning English Face up to Phrasals Mark's Mistake

Refactoring Without Ropes

Design by Contract in Eiffel

09. Component-Level Design

Mastering Data Abstraction or Get nit-picky on design advantages

Transcription:

Bruno Bossola SOLID Design Principles

About me C Developer since 1988 Java developer since 1996 XP Coach during 2000 2001 Lead coordinator and co founder of JUG Torino in 2001 Sun Java Champion since 2005 Speaker at Javapolis and Jazoon conferences

Agenda What design exactly is about? Bad design Good design SOLID principles Conclusions

Warning! Most of those slides maps directly various articles available to the web, expecially from objectmentor.com (thanks Bob!) Note that those articles are dated... 1992 1998!!!...so please don't force me to get here next year!!!!

Design What's the meaning of design? What's the difference if compared to analysis? Analysis is about what Design is about how

Design Why do we need (good) design? to deliver faster to manage change to deal with complexity

Design How do we know a design is bad? WTF? Hey, this is not the way I WTF? would have done this! WTF?? I can't WTF?? understand WTF? WTF??? this piece of code!

Design Ok, we probably need better criterias :) Are there any symptoms of bad design? Rigidity Immobility Fragility Viscosity

Rigidity the impact of a change is unpredictable every change causes a cascade of changes in dependent modules a nice two days work become a kind of endless marathon costs become unpredictable

Fragility the software tends to break in many places on every change the breakage occurs in areas with no conceptual relationship on every fix the software breaks in unexpected ways

Immobility it's almost impossible to reuse interesting parts of the software the useful modules have too many dependencies the cost of rewriting is less compared to the risk faced to separate those parts

Viscosity a hack is cheaper to implement than the solution within the design preserving design moves are difficult to think and to implement it's much easier to do the wrong thing rather than the right one

Design What's the reason why a design becomes rigid, fragile, immobile, and viscous? improper dependencies between modules

Good design So, what are the characteristics of a good design? high coesion low coupling

Good design How can we achieve a good design? SRP OCP LSP ISP DIP Let's go SOLID!!!

SOLID An acronym of acronyms! It recalls in a single word all the most important pricinple of design SRP Single Responsabiity Principle OCP Open Closed Principle LSP Liskov Substitution Principle ISP Interface Segregation Principle DIP Dependency Inversion Principle

BREAK! What is the best comment in source code you have ever encountered? (part 1) // //* Dear maintainer: //* You may think you know what the following code does. //* Once But you are dont. done Trust trying me. to 'optimize' this routine, //* and Fiddle have with realized it, and what youll a terrible spend many mistake a sleepless that was, //* please night increment cursing the the moment following you thought counter youd as a be warning clever //* to enough the next to "optimize" guy: the code below. //* Now close this file and go play with something else. //*/ total_hours_wasted_here = 16 // (source: stackoverflow.com)

Single Responsability Principle A software module should have one and only one responsability Easier: a software module should have one reason only to change It translates directly in high coesion It's usually hard to see different responsabilities

SRP Is SRP violated here? interface Modem { public void dial(string pno); public void hangup(); public void send(char c); public char recv(); }

SRP Is SRP violated here? interface Employee { public Pay calculate(); public void report(writer w); public void save(); public void reload(); }

SRP identify things that are changing for different reasons group together things that change for the same reason note the bias compared to "classical" OO smells? *Manager, *Controller, *Handler you really don't know how to name those SRP requires very precise names, very focused classes

Open Closed Principle Modules should be open for extension but closed to modification theorized in 1998 by Bertrand Meyer in a classical OO book you should be able to extend the behavior of a module without changing it!

OCP? void DrawAllShapes(ShapePointer list[], int n) { for (int 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; } }

OCP? public void draw(shape[] shapes) { for( Shape shape : shapes ) { switch (shape.gettype()) { case Shape.SQUARE: draw((square)shape); break; case Shape.CIRCLE: draw((circle)shape); break; } } }

OCP! public void draw(shape[] shapes) { for( Shape shape : shapes ) { shape.draw(); } }

OCP Abstraction is the key! Uncle Bob's recipe keep the things that change frequently away from things that don't change if they depend on each other, things that change frequently should depend upon things don't change

BREAK! What is the best comment in source code you have ever encountered? (part 2) /** * For the brave souls who get this far: * You are the chosen ones, the valiant //* I knights dedicate of all programming this code, all who my toil work, away, to my wife, //* Darlene, without who rest, will fixing have to our support most me awful and our code. three // children and the dog once it gets released into * To you, true saviors, kings of men, // the public. * I say this: never gonna give you up, * never gonna let you down, *... (continues :D) */ (source: stackoverflow.com)

Liskov Substitution Principle 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. uh?

LSP Try #2: Function that use pointers or references to base classes must be able to use objects of derived classes without knowing it

LSP Try #3: Given an entity with a behavior and some possible sub entities that could implement the original behavior, the caller should not be surprised by anything if one of the sub entities are substituted to the original entity

LSP (by example) How would you model the relationship between a square and a rectangle? Should the square class extends rectangle?

LSP (by example) Of course, isn't the Square a kind of Rectangle, after all? It seems an obvious ISA

LSP (by example) But... what about: rectangle has two attributes, width and height: how can we deal with that? how do we deal with setwidth() and setheight()? Is it safe?

LSP (by example) No, behavior is different If I pass a Square to a Rectangle aware function, then this may fail as it may assume that width and heigth are managed separately Geometry!= Code

Interface Segregation Principle fat classes may happen :( usually there are many clients each using a subset of the methods of such classes such client classes depend upon things they don't use what happens when the big class changes? all depending modules must also change

ISP ISP states that clients should not know about fat classes instead they should rely on clean cohesive interfaces you don't want to depend upon something you don't use

BREAK! What is the best comment in source code you have ever encountered? (part 3) // drunk, fix later // // Magic. When Do I wrote not touch this, only God and I // understood what I was doing // // // Now, I'm God sorry only knows (source: stackoverflow.com)

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

DIP? enum OutputDevice {printer, disk}; void copy(outputdevice dev) { int c; while((c=readkeyboard())!= EOF) { if (dev == printer) writeprinter(c); else writedisk(c); } }

DIP! void copy(reader input, Writer output) { int c; while((c=input.read())!= EOF) { output.write(c); } }

DIP don't depend on anything concrete, depend only upon abstraction high level modules should not be forced to change because of a change in low level / technology layers drives you towards low coupling

Conclusion good design is needed to successfully deal with change the main forces driving your design should be high cohesion and low coupling SOLID principles put you on the right path warning: these principles cannot be applied blindly :)

Q&A bruno.bossola@jugtorino.it