Solid principles

31
SOLID Principles Presented by Mónica Rodrigues January 24rd, 2017

Transcript of Solid principles

Page 1: Solid principles

SOLID PrinciplesPresented by Mónica Rodrigues

January 24rd, 2017

Page 2: Solid principles

Contents

➢ What is SOLID➢ Single responsibility Principle➢ Open/Closed Principle➢ Liskov Substitution Principle➢ Interface Segregation

Principle

➢ Dependency Inversion Principle

Page 3: Solid principles

Who am I?Mónica RodriguesDegree in Computer Science Engineer at ISEL

Software engineer since 2009

Keywords: Web Applications, ASP.NET MVC, ASP.NET Web API, REST, Javascript, Angular, Best practices, Design patterns, SQL Server and much more..

Page 4: Solid principles

What is SOLID?

SSRPSingleResponsibilityPrinciple

OOCPOpen/ClosedPrinciple

LLSPLiskovsSubstitutionPrinciple

IISPInterfaceSegregationPrinciple

DDIPDependencyInversionPrinciple

Page 5: Solid principles

“A class should have one and

only one reason to change”

Single Responsibility Principle

Page 6: Solid principles

Single Responsibility Principle

Business logic

Persistence

There are two responsibilities

Page 7: Solid principles

Single Responsibility Principle

How to solve this?

Page 8: Solid principles

Single Responsibility Principle

Just create two different classes

Page 9: Solid principles

Open/Closed Principle

“Software entities should be open for extension, but closed for modification.”

Page 10: Solid principles

Open/Closed Principle

Humm...and if I need to add a new payment type?

You need to modificate this class.

Page 11: Solid principles

Open/Closed Principleopen for extension

close for modification

Page 12: Solid principles

Liskov Substitution Principle

“Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T”

What do you say?

Page 13: Solid principles

Liskov Substitution Principle

“A subclass should behave in such a way that it will not

cause problems when used instead of the superclass.”

Page 14: Solid principles

Liskov Substitution Principle

Page 15: Solid principles

Liskov Substitution Principle

Much better

Page 16: Solid principles

Interface Segregation Principle

“Clients should not be forced to depend upon

interfaces that they don't use”

Page 17: Solid principles

Interface Segregation Principle

Page 18: Solid principles

Interface Segregation Principle

WHY?????I don’t need you!!

Page 19: Solid principles

Interface Segregation Principle

How to solve this?

Page 20: Solid principles

Interface Segregation Principle

You need to create two interfaces

Page 21: Solid principles

Interface Segregation Principle

Page 22: Solid principles

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.”

Page 23: Solid principles

Dependency Inversion Principle

And if I need to send a notification by SMS?You need to change this.

Page 24: Solid principles

Dependency Inversion PrincipleSo, I create an interface and now?

Page 25: Solid principles

Dependency Inversion Principle

Page 26: Solid principles

Dependency Inversion PrincipleConstructor injection:

Page 27: Solid principles

Dependency Inversion PrincipleProperty injection:

Page 28: Solid principles

Dependency Inversion PrincipleMethod injection:

Page 29: Solid principles

Keep in mindDRY - Don’t repeat yourself+ SLAP - Single layer abstraction principle + SOLID BEST

DEVELOPER

Page 30: Solid principles

Enjoy code and Keep it simple!

Page 31: Solid principles

Thank you