+ All Categories
Home > Documents > SOLID principles-Present

SOLID principles-Present

Date post: 17-Feb-2017
Category:
Upload: quang-nguyen
View: 204 times
Download: 0 times
Share this document with a friend
43
OO Design: S.O.L.I.D principles Technical team DNB 2015 Page 1 Confidential Presenters: Quang Nguyen, Lan Nguyen
Transcript
Page 1: SOLID principles-Present

OO Design: S.O.L.I.D principles

Technical teamDNB 2015

Page 1Confidential

Presenters: Quang Nguyen, Lan Nguyen

Page 2: SOLID principles-Present

Page 2Confidential

OutlineSOLID principles introductionPurpose of use.Definition, explanation, examples and benefits of

+ Single responsibility principle (SRP) + Open – close Principle (OCP) + Linkov's substitution principle (LSP) + Interface segregation principle (ISP) + Dependency inversion principle (DIP)

ConclusionQ&A

Page 3: SOLID principles-Present

Page 3Confidential

Design Matter: Doing Better with Less

Page 4: SOLID principles-Present

Introduction

Page 4Confidential

S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

S – Single Responsibility principleO – Open-closed principleL – Liskov’s substitution principleI – Interface segregation principleD – Dependency Inversion principle

Page 5: SOLID principles-Present

What are the issues that developers always face to in software development lifecycle?

Question

Page 6: SOLID principles-Present

Requirements change

Page 6Confidential

Your requirements will always change (and grow) over time

Your system need to evolve to handle the changes.We should build a software which is smartly designed

enough to change..

Page 7: SOLID principles-Present

What is “Smartly-designed Software” ?

Page 7Confidential

Handle the changes with minimal re-factor and effort.ExtendableReusableFlexible

Page 8: SOLID principles-Present

Customer and Developer satisfaction

Page 8Confidential

Customers are satisfied because the software:+ Work well + Keeps working - always.+ Can be changed, upgraded easily+ Reuse to build other software.+ Extendable, Flexible.

Help the developers:+ Handle the changes easily.+ Code better in any programming languages.

Page 9: SOLID principles-Present

Page 9Confidential

PURPOSE OF USE

When combined together, these principles will help your software: Become well and smartly-designed Easily handle the changes Make both customer and developer satisfied with their work.Save money and time.

Page 10: SOLID principles-Present

Single Responsibility principle (SRP)

Page 10Confidential

Page 11: SOLID principles-Present

SRP - Definition

Page 11Confidential

“An object should have one and only one reason to change”

Page 12: SOLID principles-Present

SRP - Explanation

Page 12Confidential

Responsibility is a family of functions that serves one particular purpose.If you have a class having more than one reason to change=> you should split the class into multiple classes base on their responsibilities.

Page 13: SOLID principles-Present

SRP - Example

Page 13Confidential

Page 14: SOLID principles-Present

SRP – Why splitting is important ?

Page 14Confidential

- If classes have more than one responsibility ?- Each responsibility is an axis of change.

+ Codes become complicated+ One change effects another

- Big classes:+ Difficult to change+ Harder to read

+ Bugs

Page 15: SOLID principles-Present

SRP – Benefit

Page 15Confidential

Increase the reusability of source codeMake your classes smaller, lesser complicated.Restrict effects to other modules when we change

source codes.Easier to read and write => less to write bugs.Higher cohesion, lower coupling.

Smaller classes and smaller methods will give you more flexibility

More classes != more complicated

Page 16: SOLID principles-Present

Open – Close Principle (OCP)

Page 16Confidential

Page 17: SOLID principles-Present

OCP - Definition

Page 17Confidential

“An object should be opened for extension and closed for modification”

Page 18: SOLID principles-Present

OCP - Explanation

Page 18Confidential

- You should extend a class without modifying it. - if you read and understand all source code to find

what they are supposed to do and update. so hard cost time, effort bugs

- But it’s easy to extend the source code to meet the new requirements.

Page 19: SOLID principles-Present

OCP - Example

Page 19Confidential

Page 20: SOLID principles-Present

OCP – Benefit

