Single Responsibility Principle (SRP)

Similar documents
Produced by. Agile Software Development. Eamonn de Leastar

Open Closed Principle (OCP)

Software Engineering

Liskov Substitution Principle

Outline. Software Rots

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

OOD Smells and Principles

Bruno Bossola SOLID Design Principles

17.11 Bean Rules persistent

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

Conception Orientée Objets. Programmation SOLID

Principles of OO Design

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

CSC207H: Software Design SOLID. CSC207 Winter 2018

OO Class Design Principles

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

Build Testable Client and Service Applications

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

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

SOLID: Principles of OOD

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

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Intro to: Design Principles

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

COURSE 2 DESIGN PATTERNS

The following topics will be covered in this course (not necessarily in this order).

Component-Level Design

Object-Oriented Design I - SOLID

Inheritance in Java. Produced by: Eamonn de Leastar Dr. Siobhán Drohan

Top 7 Lessons From My First Big Silverlight Project

Highlights of Previous Lecture

Cursul 6 27 Martie 2017

Principles of Object-Oriented Design

Design Principles: Part 2

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

Course 2 October, 16, Adrian Iftene

Design Principles: Part 2

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

Object-Oriented Concepts and Design Principles

Introduction to Testing and Maintainable code

Object-oriented design principles

Single Responsibility Principle

Summary of the course lectures

Dependency Inversion, Dependency Injection and Inversion of Control. Dependency Inversion Principle by Robert Martin of Agile Fame

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

OO Package Design Principles

Contents. Unpleasant Code Smells. Refactoring

Agile Software Development

Advanced WCF 4.0 .NET. Web Services. Contents for.net Professionals. Learn new and stay updated. Design Patterns, OOPS Principles, WCF, WPF, MVC &LINQ

Inheritance and object compatibility

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

Java Control Statements

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

Class Design Principles

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Basic design patterns

On to Object-oriented Design

Software Design and SOLID Principles

Object Oriented Software Design - I

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

CS 520 Theory and Practice of Software Engineering Fall 2018

Princípy tvorby softvéru Dizajnové princípy

5. Application Layer. Introduction

CS 320 Introduction to Software Engineering Spring March 06, 2017

2IP15 Programming Methods

Software Engineering I (02161)

CS 520 Theory and Practice of Software Engineering Fall 2017

Design Patterns. CSC207 Fall 2017

09. Component-Level Design

CS 2340 Objects and Design

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

Object-Oriented Design II

Software Development

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

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

Agile Principles, Patterns, and Practices in C#

Design Patterns V Structural Design Patterns, 2

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

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

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Object-Oriented Design I

GRASP Design Patterns A.A. 2018/2019

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

Software Engineering: Design & Construction

Java Object Oriented Design. CSC207 Fall 2014

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN. Software Engineering Design: Theory and Practice

CSC207 Week 3. Larry Zhang

Object-Oriented Design

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

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

Software Development Project. Kazi Masudul Alam

Introduction to Object-Oriented Programming

Tecniche di Progettazione: Design Patterns

Influence of Design Patterns Application on Quality of IT Solutions

Architecting ios Project. Massimo Oliviero

Object-Oriented Design II - GRASP

Design Patterns. CSC207 Winter 2017

Introduction to Design Patterns

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

Transcription:

Single Responsibility Principle (SRP) Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhán Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/

SOLID Class Design Principles In the mid-1990s, Robert C. Martin gathered five principles for object-oriented class design, presenting them as the best guidelines for building a maintainable object oriented system. Michael Feathers attached the acronym SOLID to these principles in the early 2000s.

SOLID Class Design Principles S O L I D Single Responsibility Principle (SRP). Classes should have one, and only one, reason to change. Keep your classes small and single-purposed. Open-Closed Principle (OCP). Design classes to be open for extension but closed for modification; you should be able to extend a class without modifying it. Minimize the need to make changes to existing classes. Liskov Substitution Principle (LSP). Subtypes should be substitutable for their base types. From a client s perspective, override methods shouldn t break functionality. Interface Segregation Principle (ISP). Clients should not be forced to depend on methods they don t use. Split a larger interface into a number of smaller interfaces. Dependency Inversion Principle (DIP). High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.

SOLID Class Design Principles Module Scope S O L I D Single Responsibility Principle (SRP). Classes should have one, and only one, reason to change. Keep your classes small and single-purposed. Open-Closed Principle (OCP). Design classes to be open for extension but closed for modification; you should be able to extend a class without modifying it. Minimize the need to make changes to existing classes. Liskov Substitution Principle (LSP). Subtypes should be substitutable for their base types. From a client s perspective, override methods shouldn t break functionality. Interface Segregation Principle (ISP). Clients should not be forced to depend on methods they don t use. Split a larger interface into a number of smaller interfaces. Dependency Inversion Principle (DIP). High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.

SOLID Principles in Poster form http://blogs.msdn.com/b/cdndevs/archive/2009/07/15/the-solidprinciples-explained-with-motivational-posters.aspx SOLID Motivational Posters, by Derick Bailey, is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

S in SOLID - Single Responsibility Principle Every object should have a single responsibility and all of its services should be aligned with that responsibility. Responsibility is defined as a reason to change, and Wikipedia does a pretty good job of explaining it: As an example, consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.

