CSC207H: Software Design SOLID. CSC207 Winter 2018

Similar documents
Software Engineering

COURSE 2 DESIGN PATTERNS

Object-Oriented Concepts and Design Principles

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

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

Tecniche di Progettazione: Design Patterns

Object-Oriented Design I

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

Object-Oriented Design I - SOLID

Object Oriented Software Design - I

CHAPTER 5 GENERAL OOP CONCEPTS

Inheritance and object compatibility

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

Bruno Bossola SOLID Design Principles

SOLID: Principles of OOD

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

OO Class Design Principles

Intro to: Design Principles

Open Closed Principle (OCP)

Abstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles

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

Single Responsibility Principle (SRP)

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

CSC207 Week 3. Larry Zhang

GRASP Design Patterns A.A. 2018/2019

Lecture: Modular Design

What are the characteristics of Object Oriented programming language?

17.11 Bean Rules persistent

Build Testable Client and Service Applications

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Object-Oriented Design II

Object-Oriented Design II - GRASP

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

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

Single Responsibility Principle

Java Object Oriented Design. CSC207 Fall 2014

Influence of Design Patterns Application on Quality of IT Solutions

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

CS 520 Theory and Practice of Software Engineering Fall 2017

Object oriented programming Concepts

From Module To Objects

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

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Principles of Object-Oriented Design

ROBOT OOP. Learning the basics of Object Oriented Programming using robots from popular culture

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Object Relationships

Chapter 5 Object-Oriented Programming

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

Chapter3: Introduction to Classes and Objects

CS-202 Introduction to Object Oriented Programming

09. Component-Level Design

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

Structured Programming

CS 320 Introduction to Software Engineering Spring March 06, 2017

Component-Level Design

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object-Oriented Design

CS 520 Theory and Practice of Software Engineering Fall 2018

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

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

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

Software Development

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

Summary of the course lectures

Introduction. Object-Oriented Programming Spring 2015

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

What is a Programming Paradigm

Agile Software Development

Object-Oriented Programming in C++/Handout 01 Page 1 of 8

Design Patterns. CSC207 Winter 2017

Originality is Overrated: OO Design Principles

Elementary Concepts of Object Class

OO Design Principles

Object- Oriented Design with UML and Java Part I: Fundamentals

Design patterns. Jef De Smedt Beta VZW

Conception Orientée Objets. Programmation SOLID

Object-Oriented Design

Separation of Concerns

Data Structures (list, dictionary, tuples, sets, strings)

Software Development Project. Kazi Masudul Alam

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

Liskov Substitution Principle

Basic design patterns

CAS 703 Software Design

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

The Analysis and Design of the Object-oriented System Li Xin 1, a

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

Design Principles: Part 2

Chapter 8: Class and Method Design

Design Patterns. CSC207 Fall 2017

Highlights of Previous Lecture

Introduction to Object-Oriented Programming

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

SEEM4570 System Design and Implementation Lecture 09 Object Oriented Programming

1. Write two major differences between Object-oriented programming and procedural programming?

Chapter 1: Object-Oriented Programming Using C++

Design Principles: Part 2

Transcription:

SOLID CSC207 Winter 2018 1

SOLID Principles of Object-Oriented Design How do we make decisions about what is better and what is worse design? Principles to aim for instead of rules. e.g. there is no maximum or minimum number of classes you should have in your program. But if the number of classes violates a generally accepted principle, you should reconsider your class structure. The SOLID principles are useful and cover most major situations you are likely to encounter. 2

Software Design Goals A major goal when programming is to write a program that is: easy-to-read hard-to-break maintainable efficient Software design has you use a set of principles and techniques that help you do this. These help you make software design decisions and defend them. 3

Fundamental OOP Concepts Abstraction the process of distilling a concept to a set of essential characteristics. Encapsulation the process of binding together data with methods that manipulate that data, and hiding the internal representation. The result of applying abstraction and encapsulation is (often) a class with instance variables and methods that together model a concept from the real world. Inheritance the concept that when a subclass is defined in terms another class, the features of that other class are inherited by the subclass. Polymorphism ( many forms ) the ability of an expression (such as a method call) to be applied to objects of different types. 4

Fundamental OOD goals: low coupling, high cohesion Coupling how much a class is directly linked to another class High coupling means that changes to one class may lead to changes in several other classes. Low coupling is, therefore a desired goal. Cohesion how much the features of a class belong together. Low cohesion means that methods in a class operate on unrelated tasks. This means the class does jobs that are unrelated. High cohesion means that the methods have stronglyrelated functionality. 5

