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

Similar documents
OO Class Design Principles

Principles of OO Design

Bruno Bossola SOLID Design Principles

Principles of Object-Oriented Design

Outline. Software Rots

Open Closed Principle (OCP)

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

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

SOLID: Principles of OOD

Object Oriented Software Design - I

Design Principles: Part 2

Stability. Introduction

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

Common mistakes and Basic Design Principles. David Rabinowitz

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

Design Principles: Part 2

Liskov Substitution Principle

CSC207H: Software Design SOLID. CSC207 Winter 2018

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

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Object-Oriented Design

Principles of Object-Oriented Design

17.11 Bean Rules persistent

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

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

Software Engineering

Highlights of Previous Lecture

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

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

Object-Oriented Design

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

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

Agile Software Development

OOD Smells and Principles

Why Use Object-Oriented Programming in the First Place?

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

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Summary of the course lectures

Component-Level Design

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

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

Conception Orientée Objets. Programmation SOLID

Object-oriented design principles

Single Responsibility Principle (SRP)

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

Software Engineering I (02161)

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

Intro to: Design Principles

Granularity. Inheritance Bi-directional. Association. Derived A1

Laboratorio di Tecnologie dell'informazione

Single Responsibility Principle

5. Application Layer. Introduction

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

COURSE 2 DESIGN PATTERNS

Lecture: Modular Design

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

Principles of Ruby Applica3on Design. Dean Wampler Senior Mentor and Consultant Object Mentor, Inc. Chicago, IL

Refactoring Without Ropes

OO Design Principles

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

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

Build Testable Client and Service Applications

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

Software Engineering /48

Programming 2. Object Oriented Programming. Daniel POP

Class Design Principles

Test Code Patterns. How to design your test code

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs

a correct statement? You need to know what the statement is supposed to do.

Agile Principles, Patterns, and Practices in C#

Introduction to Object Oriented Programming. (Hebrew University, CS / Spring 2012 )

Animations involving numbers

Software Engineering Design & Construction

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

Software Design and SOLID Principles

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

QUIZ. What is wrong with this code that uses default arguments?

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Tecniche di Progettazione: Design Patterns

Maintainable Software. Software Engineering Andreas Zeller, Saarland University

Influence of Design Patterns Application on Quality of IT Solutions

1: Introduction to Object (1)

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

CSE 331 Final Exam 3/16/15 Sample Solution

GRASP Design Patterns A.A. 2018/2019

The Release Reuse Equivalency Principle (REP) The Common Closure Principle (CCP) The Common Reuse Principle (CRP)

Lecture 10 Notes Linked Lists

Introduction to Testing and Maintainable code

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

CSC207 Week 3. Larry Zhang

Course December Adrian Iftene

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

Component-Level Design. Slides copyright 1996, 2001, 2005, 2009 by Roger S. Pressman. For non-profit educational use only

Understading Refactorings

Type Checking in COOL (II) Lecture 10

Reliable programming

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Object Relationships

A few more things about Agile and SE. Could help in interviews, but don t try to bluff your way through

Transcription:

S.O.L.I.D. Principles of Object-Oriented Class Design Fifteen Years Later. Copyright 1998-2010 by Object Mentor, Inc Portions of this material are Copyright 1998, by Addison Wesley Longman,Inc. and have been reproduced here with permission.

Dependency Management What is dependency management? What bearing does DM have on software? What is the result of poor DM? What is the advantage of good DM? Advanced Principles I - 2

What is dependency management? A simple idea - as interdependencies increase, features like reusability, flexibility, and maintainability decrease. Dependency management is controlling interdependencies. Advanced Principles I - 3

What bearing does DM have on software? Coupling and cohesion are the eternal concerns of software development One can say that OO is just a set of tools and techniques for Dependency Management Advanced Principles I - 4

What is the penalty for practicing poor DM? A system with poor dependency structure will typically exhibit these four negative traits: It is rigid It is fragile It is not reusable It has high viscosity Advanced Principles I - 5

