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

Similar documents
CS 320 Introduction to Software Engineering Spring March 06, 2017

CS 520 Theory and Practice of Software Engineering Fall 2017

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

Software Engineering

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS 520 Theory and Practice of Software Engineering Fall 2018

Liskov Substitution Principle

Data Structures (INE2011)

Clean Classes. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 11

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

Inheritance and Polymorphism

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Single Responsibility Principle

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

ITI Introduction to Computing II

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

Open Closed Principle (OCP)

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

ITI Introduction to Computing II

Inheritance, and Polymorphism.

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS 320 Introduction to Software Engineering Spring March

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Programming in C# Inheritance and 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

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 11 java track: lecture 3

CSC207H: Software Design SOLID. CSC207 Winter 2018

Inheritance and Interfaces

Intro to: Design Principles

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Data Structures and Other Objects Using C++

OO Class Design Principles

Object Design Guidelines

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Reusing Classes. Hendrik Speleers

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

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Java Object Oriented Design. CSC207 Fall 2014

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Topic 7: Algebraic Data Types

Object Oriented Software Design - I

Reviewing OO Concepts

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

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Last class. -More on polymorphism -super -Introduction to interfaces

solid-principles #solidprinciples

JAVA OBJECT-ORIENTED PROGRAMMING

Bridge Pattern. Used to decouple an abstraction from its implementation so that the two can vary independently

Polymorphism CSCI 201 Principles of Software Development

Lecture: Modular Design

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

CS-202 Introduction to Object Oriented Programming

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

CMSC 132: Object-Oriented Programming II

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

Making New instances of Classes

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

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

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

COURSE 2 DESIGN PATTERNS

Lexical and Syntax Analysis. Abstract Syntax

Object-Oriented Programming Concepts

Reviewing OO Concepts

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

OBJECT ORIENTED PROGRAMMING

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

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

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Conception Orientée Objets. Programmation SOLID

SOLID: Principles of OOD

Object Oriented Programming 2015/16. Final Exam June 28, 2016

VII. POLYMORPHISM AND VIRTUAL FUNCTIONS

ECE 3574: Dynamic Polymorphism using Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

INHERITANCE: CONSTRUCTORS,

Chapter 1: Object-Oriented Programming Using C++

Introduction to Object-Oriented Programming

Composite Pattern - Shapes Example - Java Sourcecode

Java interface Lecture 15

Advantages of Object Oriented Programming :- Features of Object Oriented Programming :- Advantages Of Object Oriented Programming :

Object-oriented Programming. Object-oriented Programming

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Inheritance Motivation

Single Responsibility Principle (SRP)

ITI Introduction to Computing II

CS Week 13. Jim Williams, PhD

Chapter 10 Classes Continued. Fundamentals of Java

ITI Introduction to Computing II

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

CS2 Assignment A1S The Simple Shapes Package

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Bruno Bossola SOLID Design Principles

Transcription:

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

Object-Oriented programming Encapsulation Abstraction Inheritance Polymorphism Decoupling

Inheritance Parent (Base) class Child (Derived) Class

Inheritance class ParentClass public int Property1 get; set; public string Property2 get; set; public void DoSomething(int param1) // Code here class ChildClass : ParentClass var child = new ChildClass(); child.property1 = 1; child.property2 = "Foo"; child.dosomething(99);

Interface Inheritance interface MyInterface int Property1 get; set; string Property2 get; set; void DoSomething(int param1); class ChildClass : MyInterface public int Property1 get; set; public string Property2 get; set; public void DoSomething(int param1) // Code goes here var child = new ChildClass(); child.property1 = 1; child.property2 = "Foo"; child.dosomething(99);

S.O.L.I.D. Single Responsibility Principle Open-Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle

Single Responsibility Principle A class should have one and only one reasons to change.

Single Responsibility Principle public interface IEmployee string FirstName get; set; string LastName get; set; float HourlyRate get; set; float CalculatePay(float hoursworked); string ReportHours(); void Save();

Single Responsibility Principle public interface IEmployee string FirstName get; set; string LastName get; set; float HourlyRate get; set; public interface IDataAccess void Save(); public interface IReporting string ReportHours( Employee employee); public interface IPayroll float CalculatePay( Employee employee, float hoursworked);

Single Responsibility Principle public class Employee : IEmployee public string FirstName get; set; public string LastName get; set; public float HourlyRate get; set; public class DataAccessSvc : IDataAccess public void Save() public class ReportingSvc : IReporting public string ReportHours( Employee employee) public class PayrollSvc : IPayroll public float CalculatePay( Employee employee, float hoursworked)

