SlideShare uma empresa Scribd logo
1 de 92
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Spring security oauth2
Spring security oauth2Spring security oauth2
Spring security oauth2axykim00
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationWebStackAcademy
 
Decoding Web Accessibility through Testing - Anuradha Kumari
Decoding Web Accessibility through Testing  - Anuradha KumariDecoding Web Accessibility through Testing  - Anuradha Kumari
Decoding Web Accessibility through Testing - Anuradha KumariWey Wey Web
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keywordPham Huy Tung
 
Object Oriented Programming with Laravel - Session 1
Object Oriented Programming with Laravel - Session 1Object Oriented Programming with Laravel - Session 1
Object Oriented Programming with Laravel - Session 1Shahrzad Peyman
 
아라한사의 스프링 시큐리티 정리
아라한사의 스프링 시큐리티 정리아라한사의 스프링 시큐리티 정리
아라한사의 스프링 시큐리티 정리라한사 아
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksPhill Sparks
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해beom kyun choi
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8José Paumard
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Connecting Connect with Spring Boot
Connecting Connect with Spring BootConnecting Connect with Spring Boot
Connecting Connect with Spring BootVincent Kok
 

Mais procurados (20)

Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Spring security oauth2
Spring security oauth2Spring security oauth2
Spring security oauth2
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Decoding Web Accessibility through Testing - Anuradha Kumari
Decoding Web Accessibility through Testing  - Anuradha KumariDecoding Web Accessibility through Testing  - Anuradha Kumari
Decoding Web Accessibility through Testing - Anuradha Kumari
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keyword
 
Object Oriented Programming with Laravel - Session 1
Object Oriented Programming with Laravel - Session 1Object Oriented Programming with Laravel - Session 1
Object Oriented Programming with Laravel - Session 1
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Parallel streams in java 8
Parallel streams in java 8Parallel streams in java 8
Parallel streams in java 8
 
아라한사의 스프링 시큐리티 정리
아라한사의 스프링 시큐리티 정리아라한사의 스프링 시큐리티 정리
아라한사의 스프링 시큐리티 정리
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Android presentation - Gradle ++
Android presentation - Gradle ++Android presentation - Gradle ++
Android presentation - Gradle ++
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill Sparks
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Connecting Connect with Spring Boot
Connecting Connect with Spring BootConnecting Connect with Spring Boot
Connecting Connect with Spring Boot
 

Semelhante a 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 UKMax Pronko
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboardsDenis 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 CodeSWIFTotter Solutions
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injectionJosh Adell
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowVrann 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 scenariosDivante
 
Oop php 5
Oop php 5Oop php 5
Oop php 5phpubl
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames 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 Widgetsvelveeta_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 ManagerChris Tankersley
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJoaquim Rocha
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperGary Hockin
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Djangofool2nd
 

Semelhante a 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
 

Último

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.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
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
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
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
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Último (20)

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 ...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
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...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
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
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 

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