SlideShare a Scribd company logo
THE ADAPTER PATTERN
DARREN CRAIG
@minusdarren
S.O.L.I.D. OOP
★ Single Responsibility
★ Open / Closed
★ Liskov Substitution Principle
★ Interface Segregation Principle
★ Dependency Inversion Principle
LISKOV SUBSTITUTION PRINCIPLE
“objects in a program should be replaceable
with instances of their subtypes without
altering the correctness of that program.”
THE PROBLEM
“Allow users in our system to post updates
to Facebook.”
- The Client
WE MEET THEIR REQUIREMENTS
class User
{
public function updateStatus($status)
{
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
}
}
// The Facebook SDK
class Facebook
{
public function updateStatus($status);
}
$user->updateStatus($status);
NEXT…
“Allow users in our system to post updates
to Facebook or Twitter”
- The Indecisive Client
WE MEET THEIR REQUIREMENTS
class User
{
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
// The Twitter SDK
class Twitter {
public function postStatus($status);
}
$user->updateStatus($status);
“We’re changing the
updateStatus()
method to
createStatus()”
NEXT…
“We’re changing the
updateStatus()
method to
createStatus()”
NEXT…
FINE…
class User
{
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
FINE…
class User
{
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
class User
{
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->createStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
BUT…
class HufflepuffController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
BUT…
class HufflepuffController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
class GryffindoorController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
BUT…
class HufflepuffController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
class GryffindoorController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
class HogwartsController {
public function updateStatus($status, $gateway = 'FACEBOOK') {
switch($gateway) {
case: "FACEBOOK";
$facebook = new FacebookFacebook;
$facebook->updateStatus($status);
break;
case: "TWITTER";
$twitter = new TwitterTwitter;
$twitter->postStatus($status);
break;
}
}
}
IT’S A PAIN
★ We should be able to easily add new
gateways whenever the client asks
★ We should be able to easily adapt to API
changes without too much trouble.
ADAPTERS FTW!
★ We need an interface that defines the functionality
required by out application
interface UpdatesStatuses
{
public function post($status);
public function delete($statusId);
}
FACEBOOK ADAPTER
class FacebookAdapter implements UpdatesStatuses
{
private $facebook;
public function __construct(FacebookFacebook $facebook)
{
$this->facebook = $facebook;
}
public function post($status)
{
$this->facebook->createStatus($status);
}
public function delete($statusId)
{
$this->facebook->deleteStatus($statusId);
}
}
TWITTER ADAPTER
class TwitterAdapter implements UpdatesStatuses
{
private $twitter;
public function __construct(TwitterTwitter $twitter)
{
$this->twitter = $twitter;
}
public function post($status)
{
$this->twitter->postStatus(status);
}
public function delete($statusId)
{
$this->twitter->removeStatus($statusId);
}
}
TAKING IT FURTHER
★ We want users to be able to add several social
accounts (connected accounts)
★ When they post using our application, the post
should also be shared on all of their social
accounts.
SOCIAL ACCOUNTS INTERFACE
interface SocialAccount
{
public function connect();
public function post($post);
// other stuff
}
SOCIAL ACCOUNT CLASSES
class FacebookAccount implements SocialAccount
{
public function __construct(FacebookFacebook $facebook) {
$this->facebook = $facebook;
}
public function post($post) {
$this->facebook->createPost($post);
}
}
class TwitterAccount implements SocialAccount
{
public function __construct(TwitterTwitter $twitter) {
$this->twitter = $twitter;
}
public function post($post) {
$this->twitter->makePost($post);
}
}
class LinkedInAccount implements SocialAccount
{
public function __construct(LinkedInLinkedIn $linkedin) {
$this->linkedin = $linkedin;
}
public function post($post) {
$this->linkedin->makePost($post);
}
}
THE USER
class User
{
private $connectedAccounts = [];
public function addConnectedAccount(SocialAccount $account)
{
$this->connectedAccounts[] = $account;
}
public function postToAccounts($status)
{
foreach($this->connectedAccounts as $account)
{
$account->post($status);
}
}
}
$user->addConnectedAccount(new FacebookAccount(new FacebookFacebook));
$user->addConnectedAccount(new TwitterAccount(new TwitterTwitter));
$user->addConnectedAccount(new LinkedInAccount(new LinkedInLinkedIn));
// Sometime in the future.
$user->postToAccounts($status)
PRO TIP: IOC - INVERSION OF CONTROL
★ Resolves classes and their dependencies without having to
institute them each time
$user->addConnectedAccount(new FacebookAccount(new FacebookFacebook));
// becomes
$user->addConnectedAccount(new FacebookAccount());
★ Laravel has a built in IoC container
★ A few platform agnostic packages available (Pimple, Dice)
QUESTIONS?

More Related Content

What's hot

Google app engine by example
Google app engine by exampleGoogle app engine by example
Google app engine by example
Alexander Zamkovyi
 
Introducing Vuex in your project
Introducing Vuex in your projectIntroducing Vuex in your project
Introducing Vuex in your project
Denny Biasiolli
 
Deploying applications to Cloud with Google App Engine
Deploying applications to Cloud with Google App EngineDeploying applications to Cloud with Google App Engine
Deploying applications to Cloud with Google App Engine
Alexander Zamkovyi
 
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
William Leeper
 
Pundit
PunditPundit
Pundit
Bruce White
 
Asp.net life cycle
Asp.net life cycleAsp.net life cycle
Asp.net life cycle
Irfaan Khan
 
Start developing Facebook apps in 13 steps
Start developing Facebook apps in 13 stepsStart developing Facebook apps in 13 steps
Start developing Facebook apps in 13 steps
Masakuni Kato
 
Asp.net life cycle in depth
Asp.net life cycle in depthAsp.net life cycle in depth
Asp.net life cycle in depth
sonia merchant
 
Page life cycle IN ASP.NET
Page life cycle IN ASP.NETPage life cycle IN ASP.NET
Page life cycle IN ASP.NET
Sireesh K
 
Rohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan viharRohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan vihar
Rohit malav
 

What's hot (10)

Google app engine by example
Google app engine by exampleGoogle app engine by example
Google app engine by example
 
Introducing Vuex in your project
Introducing Vuex in your projectIntroducing Vuex in your project
Introducing Vuex in your project
 
Deploying applications to Cloud with Google App Engine
Deploying applications to Cloud with Google App EngineDeploying applications to Cloud with Google App Engine
Deploying applications to Cloud with Google App Engine
 
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
 
Pundit
PunditPundit
Pundit
 
Asp.net life cycle
Asp.net life cycleAsp.net life cycle
Asp.net life cycle
 
Start developing Facebook apps in 13 steps
Start developing Facebook apps in 13 stepsStart developing Facebook apps in 13 steps
Start developing Facebook apps in 13 steps
 
Asp.net life cycle in depth
Asp.net life cycle in depthAsp.net life cycle in depth
Asp.net life cycle in depth
 
Page life cycle IN ASP.NET
Page life cycle IN ASP.NETPage life cycle IN ASP.NET
Page life cycle IN ASP.NET
 
Rohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan viharRohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan vihar
 

Similar to The Adapter Pattern in PHP

Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
Divante
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
SWIFTotter Solutions
 
Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0
Yevhen Kotelnytskyi
 
Chekout demistified
Chekout demistifiedChekout demistified
Chekout demistified
Damijan Ćavar
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
Jeroen van Dijk
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
Jeroen van Dijk
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?
Yevhen Kotelnytskyi
 
Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13
Stephan Hochdörfer
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
arcware
 
S.O.L.I.D. Principles
S.O.L.I.D. PrinciplesS.O.L.I.D. Principles
S.O.L.I.D. Principles
Jad Salhani
 
Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5
Oleg Poludnenko
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
Krzysztof Menżyk
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
PROIDEA
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
Jonathan Wage
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpday
Stephan Hochdörfer
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
mtoppa
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
HAO-WEN ZHANG
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 

Similar to The Adapter Pattern in PHP (20)

Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0
 
Chekout demistified
Chekout demistifiedChekout demistified
Chekout demistified
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?
 
Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
S.O.L.I.D. Principles
S.O.L.I.D. PrinciplesS.O.L.I.D. Principles
S.O.L.I.D. Principles
 
Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpday
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 

Recently uploaded

E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 

Recently uploaded (20)

E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 

The Adapter Pattern in PHP

