0% found this document useful (0 votes)
42 views36 pages

Vdocuments - Pub - Introduction To Solid Principles

This document introduces the SOLID principles of object-oriented design: 1. Single Responsibility Principle (SRP): Classes should have single, well-defined responsibilities to minimize dependencies. 2. Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification to avoid breaking existing code. 3. Liskov Substitution Principle (LSP): Child classes must be substitutable for their parent classes without changing desirable properties of a program. 4. Interface Segregation Principle (ISP): Clients shouldn't depend on interfaces they don't use to reduce dependencies. 5. Dependency Inversion Principle (DIP): High-level

Uploaded by

degey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views36 pages

Vdocuments - Pub - Introduction To Solid Principles

This document introduces the SOLID principles of object-oriented design: 1. Single Responsibility Principle (SRP): Classes should have single, well-defined responsibilities to minimize dependencies. 2. Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification to avoid breaking existing code. 3. Liskov Substitution Principle (LSP): Child classes must be substitutable for their parent classes without changing desirable properties of a program. 4. Interface Segregation Principle (ISP): Clients shouldn't depend on interfaces they don't use to reduce dependencies. 5. Dependency Inversion Principle (DIP): High-level

Uploaded by

degey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to

SOLID Principles
Background
• Single Responsibility Principle

• Open/Closed Principle

• Liskov Substitution Principle

• Interface Segregation Principle

• Dependency Inversion Principle


S: Single Responsibility
Principle (SRP)
“Every class should have a single responsibility, and that
responsibility should be entirely encapsulated by that class”
The Customer class has 2 reasons to change:

1. When we need to modify the way we save Customer data

2. When we want to revise the way we log our Exceptions


SRP - Tips
Difficulty in naming classes can be a sign that you are trying to do
too much, and that this class is taking on too much responsibility

Do One Thing: methods that only do one thing are usually very
short: 3-5 lines

Multiple unit tests for the same method should be a warning sign
that you are doing too much
O: Open/Closed
Principle (OCP)
“Software entities (classes, modules, functions, etc.) should be
open for extension, but closed for modification.”
O: Open/Closed
Principle (OCP)
Open for extension – you can extend an object’s behaviour

Closed for modification – without the need to modify any


existing code
L: Liskov Substitution
Principle (LSP)
“If S is a subtype of T, then objects of type T may be replaced
with objects of type S without altering any of the desirable
properties of that program”
LSP - Tips
Favour Interfaces over inheritance (Google this…you’ll find loads!)

Use inheritance sparingly.

Keep inheritance hierarchies from getting to big – aim for no


more than two or three deep.
I: Interface Segregation
Principle (ISP)
“No client should be forced to depend on methods it does not
use.”
D: Dependency
Inversion Principle (DIP)
“A. High-level modules should not depend on low-level
modules. Both should depend on abstractions.

B. Abstractions should not depend on details. Details should


depend on abstractions.”
IOC Containers
Castle Windsor
Unity
Questions
Follow Us
Dave Cook
http://tech.comparethemarket.com/author/cookie/

@dcookie_1981

@thectmers

You might also like