solid and ioc principles

Post on 13-Apr-2017

1.181 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

eleks.com eleks.com

SOLID AND IOC PRINCIPLES The first five principles of OOP design… And the additional one

Mykhailo Romaniuk

Single Responsibility Principle

SRP

A class should have only one reason to change

Robert C. Martin

SRPStates that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class

Open/Closed Principle

OCP

Objects or entities should be open for extension, but closed for modification

Bertrand Meyer

OCPStates that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification, that is, such an entity can allow its behavior to be extended without modifying its source code

Liskov Substitution Principle

Can a toy-dog woof without batteries?

LSP

Let f(x) be a property of objects X of type T. Then f(y) should be true for objects Y of type S where S is a subtype of T

Barbara Liskov

LSP

States that 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 (correctness, task performed)

Interface Segregation Principle

ISP

Many client-specific interfaces are better than one general-purpose interface

Robert C. Martin

ISP

States that no client should be forced to depend on methods it does not use. Thus having multiple grained interfaces is better that one.

Dependency Inversion Principle

DIP

One should depend upon abstractions, not on concretions

Robert C. Martin

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

Inversion of Control Principle

What Is IoC?

In software engineering, IoC describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library.

Wikipedia

“How To IoC”?

• Events• Delegates• Dependency

Injection

Dependency Injection

Dependency injection means giving an object it’s instance variables. Really. That's it.

James Shore

Injection Types• Constructor

Injection• Using Property• Thru Initialization

Method

Software Testability

If your code isn't testable, then you have a design problem

Common Sense

What Makes Your Software Testable?• SRP. Since type is doing only one job, you

can clearly understand what should be tested.

• LSP. Since types used can be replaced with subtypes, mocks and stubs can be used instead of real types.

• ISP. Since there are more granular client-specific interfaces, you know exactly what to mock.

Let’s Review Some Concepts• Reusable. Higher-level

components can be reused if lower-lever components change with time to meet the requirements.

• Extensible. Software is easily extended with new components and features because design is based on abstractions.

• Replaceable. Lower-level components can be replaced to meet new requirements without modifying existing code.

• Loosely Coupled. Allows components to perform on their own with as little knowledge as possible about other components.

• Cohesive. Build you software easily with different components as building blocks.

• Testable. It is easy to understand what and how to test in each component because components are loosely-coupled.

Thanks! Any Questions?

What can you read at home?• Wikipedia about SOLID

https://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29• Nice article about SOLID with examples

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

• Dive deeper into SOLIDhttp://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

• SOLID explanation with examples:http://www.codemag.com/article/1001061

• Simple explanation of DI for beginnershttp://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html

• IoC and DIhttp://martinfowler.com/articles/injection.html

eleks.com

Inspired by Technology.Driven by Value.

top related