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

Similar documents
Software Engineering

Inheritance and object compatibility

Principles of Object-Oriented Design

Design Principles: Part 2

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

Design Principles: Part 2

Object Oriented Software Design - I

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

CSC207H: Software Design SOLID. CSC207 Winter 2018

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

OO Class Design Principles

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

Intro to: Design Principles

Liskov Substitution Principle

COURSE 2 DESIGN PATTERNS

CS 520 Theory and Practice of Software Engineering Fall 2017

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

SOLID: Principles of OOD

Lecture: Modular Design

Software Engineering Design & Construction

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

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CSC Advanced Object Oriented Programming, Spring Specification

Object-Oriented Concepts and Design Principles

CS 520 Theory and Practice of Software Engineering Fall 2018

Lecture 11 Subtypes and Subclasses

CSE 331 Software Design and Implementation. Lecture 12 Subtypes and Subclasses

Object-Oriented Design

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

CSE 331 Software Design & Implementation

Type Hierarchy. Lecture 6: OOP, autumn 2003

Object-Oriented Design II

Subtypes and Subclasses

6.170 Recitation #5: Subtypes and Inheritance

Summary of the course lectures

CS 320 Introduction to Software Engineering Spring March 06, 2017

Object-Oriented Design I - SOLID

Object-Oriented Design

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

Component-Level Design

OO Design Principles

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

CSE 331 Software Design & Implementation

Single Responsibility Principle

Josh Bloch Charlie Garrod Darya Melicher

Chapter 10 Classes Continued. Fundamentals of Java

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

17.11 Bean Rules persistent

Software Engineering I (02161)

Introduction to Object-Oriented Programming

Inheritance and Polymorphism

Originality is Overrated: OO Design Principles

Introduction to Object-Oriented Programming

Class-level reuse with inheritance Behavioral subtyping

Conception Orientée Objets. Programmation SOLID

Programming in the large

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

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

Principles of OO Design

Open Closed Principle (OCP)

Test Code Patterns. How to design your test code

CS 320 Introduction to Software Engineering Spring March

OO Technology: Properties and Limitations for Component-Based Design

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

Subtypes. CSE 331 University of Washington. Michael Ernst

GRASP Design Patterns A.A. 2018/2019

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

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Bruno Bossola SOLID Design Principles

Maintainable Software. Software Engineering Andreas Zeller, Saarland University

MIT EECS Michael Ernst Saman Amarasinghe

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

Java Object Oriented Design. CSC207 Fall 2014

Single Responsibility Principle (SRP)

Advanced JML Erik Poll Radboud University Nijmegen

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

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

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

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

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

Object-Oriented Programming for High-Integrity Systems: Pitfalls and How to Avoid Them

Highlights of Previous Lecture

Tecniche di Progettazione: Design Patterns

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

Testing Object-Oriented Software. 22 November 2017

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

2IP15 Programming Methods

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

The Liskov Substitution Principle

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

PRINCIPLES OF SOFTWARE DESIGN

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

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

C++ Inheritance and Encapsulation

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Object-oriented design principles

Software Engineering Fall 2014

Principles of Software Construction: Objects, Design, and Concurrency (Part 1: Designing Classes) Design for Reuse (class level)

Final Exam. Final Exam Review. Ch 1: Introduction: Object-oriented analysis, design, implementation. Exam Format

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

Transcription:

CSC40232: SOFTWARE ENGINEERING Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017 Department of Computer Science and Engineering http://www.kirkk.com/modularity/2009/12/solid-principles-of-class-design/ http://blog.gauffin.org/2012/05/11/solid-principles-with-real-world-examples/ http://code.tutsplus.com/series/the-solid-principles--cms-634 1

The SOLID Principles Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle 3 4 2

Single Responsibility A class should have only one reason to change. What about this class? 5 Single Responsibility Separate out the responsibilities 6 3

Single Responsibility https://www.youtube.com/watch?v=nyxachz1row&list=pl4ce9f710017ea77a&index=1 7 Open Closed Principle If you modify a class you may break the API/Contract such that classes that depend on it may fail. It is better to reuse the class to add new features through inheritance or aggregration. This way the base class is untouched. 8 4