  • 1. THE ADAPTER PATTERN DARREN CRAIG @minusdarren
  • 2. S.O.L.I.D. OOP ★ Single Responsibility ★ Open / Closed ★ Liskov Substitution Principle ★ Interface Segregation Principle ★ Dependency Inversion Principle
  • 3. LISKOV SUBSTITUTION PRINCIPLE “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.”
  • 4. THE PROBLEM “Allow users in our system to post updates to Facebook.” - The Client
  • 5. WE MEET THEIR REQUIREMENTS class User { public function updateStatus($status) { $facebook = new FacebookFacebook; $facebook->updateStatus($status); } } // The Facebook SDK class Facebook { public function updateStatus($status); } $user->updateStatus($status);
  • 6. NEXT… “Allow users in our system to post updates to Facebook or Twitter” - The Indecisive Client
  • 7. WE MEET THEIR REQUIREMENTS class User { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } } // The Twitter SDK class Twitter { public function postStatus($status); } $user->updateStatus($status);
  • 8. “We’re changing the updateStatus() method to createStatus()” NEXT…
  • 9. “We’re changing the updateStatus() method to createStatus()” NEXT…
  • 10. FINE… class User { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } }
  • 11. FINE… class User { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } } class User { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->createStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } }
  • 12. BUT… class HufflepuffController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } }
  • 13. BUT… class HufflepuffController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } } class GryffindoorController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } }
  • 14. BUT… class HufflepuffController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } } class GryffindoorController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } } class HogwartsController { public function updateStatus($status, $gateway = 'FACEBOOK') { switch($gateway) { case: "FACEBOOK"; $facebook = new FacebookFacebook; $facebook->updateStatus($status); break; case: "TWITTER"; $twitter = new TwitterTwitter; $twitter->postStatus($status); break; } } }
  • 15.
  • 16. IT’S A PAIN ★ We should be able to easily add new gateways whenever the client asks ★ We should be able to easily adapt to API changes without too much trouble.
  • 17. ADAPTERS FTW! ★ We need an interface that defines the functionality required by out application interface UpdatesStatuses { public function post($status); public function delete($statusId); }
  • 18. FACEBOOK ADAPTER class FacebookAdapter implements UpdatesStatuses { private $facebook; public function __construct(FacebookFacebook $facebook) { $this->facebook = $facebook; } public function post($status) { $this->facebook->createStatus($status); } public function delete($statusId) { $this->facebook->deleteStatus($statusId); } }
  • 19. TWITTER ADAPTER class TwitterAdapter implements UpdatesStatuses { private $twitter; public function __construct(TwitterTwitter $twitter) { $this->twitter = $twitter; } public function post($status) { $this->twitter->postStatus(status); } public function delete($statusId) { $this->twitter->removeStatus($statusId); } }
  • 20. TAKING IT FURTHER ★ We want users to be able to add several social accounts (connected accounts) ★ When they post using our application, the post should also be shared on all of their social accounts.
  • 21. SOCIAL ACCOUNTS INTERFACE interface SocialAccount { public function connect(); public function post($post); // other stuff }
  • 22. SOCIAL ACCOUNT CLASSES class FacebookAccount implements SocialAccount { public function __construct(FacebookFacebook $facebook) { $this->facebook = $facebook; } public function post($post) { $this->facebook->createPost($post); } } class TwitterAccount implements SocialAccount { public function __construct(TwitterTwitter $twitter) { $this->twitter = $twitter; } public function post($post) { $this->twitter->makePost($post); } } class LinkedInAccount implements SocialAccount { public function __construct(LinkedInLinkedIn $linkedin) { $this->linkedin = $linkedin; } public function post($post) { $this->linkedin->makePost($post); } }
  • 23. THE USER class User { private $connectedAccounts = []; public function addConnectedAccount(SocialAccount $account) { $this->connectedAccounts[] = $account; } public function postToAccounts($status) { foreach($this->connectedAccounts as $account) { $account->post($status); } } } $user->addConnectedAccount(new FacebookAccount(new FacebookFacebook)); $user->addConnectedAccount(new TwitterAccount(new TwitterTwitter)); $user->addConnectedAccount(new LinkedInAccount(new LinkedInLinkedIn)); // Sometime in the future. $user->postToAccounts($status)
  • 24. PRO TIP: IOC - INVERSION OF CONTROL ★ Resolves classes and their dependencies without having to institute them each time $user->addConnectedAccount(new FacebookAccount(new FacebookFacebook)); // becomes $user->addConnectedAccount(new FacebookAccount()); ★ Laravel has a built in IoC container ★ A few platform agnostic packages available (Pimple, Dice)