Open-Closed Principle Objects or entities should be open for extension, but closed for modification Add new behavior; don't change existing behavior

Open-Closed Principle Strategies Parameters Inheritance Composition / Strategy Pattern

Open-Closed Principle public class Drawing public void DrawAllShapes(object[] shapes) foreach (var shape in shapes) if (shape is Circle) var circle = (Circle)shape; DrawCircle(circle); else if (shape is Square) var square = (Square)shape; DrawSquare(square);

Open-Closed Principle public interface IShape void Draw(); public class Circle : IShape public int Radius get; set; public int CenterX get; set; public int CenterY get; set; public class Square : IShape public int Side get; set; public int Top get; set; public int Left get; set; public void Draw() var center = new Tuple<int, int> (this.centerx, this.centery); var radius = this.radius; public void Draw() var topleft = new Tuple<int, int> (this.top, this.left); var side = this.side;

Liskov Substitution Principle If something is true for the base class, it must be true for every derived class

Liskov Substitution Principle Mammal Inheritance Cat is a Mammal Cat inherits from Mammal Cat

Liskov Substitution Principle Rectangle Inheritance Square is a Rectangle Square inherits from Rectangle Square

Liskov Substitution Principle public class Rectangle public virtual int Height get; set; public virtual int Width get; set; public class Square : Rectangle private int _width; private int _height; public override int Width get return _width; set _width = value; _height = value; Rectangle r = new Square() Height = 10, Width = 5 ; var area = r.width * r.height; public override int Height get return _height; set _height = value; _width = value;

Interface Segregation Principle A client should never be forced to implement and interface that it doesn't use

Interface Segregation Principle interface IMachine void Print(List<Document> docs); void Staple(List<Document> docs); void Fax(List<Document> docs); void Scan(List<Document> docs); void PhotoCopy(List<Document> docs); class Machine : IMachine public void Print(List<Document> docs) // Print the items. public void Staple(List<Document> docs) // Staple the items. public void Fax(List<Document> docs) // Fax the items. public void Scan(List<Document> docs) // Scan the items. public void PhotoCopy(List<Document> docs) // Photocopy the items. From: https://www.codeproject.com/tips/766045/interface-segregation-principle-isp-of-solid-in-cs

Interface Segregation Principle interface IMachine void Print(List<Document> docs); void Staple(List<Document> docs); void Fax(List<Document> docs); void Scan(List<Document> docs); void PhotoCopy(List<Document> docs); class Printer : IMachine public void Print(List<Document> docs) // Print the items. public void Staple(List<Document> docs) throw new NotImplementedException(); public void Fax(List<Document> docs) throw new NotImplementedException(); public void Scan(List<Document> docs) throw new NotImplementedException();. public void PhotoCopy(List<Document> docs) throw new NotImplementedException();

Interface Segregation Principle interface IPrinter void Print(List<Document> docs); interface IScanner void Scan(List<Document> docs); public class Printer : IPrinter public void Print(List<Document> docs) // Print document interface IStapler void Staple(List<Document> docs); interface IFax void Fax(List<Document> docs); interface IPhotocopier void PhotoCopy(List<Document> docs);

Interface Segregation Principle interface IPrinter void Print(List<Document> docs); interface IScanner void Scan(List<Document> docs); interface IStapler void Staple(List<Document> docs); interface IFax void Fax(List<Document> docs); public class PrinterScannerCopier : IPrinter, IScanner, IPhotocopier public void PhotoCopy(List<Document> docs) // Photocopy documents; public void Print(List<Document> docs) // Print documents public void Scan(List<Document> docs) // Scan documents interface IPhotocopier void PhotoCopy(List<Document> docs);

Dependency Inversion Principle Entities must depend on abstractions, not on concrete implementations

Dependency Inversion Principle var logger = new EventLogLogger(); logger.logevent(message, category); var logger = new EventLogLogger(); logger.logevent(message, category);

Dependency Inversion Principle public class LoggingService private ILogger _logger = null; public LoggingService(ILogger logger) _logger = logger; public void LogEvent(string message, string category) _logger.logevent(message, category); var filelogger = new FileLogger(); var logsvc = new After.LoggingService(fileLogger); logsvc.logevent("this is an event", "Event");

References http://butunclebob.com/articles.unclebob.principlesofood