O in SOLID - Open-Closed Principle Software entities such as classes, modules, functions and so on should be open for extension but closed for modification. The idea is that it s often better to make changes to things like classes by adding to or building on top of them (using mechanisms like subclassing or polymorphism) rather than modifying their code.

L in SOLID - Liskov Substitution Principle Subclases should be substitutable for the classes from which they were derived. For example, if MySubclass is a subclass of MyClass, you should be able to replace MyClass with MySubclass without bunging up the program.

I in SOLID - Interface Segregation Principle Many client specific interfaces are better than one general purpose interface. Make fine grained interfaces that are client specific. outside the scope of this module

D in SOLID - Dependency Inversion Principle High-level modules shouldn t depend on low-level modules, but both should depend on shared abstractions. In addition, abstractions should not depend on details instead, details should depend on abstractions. Depend on abstractions, not on concretions. outside the scope of this module In your own time, see the Manager example here.

SRP Single Responsibility Principle

SRP: The Single Responsibility Principle THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE. Each responsibility is an axis of change. When requirements change a change in responsibility amongst the classes. If a class assumes more than one responsibility more than one reason for it to change. changes to one responsibility may impair or inhibit the class ability to meet the others.

Rectangle example The Rectangle class has two methods: one draws the rectangle on the screen the other computes the area of the rectangle. Two applications use this class: one application uses Rectangle to help it with the mathematics of geometric shapes. the other uses the class to render a Rectangle on a window.

SRP Violation Rectangle has two responsibilities: provide a mathematical model of the geometry of a rectangle. render the rectangle on a graphical user interface. Violation of SRP: the GUI must be included in the in the computational geometry application. the class files for the GUI have to be deployed to the target platform. if a change to the Graphical Application causes the Rectangle to change for some reason, that change may force us to rebuild, retest, and redeploy the Computational Geometry Application.

SRP Fix Separate the two responsibilities into two separate classes Moves the computational portions of Rectangle into the GeometricRectangle class. Now changes made to the way rectangles are rendered cannot affect the ComputationalGeometry Application.

What is a Responsibility? A reason for change. If you can think of more than one motive for changing a class, then that class has more than one responsibility. interface Modem { void dial(string pno); void hangup(); void send(char c); char recv(); }

Modem Responsibilities interface Modem { void dial(string pno); void hangup(); void send(char c); char recv(); } Two responsibilities: connection management (dial and hangup functions) data communication (send and recv functions) They have little in common may change for different reason will be called from different parts of the applications They will change for different reasons.

Should the responsibilities be separated? It depends! How do you forsee the application changing? e.g. could the signature of the connection methods potentially change, without any change to the send/receive mechanism? interface Modem { void dial(string pno); void hangup(); void send(char c); char recv(); }

Should the responsibilities be separated? If the application can change in ways that cause the two responsibilities to change at different times separate the responsibilities. Separation here is at interface level and not class level. CAUTION: Needless complexity can occur when there is no foreseeable need to separate the responsibilities.

Another SRP Violation Coupling persistence services (store) with business rules (calculatepay) violates SRP.

Separate the Responsibilities

Example - Personal Information Manager Design an Application to manage a contact list. It should support: Console based UI Load/save to/from a file on disk Simple reports and search functions

AddressBook Propose two classes: Contact - to represent each contact AddressBook - to incorporate serialization reporting UI etc Violates SRP as AddressBook has multiple reasons to change Data structure change (e.g. HashMap to TreeMap) Serialization mechanism (e.g. binary to XML) Alternative reports (e.g. different formats and content) Command line syntax changes

Refactor Addressbook IAddressBook IContactReporter responsible for contact data structure responsible for format and content of reports ISerializationStrategy responsible for persistence IPim PimConsoleApp responsible for binding address book to serialization mechanism and for exposing coherent top level functionality responsible binding an running application to an IPim.

Pacemaker - package responsibilities transform the model into various formats Application services + user interface information model for the app general purpose application independent utilities

Pacemaker model responsibilities Represent individual locations Represent individual Activities Represent individual Users

Pacemaker utils responsibilities Centralise data/time formatting for application Encapsulate data structure serialisation Specialise serialisation for XML Specialise serialisation for JSON

Pacemaker parsers responsibilities Encapsulate parsing (transformation) requirements for app Specialise parsing for Ascii (using btc-ascii component) Specialise parsing for JSON (using jackson)

Pacemaker Response responsibilities Represent responses from an application to requests from clients (use HTTP terminology)

Pacemaker PacemakerAPI responsibilities Implement the core application features as represented by the Model.

Pacemaker PacemakerService responsibilities Expose the core application features to clients

Pacemaker PacemakerShell responsibilities Deliver a console user experience

Pacemaker controllers responsibilities Implement the core application features as represented by the Model. Represent responses from an application to requests from clients(information modelled for the app on HTTP) Expose the core application features to clients Deliver a console user experience

SRP Summary Changes in requirements are manifested as changes in class responsibilities. Therefore a cohesive responsibility is a single axis of change requirement changes often are restricted to a few cohesive responsibilities (in a reasonably designed system). Thus, to avoid coupling responsibilities that change for different reasons, a class should have only one responsibility, one reason to change. Violation of SRP causes spurious dependencies between modules that are hard to anticipate, in other words fragility.

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License. For more information, please see http://creativecommons.org/licenses/bync/3.0/