It is Rigid Rigidity is the inability to be changed The impact of a change cannot be predicted If not predicted, it cannot be estimated Time and cost cannot be quantified Managers become reluctant to authorize change Official Rigidity for Roach Motel modules Advanced Principles I - 6

Changes with Rigidity Officially Rigid Area Where the change should be made The System Where the change must be made now Are we containing risk, or spreading rot? Advanced Principles I - 7

It is Fragile 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 Advanced Principles I - 8

Increasing Risk Defects v. Cumulative Modifications 1.0 Changes Systems tend to become increasingly fragile over time. Intentional, planned partial rewrites may be necessary to sustain growth and maintenance. Advanced Principles I - 9

It is not reusable Desirable parts of the design are dependent upon undesirable parts The work and risk of extracting the desirable part may exceed the cost of redeveloping from scratch. Advanced Principles I - 10

The Trailer Advanced Principles I - 11

It has high viscosity Viscosity is resistance to fluid motion. When the right changes are much more difficult than hacking, the viscosity of the system is high. Over time, it will become increasingly difficult to continue developing the product. Advanced Principles I - 12

What is the benefit of February, 99 good DM? Interdependencies are managed, with firewalls separating aspects that need to vary independently. More Flexible Less fragile, the bugs are boxed in Easier to reuse Easier to make the right change Advanced Principles I - 13

What causes Code Rot? It s been blamed on stupidity, lack of discipline, and phases of the moon, but... A case study The Copy Routine Advanced Principles I - 14

First Version All designs start well ReadKeyboard Copy WritePRinter void copy(void) { int ch; while( (ch=readkeyboard())!= EOF) WritePrinter(ch); } The program is an overnight success! How could it be more simple, elegant, and maintainable? Advanced Principles I - 15

Second Version Oh, no! Nobody said the requirements might change! We sometimes want to read from paper tape reader. We could put a parameter in the call, but we have hundreds of users already! No big deal, this is just an exception we can make it work. Advanced Principles I - 16

Second Version Design bool GtapeReader = false; // remember to clear void copy(void) { int ch; while( (ch=gtapereader? ReadTape() : ReadKeyboard())!= EOF) WritePrinter(ch); } Copy ReadKeyboard WritePrinter ReadTape Advanced Principles I - 17

Third Version How unexpected! Requirements changed again! It seems that sometimes we need to write to a paper tape punch. We ve had this problem before, and just added a flag. Looks like it should work again. bool GtapeReader = false; Bool GtapePunch = false; // remember to clear void copy(void) { int ch; while( (ch=gtapereader? ReadTape() : ReadKeyboard())!= EOF) GtapePunch? WritePunch(ch) : WritePrinter(ch); } Advanced Principles I - 18

Example of a Good Design First and only version. void Copy() { int c; while( (c=getchar())!= EOF) putchar(c); } But wait! Aren t we supposed to be learning OO design? This isn t OO is it? Advanced Principles I - 19

is it? It is a small program based on abstractions! FILE is an abstraction It represents some kind of byte stream It has many variations It has methods Read, Write, getchar, putchar, etc The methods are *dynamically* bound FILE is a class, just implemented differently. Advanced Principles I - 20

Rephrased in OO interface Reader { char read(); } Copy interface Writer { void write(char c); } «interface» Reader KeyboardReader «interface» Writer PrinterWriter 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); } private Reader itsreader; private Writer itswriter; } Advanced Principles I - 21

Dependency Management Review Why do programs tend to rot over time? What is dependency management? What are four qualities of good designs? Are OO programs always simpler than non-oo versions? Why would anyone want to use a paradigm that may result in more complex designs? Why are compile and link time important? Advanced Principles I - 22

Class Design Principles From: Agile Software Development: Principles, Patterns, and Practices. Robert C. Martin, Prentice Hall, 2002. SRP: The Single Responsibility Principle OCP: The Open/Closed Principle LSP: The Liskov Substitution Principle ISP: The Interface Segregation Principle DIP: The Dependency Inversion Principle Advanced Principles I - 23

