SlideShare a Scribd company logo
@dbhurley OpenWest 2014
DI, SL, Testing, Life
A Pragmatic Approach
@dbhurley OpenWest 2014
WELCOME!
This session is not intimidating. This session is
not intimidating. This session is not intimidating.
This session is not intimidating. This session is
not intimidating. This session is not intimidating.
This session is not intimidating. This session is
not intimidating. This session is not intimidating.
This session is not intimidating. This session is
not intimidating. This session is not intimidating.
This session is not intimidating. This session is
not intimidating. This session is not intimidating.
This session is not intimidating. This session is
not inti
@dbhurley OpenWest 2014
WHO?
David Hurley
Open Source Evangelist
Entrepreneur / Co-Founder
Joomla! Community Manager
Production Leadership Team
@dbhurley OpenWest 2014
TERMS
Dependency Injection
An injection is the passing of a dependency (a service)
to a dependent object (a client).
Service Locator
A design pattern which uses a central registry and on
request returns the information necessary to a client.
Inversion of Control
A design in which portions of a computer program
receive flow of control from a generic, reusable library
@dbhurley OpenWest 2014
3 TYPES OF DEPENDENCY INJECTION
Constructor Setter Interface
class carPicker
{
function carPicker ( Finder $finder )
{
$this->finder = $finder;
@dbhurley OpenWest 2014
SERVICE LOCATORS
Most often seen used as a singleton registry*
class serviceLocator {
public static function carFinder ()
{
return static::finder;
}
class carPicker {
function carPicker() {
$finder = serviceLocator::carFinder();
@dbhurley OpenWest 2014
WHAT WE’RE NOT DISCUSSING
@dbhurley OpenWest 2014
WHAT WE ARE DISCUSSING
@dbhurley OpenWest 2014
THE LAW OF DEMETER
Each unit should have only limited knowledge about
other units: only units "closely" related to the current unit.
Each unit should only talk to its friends; don't talk to
strangers.
Only talk to your immediate friends.
@dbhurley OpenWest 2014
WHAT’S THAT MEAN?
Question
How do you pay your tab at a restaurant?
@dbhurley OpenWest 2014
APPLY SL
class Car {
protected $seats
protected $seatbelt
function buildCar (Locator $locator) {
$this->seats = $locator->getSeats();
$this->seatbelt = $locator->getSeatbelt();
...
}
}
@dbhurley OpenWest 2014
APPLY DI
class carFactory {
function buildCar() {
$seatbelt = new SeatBelt();
$seats = new Seats($seatbelt);
return new Car($seats);
}
}
@dbhurley OpenWest 2014
LOOKING DEEPER
class Car {
function _constructor(Seats $seats) {
...
}
}
class Seats {
function _constructor(Seatbelt $seatbelt, Fabric
$fabric) {
…
}
@dbhurley OpenWest 2014
RETURN TO FACTORY
class carFactory {
function buildCar() {
$seatbelt = new SeatBelt();
$fabric = new Fabric();
$seats = new Seats($seatbelt, $fabric);
return new Car($seats);
}
}
@dbhurley OpenWest 2014
KEY CONCEPT?
A Factory is used for all the objects of
the same lifetime.
Constructor Injection: Only inject items whose lifetime is
equal or greater than the injectee
(i.e. The seat is alive at least as long as the car)
Method Parameter: At execution for shorter lifetimes.
@dbhurley OpenWest 2014
TESTING
Service Locators are often difficult to test.
Mixed responsibilities / Law of Demeter partially broken
Dependency Injection allows for easy testing.
Clear to follow / Law of Demeter obeyed
Side note: Null checking
Pre-condition checking is hard to test. Rather check that
things work as expected rather than checking against
nulls.
@dbhurley OpenWest 2014
THE GOAL
The goal is to write clean, concise, easily-
testable code which accomplishes a
purpose and allows others to improve it.
@dbhurley OpenWest 2014
THE OUTCOME
A strong codebase which can be easily read
and understood by others.
“Always code as if the guy who ends
up maintaining your code will be a violent
psychopath who knows where you live.”
@dbhurley OpenWest 2014
QUESTIONS?
David Hurley
@dbhurley (twitter, facebook, linkedin)
me@dbhurley.com

More Related Content

Similar to Dependency Injection, Service Locators, Testing and Life

Deliver Double the Value in Half the Time
Deliver Double the Value in Half the TimeDeliver Double the Value in Half the Time
Deliver Double the Value in Half the Time
David Hawks
 
UX for Learning Design
UX for Learning DesignUX for Learning Design
UX for Learning Design
Julie Dirksen
 
Portfolio Presentation
Portfolio PresentationPortfolio Presentation
Portfolio Presentation
Mike Long
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
Mike Long
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
Mike Long
 
The Monolith First Strategy!
The Monolith First Strategy!The Monolith First Strategy!
The Monolith First Strategy!
Daniel Pfeiffer
 
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners MeetupUser Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
Agile Velocity
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Krzysztof Menżyk
 
Emergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with AgileEmergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with Agile
Alexandre Magno Figueiredo
 
AKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptxAKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptx
ammarsaputra23
 
Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014 Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014
Patrick Quattlebaum
 
PHP and Platform Independance in the Cloud
PHP and Platform Independance in the CloudPHP and Platform Independance in the Cloud
PHP and Platform Independance in the Cloud
ZendCon
 
Recruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation SlidesRecruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation Slides
SlideTeam
 
Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)
Wisetail
 
Empathy based product validation (full)
Empathy based product validation (full)Empathy based product validation (full)
Empathy based product validation (full)
Noble Ackerson
 
Recruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation SlidesRecruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation Slides
SlideTeam
 
Agile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the TimeAgile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the Time
David Hawks
 
Watercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptxWatercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptx
RestuBisnis
 
Techniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh JainTechniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh Jain
Naresh Jain
 
KAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the TimeKAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the Time
David Hawks
 

Similar to Dependency Injection, Service Locators, Testing and Life (20)

Deliver Double the Value in Half the Time
Deliver Double the Value in Half the TimeDeliver Double the Value in Half the Time
Deliver Double the Value in Half the Time
 
UX for Learning Design
UX for Learning DesignUX for Learning Design
UX for Learning Design
 
Portfolio Presentation
Portfolio PresentationPortfolio Presentation
Portfolio Presentation
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
 
The Monolith First Strategy!
The Monolith First Strategy!The Monolith First Strategy!
The Monolith First Strategy!
 
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners MeetupUser Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
 
Emergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with AgileEmergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with Agile
 
AKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptxAKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptx
 
Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014 Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014
 
PHP and Platform Independance in the Cloud
PHP and Platform Independance in the CloudPHP and Platform Independance in the Cloud
PHP and Platform Independance in the Cloud
 
Recruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation SlidesRecruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation Slides
 
Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)
 
Empathy based product validation (full)
Empathy based product validation (full)Empathy based product validation (full)
Empathy based product validation (full)
 
Recruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation SlidesRecruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation Slides
 
Agile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the TimeAgile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the Time
 
Watercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptxWatercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptx
 
Techniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh JainTechniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh Jain
 
KAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the TimeKAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the Time
 

More from David Hurley

Personalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super PowersPersonalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super Powers
David Hurley
 
Building Powerful Community Networks
Building Powerful Community NetworksBuilding Powerful Community Networks
Building Powerful Community Networks
David Hurley
 
Age of the Robot
Age of the RobotAge of the Robot
Age of the Robot
David Hurley
 
Joomla 3.4 Features and Timeline
Joomla 3.4 Features and TimelineJoomla 3.4 Features and Timeline
Joomla 3.4 Features and Timeline
David Hurley
 
Joomla!, The Future, and You
Joomla!, The Future, and YouJoomla!, The Future, and You
Joomla!, The Future, and You
David Hurley
 
Joomla! and You
Joomla! and YouJoomla! and You
Joomla! and You
David Hurley
 
The Future of Joomla
The Future of JoomlaThe Future of Joomla
The Future of Joomla
David Hurley
 
Code Like A Boss
Code Like A BossCode Like A Boss
Code Like A Boss
David Hurley
 
The Importance of the User Experience
The Importance of the User ExperienceThe Importance of the User Experience
The Importance of the User Experience
David Hurley
 
Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)David Hurley
 

More from David Hurley (10)

Personalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super PowersPersonalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super Powers
 
Building Powerful Community Networks
Building Powerful Community NetworksBuilding Powerful Community Networks
Building Powerful Community Networks
 
Age of the Robot
Age of the RobotAge of the Robot
Age of the Robot
 
Joomla 3.4 Features and Timeline
Joomla 3.4 Features and TimelineJoomla 3.4 Features and Timeline
Joomla 3.4 Features and Timeline
 
Joomla!, The Future, and You
Joomla!, The Future, and YouJoomla!, The Future, and You
Joomla!, The Future, and You
 
Joomla! and You
Joomla! and YouJoomla! and You
Joomla! and You
 
The Future of Joomla
The Future of JoomlaThe Future of Joomla
The Future of Joomla
 
Code Like A Boss
Code Like A BossCode Like A Boss
Code Like A Boss
 
The Importance of the User Experience
The Importance of the User ExperienceThe Importance of the User Experience
The Importance of the User Experience
 
Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)
 

Recently uploaded

Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 

Recently uploaded (20)

Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 

Dependency Injection, Service Locators, Testing and Life

  • 1. @dbhurley OpenWest 2014 DI, SL, Testing, Life A Pragmatic Approach
  • 2. @dbhurley OpenWest 2014 WELCOME! This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not inti
  • 3. @dbhurley OpenWest 2014 WHO? David Hurley Open Source Evangelist Entrepreneur / Co-Founder Joomla! Community Manager Production Leadership Team
  • 4. @dbhurley OpenWest 2014 TERMS Dependency Injection An injection is the passing of a dependency (a service) to a dependent object (a client). Service Locator A design pattern which uses a central registry and on request returns the information necessary to a client. Inversion of Control A design in which portions of a computer program receive flow of control from a generic, reusable library
  • 5. @dbhurley OpenWest 2014 3 TYPES OF DEPENDENCY INJECTION Constructor Setter Interface class carPicker { function carPicker ( Finder $finder ) { $this->finder = $finder;
  • 6. @dbhurley OpenWest 2014 SERVICE LOCATORS Most often seen used as a singleton registry* class serviceLocator { public static function carFinder () { return static::finder; } class carPicker { function carPicker() { $finder = serviceLocator::carFinder();
  • 7. @dbhurley OpenWest 2014 WHAT WE’RE NOT DISCUSSING
  • 8. @dbhurley OpenWest 2014 WHAT WE ARE DISCUSSING
  • 9. @dbhurley OpenWest 2014 THE LAW OF DEMETER Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Each unit should only talk to its friends; don't talk to strangers. Only talk to your immediate friends.
  • 10. @dbhurley OpenWest 2014 WHAT’S THAT MEAN? Question How do you pay your tab at a restaurant?
  • 11. @dbhurley OpenWest 2014 APPLY SL class Car { protected $seats protected $seatbelt function buildCar (Locator $locator) { $this->seats = $locator->getSeats(); $this->seatbelt = $locator->getSeatbelt(); ... } }
  • 12. @dbhurley OpenWest 2014 APPLY DI class carFactory { function buildCar() { $seatbelt = new SeatBelt(); $seats = new Seats($seatbelt); return new Car($seats); } }
  • 13. @dbhurley OpenWest 2014 LOOKING DEEPER class Car { function _constructor(Seats $seats) { ... } } class Seats { function _constructor(Seatbelt $seatbelt, Fabric $fabric) { … }
  • 14. @dbhurley OpenWest 2014 RETURN TO FACTORY class carFactory { function buildCar() { $seatbelt = new SeatBelt(); $fabric = new Fabric(); $seats = new Seats($seatbelt, $fabric); return new Car($seats); } }
  • 15. @dbhurley OpenWest 2014 KEY CONCEPT? A Factory is used for all the objects of the same lifetime. Constructor Injection: Only inject items whose lifetime is equal or greater than the injectee (i.e. The seat is alive at least as long as the car) Method Parameter: At execution for shorter lifetimes.
  • 16. @dbhurley OpenWest 2014 TESTING Service Locators are often difficult to test. Mixed responsibilities / Law of Demeter partially broken Dependency Injection allows for easy testing. Clear to follow / Law of Demeter obeyed Side note: Null checking Pre-condition checking is hard to test. Rather check that things work as expected rather than checking against nulls.
  • 17. @dbhurley OpenWest 2014 THE GOAL The goal is to write clean, concise, easily- testable code which accomplishes a purpose and allows others to improve it.
  • 18. @dbhurley OpenWest 2014 THE OUTCOME A strong codebase which can be easily read and understood by others. “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
  • 19. @dbhurley OpenWest 2014 QUESTIONS? David Hurley @dbhurley (twitter, facebook, linkedin) me@dbhurley.com