Upload
emmet-irish
View
233
Download
0
Embed Size (px)
DESCRIPTION
A talk on Inversion of Control: what it is, how it helps and how to do it.
Citation preview
Inversion of ControlAnd the Dependency Inversion Principle
What’s Inversion
?
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”
Why?
Outline“Good” and “Bad” Design
Cohesion and Coupling Inversion of Control
What is it?Code ExamplesPlugins
Conclusions
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
The Big Ones (Good Code)
High CohesionLow Coupling
Cohesion and Coupling
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
Coincidental Cohesion
Grouped at random, nothing relates the parts
Eg. Frequently used functions grouped in a class called “Utils”
Logical Cohesion
Grouped because they are logically categorized to do the same thing even if they are fundamentally different
Eg. Grouping IO functions
Procedural Cohesion
Grouped by order of when things happen
Eg. Checking file permissions and then opening it
Communicational Cohesion
Grouped because they operate on the same data
Functional Cohesion
Grouped because they all contribute to a single well-defined task of the module
Single Responsibility Principle in Action!
Example of Low Cohesive code
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
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
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
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)
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
Data Coupling
Objects share data through parameters
$mailer = new Mailer();$mailer->sendEmail($to, $from, $body);
Message Coupling
Objects use a public interface to exchange messages (events, observer pattern)
No Coupling
Objects don’t know about each other and never communicate with each other.
Example of Coupled code
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!
What is Inversion of Control?Before After
Inversion!
Ok cool, How?
1. Create objects elsewhere (DI)o Constructor/Setter Injection
2. Depend on Abstractions (D in SOLID)
First Example
Back to the “Coupled” Code
Step 1: Create objects elsewhere
Step 2: Depend on Abstractions
Bonus: Cleanup
What did we win?
TestabilityReusabilityFlexibility
Decoupled Decouple
d
Done! Dependencies are Inverted!
Bonus:
SRP?
Bonus: The Single Responsibility Principle
Another Example
Another Example
Step 1: Create objects elsewhere
Step 2: Depend on Abstractions
Bonus: Cleanup!
Plugins
Plugins
Wow, it’s a Plugin!!!
“Divide by Boundaries, then Invert the Dependencies that
cross those Boundaries”Uncle Bob - Clean Coders Video “Dependency Inversion Principle”
The “Plugin Principle”
Conclusions
Conclusions
So we know how IOC helps…And we know how to do it…
But when should we apply it?
Hint: always
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?