Page 20Confidential

- Restrict effects to others because when you change code.- Not need to read all old source codes that already worked well.- Extend the existing libraries.

Page 21: SOLID principles-Present

- Template Method Design Pattern- Strategy Design Pattern.

Reference

Page 22: SOLID principles-Present

Liskov's Substitution (LSP) - Definition

Page 22Confidential

“Objects should be replaceable with their base classes without altering the correctness of the program”

Page 23: SOLID principles-Present

LSP - Explanation

Page 23Confidential

+ Should always use a base class or interface instead of the subclasses. + This works correctly with any subclasses of X.

Extends without replacing any base class's functions

Page 24: SOLID principles-Present

LSP - Example

Page 24Confidential

Page 25: SOLID principles-Present

LSP – Benefit

Page 25Confidential

- This is an extension of OCP Wrong LSP => wrong OCP Inherit all OCP's benefits.- Preventing bugs caused by subclasses.

Page 26: SOLID principles-Present

Interface segregation Principle (ISP)

Page 26Confidential

Page 27: SOLID principles-Present

ISP - Definition

Page 27Confidential

“A client should not implement an interface, if it doesn't use that”

Page 28: SOLID principles-Present

ISP - Explanation

Page 28Confidential

Clients should not be forced to implement interfaces they don't use

=> Split a fat interface which has many methods into many small interfaces.

Page 29: SOLID principles-Present

ISP - Example

Page 29Confidential

Page 30: SOLID principles-Present

ISP – Benefit

Page 30Confidential

- Smaller interface => more reusable - Extendable - Flexible - Allows people to use only the parts of objects that they need. - High cohesion, low coupling - Comply SRP.

Page 31: SOLID principles-Present

Dependence Inversion Principle (DIP)

Page 31Confidential

Page 32: SOLID principles-Present

DIP - Definition

Page 32Confidential

“High level model should not depend on low level module, both should depend on abstractions

Abstractions should not depend on details. Details should depend on abstractions”

Page 33: SOLID principles-Present

DIP - Explanation

Page 33Confidential

- Low level module: implement basic and primary operations- High level module: encapsulate complex logic- The high level classes should not directly use and heavily depend on low level classes.=> we should create abstraction layer between high level classes and low level classes

Page 34: SOLID principles-Present

DIP - Example

Page 34Confidential

Page 35: SOLID principles-Present

Demo - Explanation

Page 35Confidential

Page 36: SOLID principles-Present

DIP - Explanation

Page 36Confidential

- Abstractions should not depend upon details- Details should depend on abstractions

Page 37: SOLID principles-Present

Page 37Confidential

- Spring frameworks' component

- Using Dependency Injection pattern

- Automatic class instantiation (from external files or annotation)

- Entirely remove the low-level component dependencies and achieve true DIP.

IoC Container & Dependency Injection

Page 38: SOLID principles-Present

DIP – Benefit

Page 38Confidential

- Reusable- Flexible- More readable.- Higher cohesion - lower coupling- Remove dependencies between low and high level

modules.- Violate DIP => Violate OCP or LSP.- More classes != More complicated

Page 39: SOLID principles-Present

Should we always apply DIP anywhere?Question

Page 40: SOLID principles-Present

Conclusion

Page 40Confidential

With S.O.L.I.D principles, our software: Easily handle the changes Become well and smartly-designed Make both customer and developer satisfied with their work.Save money and time.

Page 41: SOLID principles-Present

OOP vs OOD

Page 41Confidential

Object oriented design (OOD) = OOP + Design principles.The source code applied OOD will be:+ Object oriented+ Re-usable+ Flexible: can be changed with minimal efforts.+ Extendable: can be extended without changing existing

codes

Page 42: SOLID principles-Present

Q&A

Page 42Confidential

Page 43: SOLID principles-Present

References

Page 43Confidential

http://www.oodesign.com/

http://www.codeproject.com/Articles/703634/SOLID-architecture-principles-using-simple-Csharp

https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design

Object oriented analysis and design book by Head First

Design pattern

Dependency Injection and IoC container in Spring Framework


Recommended