0% found this document useful (0 votes)
6 views

OOP Lecture#03

Uploaded by

muqaddas zahra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP Lecture#03

Uploaded by

muqaddas zahra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

Object Oriented Analysis and Design

OO Design (SOLID) Principles

Lecture 03

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 1 / 65
Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 2 / 65
Introduction

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 3 / 65
Introduction

SOLID Principles

SOLID is a “mnemonic acronym” for five design principles intended


to make software designs more understandable, flexible, and
maintainable
SOLID principles are the foundation on which we can build clean code

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 4 / 65
Introduction

Without SOLID Principles

Code Fragility
Fragility is the tendency of the software to break in many places every
time it is changed
For example, In EMS, a change in “Payment and checkouts” module
introduces the bugs in the other sub-system “Event booking”

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 5 / 65
Introduction

Without SOLID Principles


Code Fragility
Fragility is the tendency of the software to break in many places every
time it is changed
For example, In EMS, a change in “Payment and checkouts” module
introduces the bugs in the other sub-system “Event booking”

Figure: Modules of Event Management System

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 5 / 65
Introduction

Without SOLID Principles

Code Rigidity
Rigidity is the tendency for software to be difficult to change, even in
simple ways. Every change causes a cascade of subsequent changes in
dependent modules.
For example, In EMS, a change in “Event booking” module also
initiates a modification in the other sub-system “Event Monitoring”

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 6 / 65
Introduction

Without SOLID Principles


Code Rigidity
Rigidity is the tendency for software to be difficult to change, even in
simple ways. Every change causes a cascade of subsequent changes in
dependent modules.
For example, In EMS, a change in “Event booking” module also
initiates a modification in the other sub-system “Event Monitoring”

Figure: Modules of Event Management System

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 6 / 65
Introduction

Without SOLID Principles

Technical debt
The cost of prioritizing fast delivery over code quality for long periods
of time
Fragility and Rigidity are indicators for high technical debt

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 7 / 65
Introduction

Without SOLID Principles

Technical debt
No matter how good the team is, technical debt will accumulate over
time
Left uncontrolled, it will kill your project
The key is to keep it under control (using PDD - Pain Driven
Development)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 8 / 65
Introduction

Without SOLID Principles

Pain Driven Development


Avoid premature optimization
Write code and then redesign using principles

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 9 / 65
Introduction

What is SOLID?

The “SOLID” principles are:


Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 10 / 65
Single Responsibility Principle (SRP)

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 11 / 65
Single Responsibility Principle (SRP)

Single Responsibility Principle (SRP)

Each software module should have one and only one reason to change

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 12 / 65
Single Responsibility Principle (SRP)

Single Responsibility Principle (SRP)

Identify the reasons to change that components have and reduce


them to single units

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 13 / 65
Single Responsibility Principle (SRP)

Example of Responsibilities

Business logic
User Interface
Logging
Persistence

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 14 / 65
Single Responsibility Principle (SRP)

Why SRP?

It makes code easier to understand, fix, and maintain


Classes are less coupled and more resilient to change
More testable design

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 15 / 65
Single Responsibility Principle (SRP)

Why SRP?

Separation of Concerns
Programs should be separated into distinct sections, each addressing
a separate concern, or set of information that affects the program

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 16 / 65
Single Responsibility Principle (SRP)

Examples of Reasons to change

If statements

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 17 / 65
Single Responsibility Principle (SRP)

Examples of Reasons to change

Switch statement

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 18 / 65
Single Responsibility Principle (SRP)

Examples of Reasons to change

Monster Method

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 19 / 65
Single Responsibility Principle (SRP)

Examples of Reasons to change

God Class

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 20 / 65
Single Responsibility Principle (SRP)

Example of SRP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 21 / 65
Single Responsibility Principle (SRP)

Example of SRP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 22 / 65
Single Responsibility Principle (SRP)

Example of SRP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 22 / 65
Single Responsibility Principle (SRP)

Symptoms of not using SRP

Code is difficult to read


High coupling
Side effects
Testing is difficult

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 23 / 65
Single Responsibility Principle (SRP)

Summing up!

We want to design components that are self-contained, independent,


and with a single, well-defined purpose

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 24 / 65
Open Closed Principle (OCP)

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 25 / 65
Open Closed Principle (OCP)

Open Closed Principle (OCP)

Software entities (Classes, functions, and modules) should be closed


for modification, but open for extension
It should be possible to change the behavior of a software entity
without modifying it

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 26 / 65
Open Closed Principle (OCP)

Open Closed Principle (OCP)

Closed for modification


Each new feature should not modify the existing code
Open for extension
A module should be extendable to behave in a new manner

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 27 / 65
Open Closed Principle (OCP)

Open Closed Principle (OCP)

Closed for modification