Open Closed Principle 9 Step 1. Create an interface Open Closed Principle Step 2. Get the correct concrete handler and delegate the processing to it. 10 5

Open Closed Principle Open Closed Principle Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Violates OCP because user is tied directly to Logic class. 11 Open Closed Principle Open Closed Principle The most common solution.. 12 6

Single Responsibility https://www.youtube.com/watch?v=epvfsqejq68&list=pl4ce9f710017ea77a&index=2 13 Liskov Substitution Principle 14 7

Liskov Substitution Principle Should inheritance be used between the square and rectangle classes? Every square is-a rectangle. Good opportunities for reuse. (int w, int h) 15 However there are serious problems in the design because the square inherits unwanted methods such as setsize(int w, int h). 8

Patching the problem We can easily override the unwanted behavior: Public void setsize(int w, int h) { width = h; height = h; } Unfortunately the code now behaves in an unexpected way: public void stretch(rectangle r, int dx, Graphics g){ r.erase(g); r.setsize(r.getwidth()+dx, r.getheight()); r.draw(g); } which is NOT an elegant solution. Code should behave in ways expected by the programmer. 17 Design principle of LEAST ASTONISHMENT. A designer of a class or interface MUST specify the semantics of each method. All subclasses must conform to this expected behavior. 18 9

Liskov Substitution Principle Inheritance should ensure that any property proved about super-type objects also holds for subtype objects. Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. 19 Liskov Substitution Principle Preconditions cannot be strengthened in a subclass - you cannot have a subclass that has stronger preconditions than its superclass. Postconditions cannot be weakened in a subclass - you cannot have a subclass that has weaker postconditions than its superclass. No new exceptions should be thrown by methods of the subclass, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the superclass. 20 10

Liskov Substitution Principle Subtype methods should look like corresponding supertype method Subtype methods should extend behavior of supertype method in a consistent manner Subtype methods should not change or eliminate supertype method properties Adhering to this principle yields well-behaved subclasses that support parametric polymorphism 21 A possible solution Gets around the problem of square and rectangle s behavior not matching (i.e. public methods of Rectangle were not all appropriate for square), by creating a more sophisticated hierarchy. 11

Another possible solution Single Responsibility https://www.youtube.com/watch?v=lkqwvvxndkw&index=3&list=pl4ce9f710017ea77a 24 12

Interface Segregation Principle There is nothing that says that there should be a one-to-one mapping between classes and interfaces. It s in fact much better if you can create several smaller interfaces instead 25 Interface Segregation Principle Any interface we define should be cohesive. There must be some kind of interface which a client can rely on. Its purpose is to communicate to the client code how the module should be used. So what should go into the interface? In this example we expose all functionalities that we d like to offer. 26 13

Interface Segregation Principle We could break the interface into pieces specialized to each implementation. Various cars can incorporate (aggregate) different objects. The car uses the implementations but depends on the interfaces. 27 Interface Segregation Principle A subtle difference 14

Single Responsibility https://www.youtube.com/watch?v=dmkvjyihsaq&index=4&list=pl4ce9f710017ea77a 29 Dependency Inversion Principle Let the caller create the dependencies instead of letting the class itself create the dependencies. Hence inverting the dependency control (from letting the class control them to letting the caller control them). 30 15

Dependency Inversion Principle High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. KeyboardReader Higher level classes e.g. Copy class Higher level classes e.g. Copy class PrinterWriter ireader KeyboardReader iwriter PrinterWriter 31 What about this? class Worker { public void work() { //...working } } class Manager { Worker worker; public void setworker(worker w) { worker = w; } public void manage() { worker.work(); } } class SuperWorker { public void work() { //... working much more } } 32 16

Fixed to comply with DIP 33 Single Responsibility Deferring this discussion until next week. https://www.youtube.com/watch?v=d4ubbvnrexw&list=pl4ce9f710017ea77a&index=5 34 17

What goes together? Single Responsibility Open/Closed Liskov Substitution Interface Segregation Dependency Inversion Only does what it needs to do Highly cohesive classes Easy to maintain and evolve No surprises in Subtyping and Inheritance Everyone should depend on abstractions 35 18