The Single Responsibility Principle A class should have one, and only one, reason to change. Payroll Employee + CalcPay + ReportHours + WriteEmployee Advanced Principles I - 24

The Single February, 99 Responsibility Principle. (SRP) Report Writer Payroll Employee + CalcPay Employee Repository Advanced Principles I - 25

Open/Closed Principle Modules should be open for extension, but closed for modification -Bertrand Meyer A principle which states that we should add new functionality by adding new code, not by editing old code. Defines a lot of the value of OO programming Abstraction is the key Advanced Principles I - 26

Abstraction is Key Abstraction is the most important word in OOD Client/Server relationships are open Changes to servers cause changes to clients Abstract servers close clients to changes in implementation. Client Client Server Abstract Server Concrete Server Advanced Principles I - 27

The Shape Example Procedural (not closed) implementation OO (closed) implementation Advanced Principles I - 28

Procedural (open) version Shape.h enum ShapeType {circle, square}; struct Shape {enum ShapeType itstype;}; Circle.h Square.h struct Circle { enum ShapeType itstype; double itsradius; Point itscenter; }; void DrawCircle(struct Circle*) struct Square { enum ShapeType itstype; double itsside; Point itstopleft; }; void DrawSquare(struct Square*) DrawAllShapes.c #include <Shape.h> #include <Circle.h> #include <Square.h> typedef struct Shape* ShapePtr; void DrawAllShapes(ShapePtr list[], int n) { int i; for( i=0; i< n, i++ ) { ShapePtr s = list[i]; switch ( s->itstype ) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; } } } Advanced Principles I - 29

What is wrong with the code? It can be demonstrated to work. Isn t that the important thing? DrawAllShapes is not closed. Switch/case tend to recur in diverse places. If we add a shape, we add to the switch/case All switch/case statements must be found and editd. Switch/Case statements are seldom this tidy When we add to the enum, we must rebuild everything The software is both rigid and brittle Advanced Principles I - 30

A Closed Implementation Shape.h Class Shape { public: virtual void Draw() const =0; }; Circle.h Class Circle: public Shape { public: virtual void Draw() const; }; Square.h Class Square: public Shape { public: virtual void Draw() const; }; DrawAllShapes.cpp #include <Shape.h> void DrawAllShapes(Shape* list[],int n) { for(int i=0; i< n; i++) list[i]->draw(); } Advanced Principles I - 31

Strategic Closure No program is 100% closed. Closure Against What? Closure is strategic. You have to choose which changes you ll isolate yourself against. What if we have to draw all circles first? Now DrawAllShapes must be edited (or we have to hack something) Opened Where? Somewhere, someone has to instantiate the individual shapes. It s best if we can keep the dependencies confined Advanced Principles I - 32

Picking Targets Technicians and domain users list Ways that the system is expected to change Ways that the system has already changed Isolate these to kinds of changes, not specific changes schema changes sensor hardware changes data store technology Keep this list handy throughout design Don t make the changes, just allow for them. Advanced Principles I - 33

Open/Closed Review What does the open/closed principle say? What does that mean practically? How can it be achieved? What is strategic closure? How can this be achieved in design? What if you can t close completely? Advanced Principles I - 34

Liskov Substitution Principle Derived classes must be usable through the base class interface, without the need for the user to know the difference. All derived classes must be substitutable for their base classes This principle guides us in the creation of abstractions. Advanced Principles I - 35

Square/Rectangle A square is-a rectangle, right? So lets consider Square as a subtype of Rectangle. Rectangle We can make it work: Uh, oh. This doesn t quite seem to fit -height : real -width : real void Square::SetWidth(double w) +SetHeight() { +SetWidth() width = w; height = w; } void Square::SetHeight(double h) { width = h; Square height = h; } Advanced Principles I - 36