Each new feature should not modify the existing code
Open for extension
A module should be extendable to behave in a new manner

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 27 / 65
Open Closed Principle (OCP)

Why OCP?

New features can be added easily and with minimal cost


Minimizes the risk of regression bugs
Enforces decoupling by isolating changes in specific components,
works along with the SRP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 28 / 65
Open Closed Principle (OCP)

Why OCP?

New features can be added easily and with minimal cost


Minimizes the risk of regression bugs
Enforces decoupling by isolating changes in specific components,
works along with the SRP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 28 / 65
Open Closed Principle (OCP)

OCP Example

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

OCP Example

First approach – Use Inheritance

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

OCP Example

Second approach – Use Strategy Pattern

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

OCP Example

Second approach – Use Strategy Pattern

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

OCP Example
Second approach – Use Strategy Pattern

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

OCP Example

Second approach – Use Strategy Pattern

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 29 / 65
Open Closed Principle (OCP)

Progressively Applying OCP

Make changes inline


Use Inheritance
Use Interfaces and Design Patterns

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 30 / 65
Open Closed Principle (OCP)

Summing up!

OCP is all about changes


Following this principle will lead to elegant designs that are easy and
painless to extend

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 31 / 65
Liskov Substitution Principle (LSP)

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 32 / 65
Liskov Substitution Principle (LSP)

Liskov Substitution Principle (LSP)

If S is a subtype of T, then objects of type T in a program may be


replaced with objects of type S without modifying the functionality of
the program
Any object of a type must be substitutable by objects of a derived
typed without altering the correctness of that program

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 33 / 65
Liskov Substitution Principle (LSP)

Liskov Substitution Principle (LSP)

If S is a subtype of T, then objects of type T in a program may be


replaced with objects of type S without modifying the functionality of
the program
Any object of a type must be substitutable by objects of a derived
typed without altering the correctness of that program

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 33 / 65
Liskov Substitution Principle (LSP)

Relationships

“Is a”
Square is a kind of rectangle
An ostrich is a bird
“Is substitutable by?”
Is the class rectangle fully substitutable by the class square?

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 34 / 65
Liskov Substitution Principle (LSP)

Relationships

“Is a”
Square is a kind of rectangle
An ostrich is a bird
“Is substitutable by?”
Is the class rectangle fully substitutable by the class square?

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 34 / 65
Liskov Substitution Principle (LSP)

Relationships

“Is a”
Square is a kind of rectangle
An ostrich is a bird
“Is substitutable by?”
Is the class rectangle fully substitutable by the class square?
Incorrect relationships between types cause unexpected bugs or side
effects.

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 34 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

Empty Methods

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle


Harden Preconditions

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

Harden Preconditions

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

Harden Preconditions

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

Partially Implemented Interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle


Partially Implemented Interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Violations of the Liskov Substitution Principle

Type Checking

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 35 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP

Eliminate incorrect relationship between objects


Use “Tell, don’t ask!” to eliminate type checking and casting

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 36 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP

Empty Methods

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 37 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP

Empty Methods – Fix

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 37 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP


Partially implemented interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 38 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP


Partially implemented interfaces – Fix

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 38 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP

Type checking

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 39 / 65
Liskov Substitution Principle (LSP)

Fixing code to follow LSP

Type checking – Fix (Tell, don’t ask!)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 39 / 65
Liskov Substitution Principle (LSP)

Applying LSP

Make sure that a derived type can substitute its base type completely
Keep base classes small and focused
Keep interfaces lean

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 40 / 65
Liskov Substitution Principle (LSP)

Summing up!

Real life categories do not always map to OOP relationships


If it looks like a duck, quacks like a duck, but needs batteries you
probably have the wrong abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 41 / 65
Liskov Substitution Principle (LSP)

Summing up!

Real life categories do not always map to OOP relationships


If it looks like a duck, quacks like a duck, but needs batteries you
probably have the wrong abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 41 / 65
Interface Segregation Principle (ISP)

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 42 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP)

Clients should not be forced to depend on methods that they do not


use
The interface word here doesn’t mean a programming language
interface

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 43 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP)

Clients should not be forced to depend on methods that they do not


use
The interface word here doesn’t mean a programming language
interface

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 43 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP)

ISP enforces other principles


LSP - By keeping interfaces small, the classes that implement them
have a higher chance to fully substitute the interface
SRP - Classes that implement small interfaces are more focused and
tend to have a single purpose

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 44 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP)

ISP enforces other principles


LSP - By keeping interfaces small, the classes that implement them
have a higher chance to fully substitute the interface
SRP - Classes that implement small interfaces are more focused and
tend to have a single purpose

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 44 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP) – Benefits

Lean interfaces minimize dependencies on unused members and


reduce code coupling
Code becomes more cohesive and focused
It reinforces the use of the SRP and LSP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 45 / 65
Interface Segregation Principle (ISP)

