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

Similar documents
Object Oriented Software Design - I

Liskov Substitution Principle

Chapter 6: Inheritance

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

Conception Orientée Objets. Programmation SOLID

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Lecture: Modular Design

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

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

Design Principles: Part 2

Design Principles: Part 2

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

Software Development Project. Kazi Masudul Alam

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

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

CSC207H: Software Design SOLID. CSC207 Winter 2018

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Principles of OO Design

Software Engineering

Software Reuse Techniques

CS 520 Theory and Practice of Software Engineering Fall 2017

On Polymorphism and the Open-Closed Principle

Principles of Object-Oriented Design

OO Class Design Principles

On Polymorphism and the Open-Closed Principle

CS 520 Theory and Practice of Software Engineering Fall 2018

Object-oriented design principles

Test Code Patterns. How to design your test code

The S.O.L.I.D. Principles. of Object Oriented Programming

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

CS 320 Introduction to Software Engineering Spring March 06, 2017

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Single Responsibility Principle

Bruno Bossola SOLID Design Principles

Introduction to Software Reuse

Single Responsibility Principle (SRP)

Object-Oriented Design I - SOLID

Introduction. Introduction. Introduction

Reuse in Object Oriented Programming

Reviewing OO Concepts

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Open Closed Principle (OCP)

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

ITI Introduction to Computing II

ITI Introduction to Computing II

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

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

Inheritance (Deitel chapter 9)

CS 320 Introduction to Software Engineering Spring March

Introduction to Object-Oriented Programming

OOD Smells and Principles

The Liskov Substitution Principle

Intro to: Design Principles

Inheritance and object compatibility

Class Design Principles

Software Engineering Design & Construction

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

Object-Oriented Concepts and Design Principles

Component-Level Design

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

OBJECT ORİENTATİON ENCAPSULATİON

Software Engineering I (02161)

Reviewing OO Concepts

Outline. Software Rots

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

CSC207 Week 3. Larry Zhang

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Produced by. Agile Software Development. Eamonn de Leastar

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Course 2 October, 16, Adrian Iftene

S Single Responsability Principle

Testing Object-Oriented Software. 22 November 2017

TWO-DIMENSIONAL FIGURES

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

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

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

Classes as Blueprints: How to Define New Types of Objects

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java interface Lecture 15

Cursul 6 27 Martie 2017

Programming in the large

First Name: AITI 2004: Exam 2 July 19, 2004

Chapter 11 Classes Continued

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

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

OBJECT ORIENTED PROGRAMMING

17.11 Bean Rules persistent

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

COMP 249: Object Oriented Programming II. Tutorial 11: Generics

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

Chapter 9 - Object-Oriented Programming: Polymorphism

COMP 401 Fall Recitation 6: Inheritance

Extending Classes (contd.) (Chapter 15) Questions:

Transcription:

DCC / ICEx / UFMG S.O.L.I.D: Software Engineering Principles Eduardo Figueiredo http://www.dcc.ufmg.br/~figueiredo

S.O.L.I.D Principles These principles intend to create systems that are easier to maintain and extend S O L I D Single-responsibility principle Open-closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle

Single-Responsibility Principle

Single Responsibility Principle A class should have only one reason to change A responsibility is a reason for change If a class assumes more than one responsibility, it will have more than one reason for change This principle is related to low cohesion

Example of Violation Geometry Application Rectangle + draw() + area() Graphical Application GUI

Example of Violation The Rectangle class has two responsibilities Geometry Application Rectangle + draw() + area() Graphical Application Application 1 never draws the rectangle on screen GUI Application 2 draws the rectangle on screen

Separated Responsibilities Solution: move the drawing responsibility to another class Geometry Application Graphical Application Rectangle RectangleDrawer GUI + area() + draw()

Open-Closed Principle

Open-Closed Principle Software entities should be open for extension, but closed for modification Modules should never change Behavior of modules can be extended When requirements change, you extend the behavior of modules by adding code Encapsulation is a key concept Avoid public and global variables

