Slide 1

Slide 1 text

CSE 564 Software Design Lecture: The SOLID Design Principles Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 2 Announcements • Monday, June 20

Slide 3

Slide 3 text

Previously … Programming

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 4 Global Ideas • Create code Elegant and Efficient –Bjarne Stroustrup • Create code Simple and Direct –Grady Booch • Create code looks like written by someone who cares –Michael Feathers

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 5 Clean Code Principles • Readability Use Meaningful Names for Classes, Methods, and Variables Follow coding guidelines: e.g., read the Java Style Reference • KISS (Keep It Simple by K. Johnson) for your reader Small functions that do one thing • DRY (Do not Repeat Yourself): Avoid Code Bloat

Slide 6

Slide 6 text

SOLID Principles Object-Oriented Design

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 7 Key idea • Just as the source code a design should be clean and that include Keep it Simple A design that is more than what we need smells So, abstract classes, interfaces, design patterns, and other infrastructure elements that do not solve a problem and are there to over-design the system are as bad as their absence when needed. Eliminate design smells is the goal of design principles.

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 8 Key Idea Design principles are not a perfume to be liberally scattered all over the system –Robert Martin

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 9 Design Principles There are five key design principles to consider in Object-Oriented: • Single Responsibility Principle (SRP) • Open-Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP)

Slide 10

Slide 10 text

Single Responsibility Principle SRP

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 11 Definition A class should have only one responsibility SRP is all about understanding when and how to apply decoupling. Why? Because each responsibility implies a possibility of a change.

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 12 SRP Example • Imagine that you need a piece of software to read data from diverse sensor devices (a heart rate monitor, a brain-computer interface, a skin conductance sensor, etc.) • And you need to store that information for future use also. • For some sensors, we need to gather data directly from a serial port. • For others, we use a WebSockets (third-party APIs help us get data from the physical device). • To store data, we want to be able to store data in a local file (text file) or in a database

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 13 SRP Example

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 14 SRP Example

Slide 15

Slide 15 text

Open-Closed Principle OCP

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 16 Definition Software entities (functions, classes, modules, etc.) should be open for extension but closed for modification. OCP is all about achieving changes adding new code, not changing the old code that already works. Closure cannot be complete. There will always be some change against which the entity is not closed. Thus, the closure must be strategic. As a developer, make educated guesses about the likely kinds of changes that the application could suffer over time. OCP means that we do not want to modify the class, i.e., write code into a class. Once you create a class and put that class in a production environment, you do not want to touch that class. OCP can be satisfied with a simple and effective heuristic: inheritance

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 17 OCP Example • Imagine you are asked to create a program to draw geometric shapes on screen. • We want to draw circles and draw squares; maybe later, we would ask for draw triangles, etc.

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 18 OCP Example

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 19 OCP Example

Slide 20

Slide 20 text

Liskov Substitution Principle LSP

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 21 Definition Subtypes must be substitutable for all their base types. i.e., a child should always be better than its parent. And, “better” means more behaviors, not less. That principle is the answer proposed by Barbara Liskov (1988) to the questions: • What are the characteristics of the best inheritance hierarchies? • What are the traps that could create hierarchies that jeopardize the OCP?

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 22 LSP Example • Imagine you already have a class Circle, and you are asked to create a class Cylinder. • Or maybe you have a class Rectangle, and you are asked to create a class Square (a square is a rectangle with the same width and height). • Or you have a class LinkedList, and you are asked to create a class PersistentLinkedList (one that writes out its elements to a stream and can read them back later). If you are tempted to use inheritance from Circle to Cylinder, or from Rectangle to Square, or from LinkedList to PersistentLinkedList, i.e., create a parent-child relationship for any of these cases, you will have problems.

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 23 LSP Example • The class Cylinder would eliminate the method calculateArea() in Circle since calculating an area does not make sense. It is impossible to use our Cylinder object to replace a Circle object. • The class Square will make the methods setWidth() and setHeight() to modify both width and height attributes (they are equal in a square, right?). Therefore, it will be impossible to use a Square object to replace a Rectangle object. • The class PersistentLinkedList needs persistent (serializable) objects while LinkedList does not. Moreover, probably, PersistentLinkedList would need to throw some exceptions.

Slide 24

Slide 24 text

Interface Segregation Principle ISP

Slide 25

Slide 25 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 25 Definition • Clients should not be forced to depend on methods that they do not use. • ISP deals with the disadvantage of “fat” interfaces (or abstract classes). • ISP recommends to broke up interfaces with a lot of methods into several interfaces.

Slide 26

Slide 26 text

Dependency Inversion Principle DIP

Slide 27

Slide 27 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 27 Definition • High-level modules should not depend on low-level modules. Both should depend on abstractions. • Traditional procedural programming creates software structures in which high-level modules depend on low-level modules. • The dependency structure of a well-designed object-oriented program is “inverted” with respect to the dependency structure that generally results from traditional procedural methods. • DIP is what makes software fulfill the object-oriented paradigm.

Slide 28

Slide 28 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 28 Definition • Hollywood Principle: “do not call us, we will call you.” • Review DIP whenever one class sends a message to another. DIP is about calling methods. • When doing that, depend on abstractions (use abstract classes or interfaces).

Slide 29

Slide 29 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 29 DIP Example

Slide 30

Slide 30 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 30 DIP Example What about ISP?

Slide 31

Slide 31 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 31 Reference Read: • Design Patterns: Abstraction and Reuse of Object- Oriented Design Gamma, Helm, Johnson, Vlissides • European Conference on Object-Oriented Programming October 1993 • This will become:

Slide 32

Slide 32 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 32 Assignment 02

Slide 33

Slide 33 text

CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2022 Copyright. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.