Interface Segregation Principle (ISP) – Benefits

Lean interfaces minimize dependencies on unused members and


reduce code coupling
Code becomes more cohesive and focused
It reinforces the use of the SRP and LSP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 45 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Interfaces with many methods

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Throwing exceptions

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Interfaces with low cohesion

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Increased coupling

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Not just about interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Identifying “Fat” Interfaces

Empty methods

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 46 / 65
Interface Segregation Principle (ISP)

Symptoms of Interface Pollution

Interfaces with lots of methods


Interfaces with low cohesion
Client provides empty implementation
Client throws exception instead of implementing method
Client forces implementation and becomes highly coupled

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 47 / 65
Interface Segregation Principle (ISP)

Refactoring the code to follow ISP

Your own code


Breaking interfaces is pretty easy and safe due to the possibility to
implement as many interfaces as we want
External legacy code
You can’t control the interfaces in the external code, so you use design
patterns like “Adapter”

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 48 / 65
Interface Segregation Principle (ISP)

Fixing “Fat” Interfaces

From a Fat Interface to lean interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 49 / 65
Interface Segregation Principle (ISP)

Fixing “Fat” Interfaces


From a Fat Interface to three lean interfaces

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 49 / 65
Interface Segregation Principle (ISP)

Fixing “Fat” Interfaces

Ensuring Cohesion

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 49 / 65
Interface Segregation Principle (ISP)

Fixing “Fat” Interfaces

Increased reuse

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 49 / 65
Interface Segregation Principle (ISP)

Summing up!

Fat interfaces lead to inadvertent couplings between clients that


ought otherwise to be isolated

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 50 / 65
Dependency Inversion Principle (DIP)

Agenda

1 Introduction

2 Single Responsibility Principle (SRP)

3 Open Closed Principle (OCP)

4 Liskov Substitution Principle (LSP)

5 Interface Segregation Principle (ISP)

6 Dependency Inversion Principle (DIP)


Dependency Inversion
Dependency Injection (DI)
Inversion of Control (IoC)

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 51 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Dependency Inversion Principle (DIP)

High level modules should not depend on low level modules; both
should depend on abstractions
Abstractions should not depend on details. Details should depend
upon abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 52 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Dependency Inversion Principle (DIP)

High level modules should not depend on low level modules; both
should depend on abstractions
Abstractions should not depend on details. Details should depend
upon abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 52 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

High-Level Modules

Modules written to solve real problems and use cases


They are more abstract and map to the business domain
What the software should do

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 53 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Low-Level Modules

Contain implementation details that are required to execute the


business policies
They are considered the “plumbing” or “internals” of an application
How the software should do various tasks

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 54 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Examples of Low-Level Modules

Logging
Data-access
Network communication
IO

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 55 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Relationship between High-Level and Low-Level Modules

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 56 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Abstractions

Something that is not concrete


Something that you can not “new” up

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 57 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Putting it together

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 58 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

Low-Level Class

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

High-Level Class

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

Abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

Low-Level Class depends on Abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

High-Level Class depends on Abstraction

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

Creating a Factory

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

Creating a Factory

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Writing the code to follow DIP

After applying DIP

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 59 / 65
Dependency Inversion Principle (DIP) Dependency Inversion

Dependency Injection

Still some coupling

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 60 / 65
Dependency Inversion Principle (DIP) Dependency Injection (DI)

Dependency Injection

A technique that allows the creation of dependent objects outside of


a class and provides those objects to a class

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 61 / 65
Dependency Inversion Principle (DIP) Dependency Injection (DI)

Dependency Injection

Declaring dependencies in constructor

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 62 / 65
Dependency Inversion Principle (DIP) Dependency Injection (DI)

Dependency Injection

Passing dependencies

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 62 / 65
Dependency Inversion Principle (DIP) Inversion of Control (IoC)

Inversion of Control (IoC)

Inversion of Control is a design principle in which the control of object


creation, configuration, and lifecycle is passed to a container or
framework

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 63 / 65
Dependency Inversion Principle (DIP) Inversion of Control (IoC)

Inversion of Control (IoC)

Complex dependencies

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 64 / 65
Dependency Inversion Principle (DIP) Inversion of Control (IoC)

Summing up!

Classes should depend on abstractions, not implementation details


DIP, DI and loC work hand in hand to eliminate coupling and make
applications less brittle
New is glue

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 65 / 65
Dependency Inversion Principle (DIP) Inversion of Control (IoC)

Summing up!

Classes should depend on abstractions, not implementation details


DIP, DI and loC work hand in hand to eliminate coupling and make
applications less brittle
New is glue

SOLID Design Principles Object Oriented Analysis and Design September 22, 2021 65 / 65

You might also like