46
Inversion of Control And the Dependency Inversion Principle What’s Inversio n?

Inversion of control

Embed Size (px)

DESCRIPTION

A talk on Inversion of Control: what it is, how it helps and how to do it.

Citation preview

Page 1: Inversion of control

Inversion of ControlAnd the Dependency Inversion Principle

What’s Inversion

?

Page 2: Inversion of control

The Dependency Inversion Principle

“High-level modules should not depend on low-level

modules. Both should depend on abstractions.

Abstractions should not depend on details. Details

should depend on abstractions.”

The “Plugin Principle”

Page 3: Inversion of control

Why?

Page 4: Inversion of control

Outline“Good” and “Bad” Design

Cohesion and Coupling Inversion of Control

What is it?Code ExamplesPlugins

Conclusions

Page 5: Inversion of control

Characteristics of “Bad” Design

Rigidity:Difficult to change because a change affects other parts

Fragility:One change causes unexpected breaks

Immobility:Difficult to reuse – tied to specific implementation

Page 6: Inversion of control

The Big Ones (Good Code)

High CohesionLow Coupling

Page 7: Inversion of control

Cohesion and Coupling

Page 8: Inversion of control

Cohesion

● Coincidental cohesion

● Logical cohesion

● Procedural cohesion

● Communicational cohesion

● Functional cohesion

Better

http://www.codeodor.com/index.cfm/2009/6/17/Strive-for-low-coupling-and-high-cohesion-What-does-that-even-mean/2902

Page 9: Inversion of control

Coincidental Cohesion

Grouped at random, nothing relates the parts

Eg. Frequently used functions grouped in a class called “Utils”

Page 10: Inversion of control

Logical Cohesion

Grouped because they are logically categorized to do the same thing even if they are fundamentally different

Eg. Grouping IO functions

Page 11: Inversion of control

Procedural Cohesion

Grouped by order of when things happen

Eg. Checking file permissions and then opening it

Page 12: Inversion of control

Communicational Cohesion

Grouped because they operate on the same data

Page 13: Inversion of control

Functional Cohesion

Grouped because they all contribute to a single well-defined task of the module

Single Responsibility Principle in Action!

Page 14: Inversion of control

Example of Low Cohesive code

Page 15: Inversion of control

Example of Low Cohesive Code

Included are 35+ functions that provide you with

the ability to [use] a nicely formatted var dump, validate

emails, generate random strings, flatten an array, pull a

single column out of a multidimensional array and much more!

Util::ity.php – every programmers little buddy

Page 16: Inversion of control

Coupling

● Content Coupling

● Control Coupling

● Stamp Coupling

● Data Coupling

● Message Coupling

Better

http://www.codeodor.com/index.cfm/2009/6/17/Strive-for-low-coupling-and-high-cohesion-What-does-that-even-mean/2902

Page 17: Inversion of control

Content Coupling

One class directly manipulates another's data, so a change in the data means a change in the other class too

Two modules share the same global data (e.g. a global variable). Changing the shared resource implies changing all the modules using it

Page 18: Inversion of control

Control Coupling

One module controlling the logic of another, by passing it information on what to do (e.g. passing a what-to-do flag)

Page 19: Inversion of control

Stamp (Data-Structured) Coupling

Modules both use a common data structure and use only a part of it. Can lead to unexpected breaks when data structure changes

Page 20: Inversion of control

Data Coupling

Objects share data through parameters

$mailer = new Mailer();$mailer->sendEmail($to, $from, $body);

Page 21: Inversion of control

Message Coupling

Objects use a public interface to exchange messages (events, observer pattern)

Page 22: Inversion of control

No Coupling

Objects don’t know about each other and never communicate with each other.

Page 23: Inversion of control

Example of Coupled code

Page 24: Inversion of control

Inversion of Control

Page 25: Inversion of control

What is Inversion of Control (IOC)

“Moving the decision of which concrete

class to use away from the part of the

system which uses it”

Flexibility!

Page 26: Inversion of control

What is Inversion of Control?Before After

Inversion!

Page 27: Inversion of control

Ok cool, How?

1. Create objects elsewhere (DI)o Constructor/Setter Injection

2. Depend on Abstractions (D in SOLID)

Page 28: Inversion of control

First Example

Page 29: Inversion of control

Back to the “Coupled” Code

Page 30: Inversion of control

Step 1: Create objects elsewhere

Page 31: Inversion of control

Step 2: Depend on Abstractions

Page 32: Inversion of control

Bonus: Cleanup

Page 33: Inversion of control

What did we win?

TestabilityReusabilityFlexibility

Decoupled Decouple

d

Page 34: Inversion of control

Done! Dependencies are Inverted!

Bonus:

SRP?

Page 35: Inversion of control

Bonus: The Single Responsibility Principle

Page 36: Inversion of control

Another Example

Page 37: Inversion of control

Another Example

Page 38: Inversion of control

Step 1: Create objects elsewhere

Page 39: Inversion of control

Step 2: Depend on Abstractions

Page 40: Inversion of control

Bonus: Cleanup!

Page 41: Inversion of control

Plugins

Page 42: Inversion of control

Plugins

Wow, it’s a Plugin!!!

Page 43: Inversion of control

“Divide by Boundaries, then Invert the Dependencies that

cross those Boundaries”Uncle Bob - Clean Coders Video “Dependency Inversion Principle”

The “Plugin Principle”

Page 44: Inversion of control

Conclusions

Page 45: Inversion of control

Conclusions

So we know how IOC helps…And we know how to do it…

But when should we apply it?

Hint: always

Page 46: Inversion of control

References http://martinfowler.com/bliki/InversionOfControl.html http://martinfowler.com/articles/injection.html https://leanpub.com/cleanphp http://fabien.potencier.org/media/talk/2008/decouple-your-code-for-reusability-ipc-2008.pdf https://gist.github.com/Integralist/5763515 http://

www.codeodor.com/index.cfm/2009/6/17/Strive-for-low-coupling-and-high-cohesion-What-does-that-even-mean/2902

So, what was Inversion

again?

Questions?