SlideShare a Scribd company logo
Laravel Design Patterns
About Me
Bobby Bouwmann
@bobbybouwmann
Enrise -Code Cuisine
markdownmail.com
Laracasts
Laravel Design Patterns
Daily Design Patterns
What are Design Patterns?
Design Patterns are ...
Recipes for building maintainable code
Be your own Chef
Using the boundaries of Design Pattern
Why use Design Patterns?
— Provides a way to solve issues related to software development using
a proven solution
— Makes communication between developer more efficient
Why use Design Patterns?
— There is no"silver bullet"for using design pattern,each project and
situation is different
Maintainable Code
Maintainable Code
— Understandable
— Intuitive
— Adaptable
— Extendable
— Debuggable
Maintainable Code
— Don't Repeat Yourself
— Keep It Simple Stupid
Design Pattern Time
Design Pattern types
— Creational Patterns
— Structural Patterns
— Behavioural Patterns
Creational Patterns
— Class instantiation
— Hiding the creation logic
— Examples: Factory,Builder,Prototype,Singleton
Structural Patterns
— Composition between objects
— Usages of interfaces,abstract classes
— Examples: Adapter,Bridge,Decorator,Facade,Proxy
Behavioural Patterns
— Communication between objects
— Usages of mostly interfaces
— Examples: Command,Iterator,Observer,State,Strategy
Design Patterns in Laravel
— Factory Pattern
— Builder (Manager) Pattern
— Strategy Pattern
— Provider Pattern
— Repository Pattern
— Iterator Pattern
— Singleton Pattern
— Presenter Pattern
Design Patterns in Laravel
— Factory Pattern
— Builder (Manager) Pattern
— Strategy Pattern
— Provider Pattern
Factory Pattern
Factory Pattern
Provides an interface for creating objects
without specifying their concrete classes.
Factory Pattern
— Decouples code
— Factory is responsible for creating objects,not the client
— Multiple clients call the same factory,one place for changes
— Easier to test,easy to mock and isolate
Factory Pattern Example
interface PizzaFactoryContract
{
public function make(array $toppings = []): Pizza;
}
class PizzaFactory implements PizzaFactoryContract
{
public function make(array $toppings = []): Pizza
{
return new Pizza($toppings);
}
}
$pizza = (new PizzaFactory)->make(['chicken', 'onion']);
Factory Pattern Laravel Example
class PostsController
{
public function index(): IlluminateViewView
{
$posts = Post::all();
return view('posts.index', ['posts' => $posts]);
}
}
// IlluminateFoundationhelpers.php
/**
* @return IlluminateViewView|IlluminateContractsViewFactory
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app(ViewFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($view, $data, $mergeData);
}
/**
* Get the evaluated view contents for the given view.
*
* @return IlluminateContractsViewView
*/
public function make($view, $data = [], $mergeData = [])
{
$path = $this->finder->find(
$view = $this->normalizeName($view)
);
$data = array_merge($mergeData, $this->parseData($data));
return tap($this->viewInstance($view, $path, $data), function ($view) {
$this->callCreator($view);
});
}
/**
* Create a new view instance from the given arguments.
*
* @return IlluminateContractsViewView
*/
protected function viewInstance($view, $path, $data)
{
return new View($this, $this->getEngineFromPath($path), $view, $path, $data);
}
/**
* Create a new Validator instance.
*
* @return IlluminateValidationValidator
*/
public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->resolve(
$data, $rules, $messages, $customAttributes
);
$this->addExtensions($validator);
return $validator;
}
/**
* Resolve a new Validator instance.
*
* @return IlluminateValidationValidator
*/
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
{
if (is_null($this->resolver)) {
return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
}
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
}
Builder (Manager) Pattern
Builder (Manager) Pattern
Builds complex objects step by step.It can return different objects
based on the given data.
Builder (Manager) Pattern
— Decouples code
— Focuses on building complex objects step by step and returns them
— Has functionality to decide which objects should be returned
Builder Pattern Example
class PizzaBuilder
{
public function make(PizzaBuilderInterface $pizza): Pizza
{
// Returns object of type Pizza
}
}
class PizzaBuilder
{
public function make(PizzaBuilderInterface $pizza): Pizza
{
$pizza->prepare();
$pizza->applyToppings();
$pizza->bake();
return $pizza;
}
}
interface PizzaBuilderInterface
{
public function prepare(): Pizza;
public function applyToppings(): Pizza;
public function bake(): Pizza;
}
class MargarithaBuilder implements PizzaBuilderInterface
{
protected $pizza;
public function prepare(): Pizza
{
$this->pizza = new Pizza();
return $this->pizza;
}
public function applyToppings(): Pizza
{
$this->pizza->setToppings(['cheese', 'tomato']);
return $this->pizza;
}
public function bake(): Pizza
{
$this->pizza->setBakingTemperature(180);
$this->pizza->setBakingMinutes(8);
return $this->pizza;
}
}
class PizzaBuilder
{
public function make(PizzaBuilderInterface $pizza): Pizza
{
return $pizza
->prepare();
->applyToppings();
->bake();
}
}
// Create a PizzaBuilder
$pizzaBuilder = new PizzaBuilder;
// Create pizza using Builder which returns Pizza instance
$pizza = $pizzaBuilder->make(new MargarithaBuilder());
class ChickenBuilder implements PizzaBuilderInterface
{
protected $pizza;
public function prepare(): Pizza
{
$this->pizza = new Pizza();
return $this->pizza;
}
public function applyToppings(): Pizza
{
$this->pizza->setToppings(['cheese', 'tomato', 'chicken', 'corn']);
return $this->pizza;
}
public function bake(): Pizza
{
$this->pizza->setBakingTemperature(200)
$this->pizza->setBakingMinutes(8);
return $this->pizza;
}
}
class PizzaBuilder
{
public function make(PizzaBuilderInterface $pizza): Pizza
{
return $pizza
->prepare()
->applyToppings()
->bake();
}
}
$pizzaBuilder = new PizzaBuilder;
$pizzaOne = $pizzaBuilder->make(new MargarithaBuilder());
$pizzaTwo = $pizzaBuilder->make(new ChickenBuilder());
Builder Pattern Laravel
class PizzaManager extends IlluminateSupportManager
{
public function getDefaultDriver()
{
return 'margaritha';
}
public function createMargarithaDriver(): PizzaBuilderInterface
{
return new MargarithaBuilder();
}
public function createChickenPizzaDriver(): PizzaBuilderInterface
{
return new ChickenPizzaBuilder();
}
}
public function driver($driver = null)
{
$driver = $driver ?: $this->getDefaultDriver();
// If the given driver has not been created before, we will create the instances
// here and cache it so we can return it next time very quickly. If there is
// already a driver created by this name, we'll just return that instance.
if (! isset($this->drivers[$driver])) {
$this->drivers[$driver] = $this->createDriver($driver);
}
return $this->drivers[$driver];
}
protected function createDriver($driver)
{
// We'll check to see if a creator method exists for the given driver. If not we
// will check for a custom driver creator, which allows developers to create
// drivers using their own customized driver creator Closure to create it.
if (isset($this->customCreators[$driver])) {
return $this->callCustomCreator($driver);
} else {
$method = 'create' . Str::studly($driver) . 'Driver';
if (method_exists($this, $method)) {
return $this->$method();
}
}
throw new InvalidArgumentException("Driver [$driver] not supported.");
}
class PizzaManager extends IlluminateSupportManager
{
public function getDefaultDriver()
{
return 'margaritha';
}
public function createMargarithaDriver(): PizzaBuilderInterface
{
return new MargarithaBuilder();
}
}
$manager = new PizzaManager();
// MargarithaBuilder implementing PizzaBuilderInterface
$builder = $manager->driver('margaritha');
$pizza = $builder->make(); // Pizza
// IlluminateMailTransportManager
class TransportManager extends Manager
{
protected function createSmtpDriver()
{
// Code for building up a SmtpTransport class
}
protected function createMailgunDriver()
{
// Code for building up a MailgunTransport class
}
protected function createSparkpostDriver()
{
// Code for building up a SparkpostTransport class
}
protected function createLogDriver()
{
// Code for building up a LogTransport class
}
}
class TransportManager extends Manager
{
public function getDefaultDriver()
{
return $this->app['config']['mail.driver'];
}
// All create{$driver}Driver methods
}
// config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
// config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
// IlluminateMailTransportManager
createSmtpDriver()
// config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
// IlluminateMailTransportManager
createSmtpDriver()
// app/Http/Controllers/RegisterController
Mail::to($user)
->send(new UserRegistered($user));
Laravel Manager Pattern Examples
IlluminateAuthAuthManager
IlluminateBroadcastingBroadcastManager
IlluminateCacheCacheManager
IlluminateFilesystemFilesystemManager
IlluminateMailTransportManager
IlluminateNotificationsChannelManager
IlluminateQueueQueueManager
IlluminateSessionSessionManager
Strategy Pattern
Strategy Pattern
Defines a familiy of algorithms that are
interchangeable
Strategy Pattern
Program to an interface, not an
implementation.
Strategy Pattern Example
interface DeliveryStrategy
{
public function deliver(Address $address);
}
interface DeliveryStrategy
{
public function deliver(Address $address);
}
class BikeDelivery implements DeliveryStrategy
{
public function deliver(Address $address):
{
$route = new BikeRoute($address);
echo $route->calculateCosts();
echo $route->calculateDeliveryTime();
}
}
class PizzaDelivery
{
public function deliverPizza(DeliveryStrategy $strategy, Address $address)
{
return $strategy->deliver($address);
}
}
$address = new Address('Meer en Vaart 300, Amsterdam');
$delivery = new PizzaDelivery();
$delivery->deliver(new BikeDelivery(), $address);
class DroneDelivery implements DeliveryStrategy
{
public function deliver(Address $address):
{
$route = new DroneRoute($address);
echo $route->calculateCosts();
echo $route->calculateFlyTime();
}
}
class PizzaDelivery
{
public function deliverPizza(DeliveryStrategy $strategy, Address $address)
{
return $strategy->deliver($address);
}
}
$address = new Address('Meer en Vaart 300, Amsterdam');
$delivery = new PizzaDelivery();
$delivery->deliver(new BikeDelivery(), $address);
$delivery->deliver(new DroneDelivery(), $address);
Strategy Pattern Laravel Example
<?php
namespace IlluminateContractsEncryption;
interface Encrypter
{
/**
* Encrypt the given value.
*
* @param string $value
* @param bool $serialize
* @return string
*/
public function encrypt($value, $serialize = true);
/**
* Decrypt the given value.
*
* @param string $payload
* @param bool $unserialize
* @return string
*/
public function decrypt($payload, $unserialize = true);
}
namespace IlluminateEncryption;
use IlluminateContractsEncryptionEncrypter as EncrypterContract;
class Encrypter implements EncrypterContract
{
/**
* Create a new encrypter instance.
*/
public function __construct($key, $cipher = 'AES-128-CBC')
{
$this->key = (string) $key;
$this->cipher = $cipher;
}
/**
* Encrypt the given value.
*/
public function encrypt($value, $serialize = true)
{
// Do the encrypt part and return the encrypted value
}
/**
* Decrypt the given value.
*/
public function decrypt($payload, $unserialize = true)
{
// Do the decrypt part and return the original value
}
}
use IlluminateContractsEncryptionEncrypter as EncrypterContract;
class MD5Encrypter implements EncrypterContract
{
const ENCRYPTION_KEY = 'qJB0rGtIn5UB1xG03efyCp';
/**
* Encrypt the given value.
*/
public function encrypt($value, $serialize = true)
{
return base64_encode(mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
md5(self::ENCRYPTION_KEY),
$value,
MCRYPT_MODE_CBC,
md5(md5(self::ENCRYPTION_KEY))
));
}
/**
* Decrypt the given value.
*/
public function decrypt($payload, $unserialize = true)
{
return rtrim(mcrypt_decrypt(
MCRYPT_RIJNDAEL_256,
md5(self::ENCRYPTION_KEY),
base64_decode($payload),
MCRYPT_MODE_CBC,
md5(md5(self::ENCRYPTION_KEY))
), "0");
}
}
Laravel Strategy Pattern Examples
IlluminateContractsAuthGaurd
IlluminateContractsCacheStore
IlluminateContractsEncryptionEncrypter
IlluminateContractsEventsDispatcher
IlluminateContractsHashingHasher
IlluminateContractsLoggingLog
IlluminateContractsSessionSession
IlluminateContractsTranslationLoader
Provider Pattern
Provider Pattern
Sets a pattern for providing some
essential service
Provider Pattern Example
class DominosServiceProvider extends ServiceProvider
{
public function register()
{
// Register your services here
}
}
use AppDominosPizzaManager;
class PizzaServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('pizza-manager', function ($app) {
return new PizzaManager();
});
}
}
// Using instantiation
$pizzaManager = new AppDominosPizzaManager();
// Using the provider pattern and the container
$pizzaManager = app('pizza-manager');
$margaritha = $pizzaManager->driver('margaritha');
$chicken = $pizzaManager->driver('chicken-pizza');
Provider Pattern Laravel Example
class DebugbarServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('debugbar', function ($app) {
$debugbar = new LaravelDebugbar($app);
if ($app->bound(SessionManager::class)) {
$sessionManager = $app->make(SessionManager::class);
$httpDriver = new SymfonyHttpDriver($sessionManager);
$debugbar->setHttpDriver($httpDriver);
}
return $debugbar;
});
}
}
Factory Pattern Recap
Factory translates interface to an instance
Builder Pattern Recap
Builder Pattern can do the configuration
for a new object without passing this
configuration to that object
Strategy Pattern Recap
Strategy pattern provides one place where
the correct strategy (algorithm) is called
Provider Pattern Recap
Provider Pattern let you extend the
framework by registering new classes in
the container
Wrapping up
Wrapping up
use AppDominosPizzaManager;
class PizzaServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('pizza-manager', function ($app) {
return new PizzaManager();
});
}
}
Design Pattern Fever
Want to learn more?
More Design Patterns: http://goo.gl/sHT3xK
Slideshare: https://goo.gl/U3YncS
Thank youBobby Bouwmann
@bobbybouwmann
Feedback: https://joind.in/talk/25af5
Thank youBobby Bouwmann
@bobbybouwmann
Feedback: https://joind.in/talk/25af5