Fundamental OOD principles SOLID: five basic principles of objectoriented design (Developed by Robert C. Martin, affectionately known as Uncle Bob.) Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle 6

Single Responsibility Principle ( Each class does one thing and does it well ) Every class should have a single responsibility. Another way to view this is that a class should only have one reason to change. But who causes the change? 7

Single Responsibility Principle ( Each class does one thing and does it well ) But who causes the change? This principle is about people.... When you write a software module, you want to make sure that when changes are requested, those changes can only originate from a single person, or rather, a single tightly coupled group of people representing a single narrowly defined business function. You want to isolate your modules from the complexities of the organization as a whole, and design your systems such that each module is responsible (responds to) the needs of just that one business function. Gather together the things that change for the same reasons. Separate those things that change for different reasons. [Uncle Bob, The Single Responsibility Principle] 8

Open/Closed Principle ( Open for extension, closed for modification. ) Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Add new features not by modifying the original class, but rather by extending it and adding new behaviours, or by adding plugin capabilities. I ve heard it said that the OCP is wrong, unworkable, impractical, and not for real programmers with real work to do. The rise of plugin architectures makes it plain that these views are utter nonsense. On the contrary, a strong plugin architecture is likely to be the most important aspect of future software systems. [Uncle Bob, The Open Closed Principle] 9

CSC207H: Software Design Open/Closed Principle ( Open for extension, closed for modification. ) An example, using inheritance: area calculates the area of all Rectangles in the input. What if we need to add more shapes? 10

Open/Closed Principle (simplified) With this design, we can add any number of shapes (open for extension) and we don't need to re-write the AreaCalculator class (closed for modification). 11

Liskov Substitution Principle ( The child class must uphold the spirit of the parent class ) Replacing a parent with the child should not break your code. If S is a subtype of T, then objects of type S may be substituted for objects of type T, without altering any of the desired properties of the program. S is a subtype of T? In Java, S is a child class of T, or S implements interface T. For example, if C is a child class of P, then we should be able to substitute C for P in our code without breaking it. 12

A classic example of breaking this principle: CSC207H: Software Design Liskov Substitution Principle ( The child class must uphold the spirit of the parent class ) In OO programming and design, unlike in math, it is not the case that a Square is a Rectangle! This is because a Rectangle has more behaviours than a Square, not less. The LSP is related to the Open/ Closed principle: the subclasses should only extend (add behaviours), not modify or remove them. 13

Interface Segregation Principle ( Better to have many small interfaces than one big one. ) Here, interface means the public methods in a class. (In Java, these are often specified using a Java interface.) Context: a class that provides a service for other client programmers usually requires that the clients write code that has a particular set of features. The service provider says your code needs to have this interface. No client should be forced to implement irrelevant methods of an interface. Better to have lots of small, specific interfaces than fewer larger ones: easier to extend and modify the design. (Aside: The interface keyword is harmful. [Uncle Bob, 'Interface' Considered Harmful]) 14

Dependency inversion principle ( 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 When building a complex system, programmers are often tempted to define low-level classes first and then build higher-level classes that use the low-level classes directly. But this approach is not flexible! What if we need to replace a low-level class? The logic in the high-level class will need to be replaced an indication of high coupling. To avoid such problems, we introduce an abstraction layer between low-level classes and high-level classes. 15

Goal: You want to decouple your system so that you can change individual pieces without having to change anything more than the individual piece. CSC207H: Software Design Dependency inversion principle ( High-level modules should not depend on low-level modules; both should depend on abstractions ) Two aspects to the dependency inversion principle: High-level modules should not depend on lowlevel modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. 16

Dependency inversion principle ( High-level modules should not depend on low-level modules; both should depend on abstractions ) Example: from Dependency Inversion Principle on OODesign You have a large system, and part of it has Managers manage Workers. Let s say that the company is restructuring and introducing new kinds of workers, and wants the code updated to reflect this. Your code current has a Manager class and a Worker class, and the Manager class has several methods that have Worker parameters. Now there s a new kind of worker called SuperWorker, and their behaviour and features are separate from regular Workers. Oh dear... 17

To make Manager work with SuperWorker, we would need to rewrite the code in Manager. CSC207H: Software Design Dependency inversion principle ( High-level modules should not depend on low-level modules; both should depend on abstractions ) Solution: create an IWorker interface and have Manager use it. 18

In this design, Manager does not know anything about Worker, nor about SuperWorker. It can work with any IWorker, the code in Manager does not need rewriting. CSC207H: Software Design Dependency inversion principle ( High-level modules should not depend on low-level modules; both should depend on abstractions ) 19