SlideShare a Scribd company logo
1 of 25
Download to read offline
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

Introducing Vuex in your project
Introducing Vuex in your projectIntroducing Vuex in your project
Introducing Vuex in your projectDenny 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 EngineAlexander Zamkovyi
 
Asp.net life cycle
Asp.net life cycleAsp.net life cycle
Asp.net life cycleIrfaan 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 stepsMasakuni Kato
 
Asp.net life cycle in depth
Asp.net life cycle in depthAsp.net life cycle in depth
Asp.net life cycle in depthsonia merchant
 
Page life cycle IN ASP.NET
Page life cycle IN ASP.NETPage life cycle IN ASP.NET
Page life cycle IN ASP.NETSireesh 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 viharRohit 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 scenariosDivante
 
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 CodeSWIFTotter Solutions
 
Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Yevhen Kotelnytskyi
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen 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 - phpugffm13Stephan 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. PrinciplesJad Salhani
 
Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Oleg Poludnenko
 
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żykPROIDEA
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpdayStephan Hochdörfer
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPressmtoppa
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in LaravelHAO-WEN ZHANG
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena 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
 
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
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
 
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

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

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)