More Related Content

What's hot

Validate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiValidate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation api
Raffaele Chiocca
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
Jennifer Estrada
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol
 
React JS
React JSReact JS
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
Abul Hasan
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
Sandi Barr
 
Sql Antipatterns Strike Back
Sql Antipatterns Strike BackSql Antipatterns Strike Back
Sql Antipatterns Strike Back
Karwin Software Solutions LLC
 
Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
Karwin Software Solutions LLC
 
Android-Tp5 : web services
Android-Tp5 : web servicesAndroid-Tp5 : web services
Android-Tp5 : web services
Lilia Sfaxi
 
Angular
AngularAngular
Angular
Lilia Sfaxi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
Rafael Dohms
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Nascenia IT
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
Piotr Paradziński
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
Tutorial: Implementing Specification-By-Example with Gherkin
Tutorial: Implementing Specification-By-Example with GherkinTutorial: Implementing Specification-By-Example with Gherkin
Tutorial: Implementing Specification-By-Example with Gherkin
Christian Hassa
 

What's hot (20)

Validate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiValidate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation api
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
React JS
React JSReact JS
React JS
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Sql Antipatterns Strike Back
Sql Antipatterns Strike BackSql Antipatterns Strike Back
Sql Antipatterns Strike Back
 
Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
 