Substitution denied! It is reasonable for users of a rectangle to expect that height and width may change independently. These expectations are preconditions and postconditions Bertrand Meyer calls it Design by Contract Post condition contract for rectangle is width = new Width height = old height Square violates Rectangle s contract Advanced Principles I - 37

Liskov Substitution Principle (cont.) A client of rectangle expects height and width to be changed independently void setaspectratio( Rectange* r, double ratio ); By deriving Square from Rectangle, we are allowing someone to set the aspect ratio of a Square! We can still make it work if ( typeid(r) == typeid(rectangle) ) Violates Open/Closed Principle! Advanced Principles I - 38

Liskov Substitution Design by Contract Bertrand Meyer Principle (cont.) Preconditions, postconditions, invariants Rectangle's postconditions for setwidth() width = newwidth length = oldlength Square can require no more of clients, nor promise any less Doesn't maintain invariant of length Violates the contract Advanced Principles I - 39

LSP Guides the Creation of Abstractions Abstractions do not stand alone Abstractions don t always conform to real world expectations Violating LSP is tantamount to violating the OCP Advanced Principles I - 40

LSP Review What does the LSP state? What is the risk if LSP is violated? What is Design by Contract? Advanced Principles I - 41

Dependency Inversion Principle Details should depend on abstractions. Abstractions should not depend on details. Copy Copy V. Reader Writer ReadKeyboard WritePRinter KeyboardReader PrinterWriter Advanced Principles I - 42

DIP Implications Everything should depend upon abstractions Avoid deriving from concrete classes Avoid associating to concrete classes Avoid aggregating concrete classes Avoid dependencies on concrete components Advanced Principles I - 43

Dependency Inversion February, 99 Principle (cont.) Legitimate reasons to violate Creation of objects new Circle creates a dependency on concrete class localize these dependencies using factories Nonvolatile classes string, vector, etc. providing they are stable Advanced Principles I - 44

Interface Segregation Principle Helps deal with fat or inappropriate interfaces Sometimes class methods have various groupings. These classes are used for different purposes. Not all users rely upon all methods. This lack of cohesion can cause serious dependency problems These problems can be refactored away. Advanced Principles I - 45

Interface Pollution by collection Distinct clients of our class have distinct interface needs. Client 1 Client 2 Client 3 Server + C1Function() + C2Function() + C3Function() Advanced Principles I - 46

A Segregated Example Client 1 Client 2 Client 3 «interface» Client 1Interface «interface» Client 2Interface «interface» Client 3Interface + C1Function() + C2Function() + C3Function() Server + C1Function() + C2Function() + C3Function Advanced Principles I - 47

ATM UI Example Withdraw Deposit Transfer «interface» ATM UI + GetWithdrawAmountAndAccount() + GetDepositAmountAndAccount() + GetTransferAmountAndAccounts() French ATM UI English ATM UI Advanced Principles I - 48

A Segregated ATM UI Example Deposit Withdraw Transfer «interface» ATM Deposit UI «interface» ATM Withdraw UI «interface» ATM Transfer UI + GetDepositAmountAndAccount() + GetWithdrawAmountAndAccount + GetTransferAmountAndAccounts() «interface» ATM UI + GetWithdrawAmountAndAccount() + GetDepositAmountAndAccount() + GetTransferAmountAndAccounts() French ATM UI English ATM UI Advanced Principles I - 49

Logger Example Code needing to log something Log Control Application «interface» EventLog + Log(s: string*) «interface» LogManager + SendLotToFile(name) + LogToMem(size) + LogToConsole() ManagedEventLog + Log(s: string*) + SendLotToFile(name) + LogToMem(size) + LogToConsole() Advanced Principles I - 50

ISP Review What is the ISP? What does it affect? How do these problems arise? Does it really provide a real solution or just a restructuring of the problem? When is it worth the effort? Advanced Principles I - 51

Four Class Design Principles - Review OCP Extend function without editing code LSP Child instances substitute cleanly for base DIP Depend on abstractions instead of details ISP Split interfaces to manage dependencies Advanced Principles I - 52