Example of Violation public double sumarea(object[] shapes) { double area = 0; for (int i = 0; i<shapes.length; i++) { if (shapes[i] instanceof Square) area += Math.pow(((Square)shapes[i]).getLength(), 2); if (shapes[i] instanceof Circle) area += Math.PI * ( Math.pow(((Circle)shapes[i]).getRadius(), 2) ); } return area; } Square Circle # length + getlength() # radius + getradius() Geometry Application

Open for Extension Solution: client is coupled only to abstraction Geometry Application Shape + area() public double sumarea(shape[] shapes) { double area = 0; for (int i = 0; i<shapes.length; i++) { area += shapes[i].area(); } return area; } Square # length + getlength() + area() Circle # radius + getradius() + area()

Liskov Substitution Principle

Liskov Substitution Principle Functions that use references to classes must be able to use objects of subclasses without knowing it This principle implies in careful use of inheritance ( is a relationship) All subclasses must conform to the behavior that clients expect Functions which use base classes should be reused without penalty

Square extends Rectangle Rectangle An application has to manipulate squares in addition to rectangles Square Inheritance represents the is a relationship Square is a rectangle ( is a relationship holds)

Useless Inherited Members Rectangle # height # width + setheight() + getheight()... Square A square does not need both height and width fields (and other members) It inherits them anyway The methods setheight() and setwidth() are inappropriate How to fix them?

Example of Violation Rectangle # height # width + setheight() + getheight()... Square + setheight() + setwidth() public class Square extends Rectangle { } Workaround: override the setheight() and setwidth() methods public void setheight(int h) { this.height = h; this.width = h; } public void setwidth(int w) { this.height = w; this.width = w; }

Example of Violation Rectangle # height # width + setheight() + getheight()... Geometry Application Problem: testarea() works for Rectangle, but not for Square Square + setheight() + setwidth() public void testarea(rectangle r) { r.setheight(5); r.setwidth(4); asserttrue("test rectangle area", ((r.getheight() * r.getwidth()) == 20) ); }

Interface Segregation Principle

Interface Segregation Principle Clients should not be forced to depend upon interfaces that they do not use This principle deals with the disadvantages of fat interfaces Classes with fat interfaces are not cohesive There are objects with non-cohesive functionalities, but clients should know them by their (many) cohesive interfaces

Example of Violation IShape + area() + volume() Square + area() + volume() Cube + area() + volume() Both square and cube are shapes The is a relationship holds A square does not need the volume() method It inherits this method anyway

Segregated Interfaces IShape + area() ISolidShape + volume() Solution: break down a fat interface into smaller interfaces with cohesive sets of responsibilities Square Cube + area() + area() + volume()

Dependency Inversion Principle

Dependency Inversion Principle High level modules should not depend upon low level modules. Both should depend upon abstractions We want to reuse high level modules High level modules are hard to reuse, when they depend on details We easily use low level modules as functions or libraries

Example of Violation PasswordReminder + restoretip() public String restoretip() { String tip = ""; MySQLConnection c = new MySQLConnection(); // connect to MySQL database // recovery password tip // close database connection return tip; } MySQLConnection + connect()... Problem: if you change your database engine later, you have to edit the PasswordReminder class

Dependency Inversion Solution: the PasswordReminder class can connect to the database without knowing the engine PasswordReminder DBConnection + restoretip() + connect()... MySQLConnection + connect()...

Template Method Example The Template Method design pattern is often an example of this principle public class Trip { public final void performtrip(){ arrive(); dodaya(); dodayb(); dodayc(); leave(); } public void arrive() {... } public abstract void dodaya(); public abstract void dodayb(); public abstract void dodayc(); } public void leave() {... }

Bibliography Robert C. Martin. Agile Software Development, Principles, Patterns, and Practices. Pearson Education Limited, 2013. Chapter 8 to 12