Android-Tp5 : web services
Android-Tp5 : web servicesAndroid-Tp5 : web services
Android-Tp5 : web services
 
Angular
AngularAngular
Angular
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
Tutorial: Implementing Specification-By-Example with Gherkin
Tutorial: Implementing Specification-By-Example with GherkinTutorial: Implementing Specification-By-Example with Gherkin
Tutorial: Implementing Specification-By-Example with Gherkin
 

Similar to Laravel Design Patterns

Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UKKey Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
Max Pronko
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
Alexander Varwijk
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
Jonathan Wage
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
Denis Ristic
 
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
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injection
Josh Adell
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
Jeroen van Dijk
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
Vrann Tulika
 
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
 
Oop php 5
Oop php 5Oop php 5
Oop php 5
phpubl
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
James Casey
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
velveeta_512
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
Chris Tankersley
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
Kris Wallsmith
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Joaquim Rocha
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 Developer
Gary Hockin
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
Lars Jankowfsky
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Djangofool2nd
 

Similar to Laravel Design Patterns (20)

Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UKKey Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
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
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injection
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
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
 
Oop php 5
Oop php 5Oop php 5
Oop php 5
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 Developer
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 

Recently uploaded

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 

Recently uploaded (20)

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

Laravel Design Patterns

  • 2. About Me Bobby Bouwmann @bobbybouwmann Enrise -Code Cuisine markdownmail.com Laracasts
  • 3.
  • 6. What are Design Patterns?
  • 7. Design Patterns are ... Recipes for building maintainable code
  • 8. Be your own Chef Using the boundaries of Design Pattern
  • 9. Why use Design Patterns? — Provides a way to solve issues related to software development using a proven solution — Makes communication between developer more efficient
  • 10. Why use Design Patterns? — There is no"silver bullet"for using design pattern,each project and situation is different
  • 12. Maintainable Code — Understandable — Intuitive — Adaptable — Extendable — Debuggable
  • 13. Maintainable Code — Don't Repeat Yourself — Keep It Simple Stupid
  • 15. Design Pattern types — Creational Patterns — Structural Patterns — Behavioural Patterns
  • 16. Creational Patterns — Class instantiation — Hiding the creation logic — Examples: Factory,Builder,Prototype,Singleton
  • 17. Structural Patterns — Composition between objects — Usages of interfaces,abstract classes — Examples: Adapter,Bridge,Decorator,Facade,Proxy
  • 18. Behavioural Patterns — Communication between objects — Usages of mostly interfaces — Examples: Command,Iterator,Observer,State,Strategy
  • 19. Design Patterns in Laravel — Factory Pattern — Builder (Manager) Pattern — Strategy Pattern — Provider Pattern — Repository Pattern — Iterator Pattern — Singleton Pattern — Presenter Pattern
  • 20. Design Patterns in Laravel — Factory Pattern — Builder (Manager) Pattern — Strategy Pattern — Provider Pattern
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Factory Pattern Provides an interface for creating objects without specifying their concrete classes.
  • 27. Factory Pattern — Decouples code — Factory is responsible for creating objects,not the client — Multiple clients call the same factory,one place for changes — Easier to test,easy to mock and isolate
  • 28. Factory Pattern Example interface PizzaFactoryContract { public function make(array $toppings = []): Pizza; } class PizzaFactory implements PizzaFactoryContract { public function make(array $toppings = []): Pizza { return new Pizza($toppings); } } $pizza = (new PizzaFactory)->make(['chicken', 'onion']);
  • 29. Factory Pattern Laravel Example class PostsController { public function index(): IlluminateViewView { $posts = Post::all(); return view('posts.index', ['posts' => $posts]); } }
  • 30. // IlluminateFoundationhelpers.php /** * @return IlluminateViewView|IlluminateContractsViewFactory */ function view($view = null, $data = [], $mergeData = []) { $factory = app(ViewFactory::class); if (func_num_args() === 0) { return $factory; } return $factory->make($view, $data, $mergeData); }
  • 31. /** * Get the evaluated view contents for the given view. * * @return IlluminateContractsViewView */ public function make($view, $data = [], $mergeData = []) { $path = $this->finder->find( $view = $this->normalizeName($view) ); $data = array_merge($mergeData, $this->parseData($data)); return tap($this->viewInstance($view, $path, $data), function ($view) { $this->callCreator($view); }); } /** * Create a new view instance from the given arguments. * * @return IlluminateContractsViewView */ protected function viewInstance($view, $path, $data) { return new View($this, $this->getEngineFromPath($path), $view, $path, $data); }
  • 32. /** * Create a new Validator instance. * * @return IlluminateValidationValidator */ public function make(array $data, array $rules, array $messages = [], array $customAttributes = []) { $validator = $this->resolve( $data, $rules, $messages, $customAttributes ); $this->addExtensions($validator); return $validator; } /** * Resolve a new Validator instance. * * @return IlluminateValidationValidator */ protected function resolve(array $data, array $rules, array $messages, array $customAttributes) { if (is_null($this->resolver)) { return new Validator($this->translator, $data, $rules, $messages, $customAttributes); } return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes); }
  • 34.
  • 35.
  • 36.
  • 37. Builder (Manager) Pattern Builds complex objects step by step.It can return different objects based on the given data.
  • 38. Builder (Manager) Pattern — Decouples code — Focuses on building complex objects step by step and returns them — Has functionality to decide which objects should be returned
  • 39. Builder Pattern Example class PizzaBuilder { public function make(PizzaBuilderInterface $pizza): Pizza { // Returns object of type Pizza } }
  • 40. class PizzaBuilder { public function make(PizzaBuilderInterface $pizza): Pizza { $pizza->prepare(); $pizza->applyToppings(); $pizza->bake(); return $pizza; } }
  • 41. interface PizzaBuilderInterface { public function prepare(): Pizza; public function applyToppings(): Pizza; public function bake(): Pizza; }
  • 42. class MargarithaBuilder implements PizzaBuilderInterface { protected $pizza; public function prepare(): Pizza { $this->pizza = new Pizza(); return $this->pizza; } public function applyToppings(): Pizza { $this->pizza->setToppings(['cheese', 'tomato']); return $this->pizza; } public function bake(): Pizza { $this->pizza->setBakingTemperature(180); $this->pizza->setBakingMinutes(8); return $this->pizza; } }
  • 43. class PizzaBuilder { public function make(PizzaBuilderInterface $pizza): Pizza { return $pizza ->prepare(); ->applyToppings(); ->bake(); } } // Create a PizzaBuilder $pizzaBuilder = new PizzaBuilder; // Create pizza using Builder which returns Pizza instance $pizza = $pizzaBuilder->make(new MargarithaBuilder());
  • 44. class ChickenBuilder implements PizzaBuilderInterface { protected $pizza; public function prepare(): Pizza { $this->pizza = new Pizza(); return $this->pizza; } public function applyToppings(): Pizza { $this->pizza->setToppings(['cheese', 'tomato', 'chicken', 'corn']); return $this->pizza; } public function bake(): Pizza { $this->pizza->setBakingTemperature(200) $this->pizza->setBakingMinutes(8); return $this->pizza; } }
  • 45. class PizzaBuilder { public function make(PizzaBuilderInterface $pizza): Pizza { return $pizza ->prepare() ->applyToppings() ->bake(); } } $pizzaBuilder = new PizzaBuilder; $pizzaOne = $pizzaBuilder->make(new MargarithaBuilder()); $pizzaTwo = $pizzaBuilder->make(new ChickenBuilder());
  • 47. class PizzaManager extends IlluminateSupportManager { public function getDefaultDriver() { return 'margaritha'; } public function createMargarithaDriver(): PizzaBuilderInterface { return new MargarithaBuilder(); } public function createChickenPizzaDriver(): PizzaBuilderInterface { return new ChickenPizzaBuilder(); } }
  • 48. public function driver($driver = null) { $driver = $driver ?: $this->getDefaultDriver(); // If the given driver has not been created before, we will create the instances // here and cache it so we can return it next time very quickly. If there is // already a driver created by this name, we'll just return that instance. if (! isset($this->drivers[$driver])) { $this->drivers[$driver] = $this->createDriver($driver); } return $this->drivers[$driver]; }
  • 49. protected function createDriver($driver) { // We'll check to see if a creator method exists for the given driver. If not we // will check for a custom driver creator, which allows developers to create // drivers using their own customized driver creator Closure to create it. if (isset($this->customCreators[$driver])) { return $this->callCustomCreator($driver); } else { $method = 'create' . Str::studly($driver) . 'Driver'; if (method_exists($this, $method)) { return $this->$method(); } } throw new InvalidArgumentException("Driver [$driver] not supported."); }
  • 50. class PizzaManager extends IlluminateSupportManager { public function getDefaultDriver() { return 'margaritha'; } public function createMargarithaDriver(): PizzaBuilderInterface { return new MargarithaBuilder(); } } $manager = new PizzaManager(); // MargarithaBuilder implementing PizzaBuilderInterface $builder = $manager->driver('margaritha'); $pizza = $builder->make(); // Pizza
  • 51. // IlluminateMailTransportManager class TransportManager extends Manager { protected function createSmtpDriver() { // Code for building up a SmtpTransport class } protected function createMailgunDriver() { // Code for building up a MailgunTransport class } protected function createSparkpostDriver() { // Code for building up a SparkpostTransport class } protected function createLogDriver() { // Code for building up a LogTransport class } }
  • 52. class TransportManager extends Manager { public function getDefaultDriver() { return $this->app['config']['mail.driver']; } // All create{$driver}Driver methods }
  • 53. // config/mail.php 'driver' => env('MAIL_DRIVER', 'smtp'),
  • 54. // config/mail.php 'driver' => env('MAIL_DRIVER', 'smtp'), // IlluminateMailTransportManager createSmtpDriver()
  • 55. // config/mail.php 'driver' => env('MAIL_DRIVER', 'smtp'), // IlluminateMailTransportManager createSmtpDriver() // app/Http/Controllers/RegisterController Mail::to($user) ->send(new UserRegistered($user));
  • 56. Laravel Manager Pattern Examples IlluminateAuthAuthManager IlluminateBroadcastingBroadcastManager IlluminateCacheCacheManager IlluminateFilesystemFilesystemManager IlluminateMailTransportManager IlluminateNotificationsChannelManager IlluminateQueueQueueManager IlluminateSessionSessionManager
  • 58.
  • 59.
  • 60.
  • 61.
  • 62. Strategy Pattern Defines a familiy of algorithms that are interchangeable
  • 63. Strategy Pattern Program to an interface, not an implementation.
  • 64. Strategy Pattern Example interface DeliveryStrategy { public function deliver(Address $address); }
  • 65. interface DeliveryStrategy { public function deliver(Address $address); } class BikeDelivery implements DeliveryStrategy { public function deliver(Address $address): { $route = new BikeRoute($address); echo $route->calculateCosts(); echo $route->calculateDeliveryTime(); } }
  • 66. class PizzaDelivery { public function deliverPizza(DeliveryStrategy $strategy, Address $address) { return $strategy->deliver($address); } } $address = new Address('Meer en Vaart 300, Amsterdam'); $delivery = new PizzaDelivery(); $delivery->deliver(new BikeDelivery(), $address);
  • 67. class DroneDelivery implements DeliveryStrategy { public function deliver(Address $address): { $route = new DroneRoute($address); echo $route->calculateCosts(); echo $route->calculateFlyTime(); } }
  • 68. class PizzaDelivery { public function deliverPizza(DeliveryStrategy $strategy, Address $address) { return $strategy->deliver($address); } } $address = new Address('Meer en Vaart 300, Amsterdam'); $delivery = new PizzaDelivery(); $delivery->deliver(new BikeDelivery(), $address); $delivery->deliver(new DroneDelivery(), $address);
  • 70. <?php namespace IlluminateContractsEncryption; interface Encrypter { /** * Encrypt the given value. * * @param string $value * @param bool $serialize * @return string */ public function encrypt($value, $serialize = true); /** * Decrypt the given value. * * @param string $payload * @param bool $unserialize * @return string */ public function decrypt($payload, $unserialize = true); }
  • 71. namespace IlluminateEncryption; use IlluminateContractsEncryptionEncrypter as EncrypterContract; class Encrypter implements EncrypterContract { /** * Create a new encrypter instance. */ public function __construct($key, $cipher = 'AES-128-CBC') { $this->key = (string) $key; $this->cipher = $cipher; } /** * Encrypt the given value. */ public function encrypt($value, $serialize = true) { // Do the encrypt part and return the encrypted value } /** * Decrypt the given value. */ public function decrypt($payload, $unserialize = true) { // Do the decrypt part and return the original value } }
  • 72. use IlluminateContractsEncryptionEncrypter as EncrypterContract; class MD5Encrypter implements EncrypterContract { const ENCRYPTION_KEY = 'qJB0rGtIn5UB1xG03efyCp'; /** * Encrypt the given value. */ public function encrypt($value, $serialize = true) { return base64_encode(mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5(self::ENCRYPTION_KEY), $value, MCRYPT_MODE_CBC, md5(md5(self::ENCRYPTION_KEY)) )); } /** * Decrypt the given value. */ public function decrypt($payload, $unserialize = true) { return rtrim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5(self::ENCRYPTION_KEY), base64_decode($payload), MCRYPT_MODE_CBC, md5(md5(self::ENCRYPTION_KEY)) ), "0"); } }
  • 73. Laravel Strategy Pattern Examples IlluminateContractsAuthGaurd IlluminateContractsCacheStore IlluminateContractsEncryptionEncrypter IlluminateContractsEventsDispatcher IlluminateContractsHashingHasher IlluminateContractsLoggingLog IlluminateContractsSessionSession IlluminateContractsTranslationLoader
  • 75.
  • 76.
  • 77. Provider Pattern Sets a pattern for providing some essential service
  • 78. Provider Pattern Example class DominosServiceProvider extends ServiceProvider { public function register() { // Register your services here } }
  • 79. use AppDominosPizzaManager; class PizzaServiceProvider extends ServiceProvider { public function register() { $this->app->bind('pizza-manager', function ($app) { return new PizzaManager(); }); } }
  • 80. // Using instantiation $pizzaManager = new AppDominosPizzaManager(); // Using the provider pattern and the container $pizzaManager = app('pizza-manager'); $margaritha = $pizzaManager->driver('margaritha'); $chicken = $pizzaManager->driver('chicken-pizza');
  • 82. class DebugbarServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('debugbar', function ($app) { $debugbar = new LaravelDebugbar($app); if ($app->bound(SessionManager::class)) { $sessionManager = $app->make(SessionManager::class); $httpDriver = new SymfonyHttpDriver($sessionManager); $debugbar->setHttpDriver($httpDriver); } return $debugbar; }); } }
  • 83. Factory Pattern Recap Factory translates interface to an instance
  • 84. Builder Pattern Recap Builder Pattern can do the configuration for a new object without passing this configuration to that object
  • 85. Strategy Pattern Recap Strategy pattern provides one place where the correct strategy (algorithm) is called
  • 86. Provider Pattern Recap Provider Pattern let you extend the framework by registering new classes in the container
  • 88. Wrapping up use AppDominosPizzaManager; class PizzaServiceProvider extends ServiceProvider { public function register() { $this->app->bind('pizza-manager', function ($app) { return new PizzaManager(); }); } }
  • 90. Want to learn more? More Design Patterns: http://goo.gl/sHT3xK Slideshare: https://goo.gl/U3YncS