51
Software Design Patterns in Laravel 4 by Phill Sparks

Software design patterns in laravel by phill sparks

Embed Size (px)

Citation preview

Software Design Patternsin Laravel 4

by Phill Sparks

Credits

• Rosalind Goodall - Graphics

• Taylor Otwell - Laravel

@PhillSparks

@PhillSparks

• API Engineer at CrowdLab

@PhillSparks

• API Engineer at CrowdLab

• From Leicester, UK

@PhillSparks

• API Engineer at CrowdLab

• From Leicester, UK

• Climbing Instructor

@PhillSparks

• API Engineer at CrowdLab

• From Leicester, UK

• Climbing Instructor

•Scout

@PhillSparks

• API Engineer at CrowdLab

• From Leicester, UK

• Climbing Instructor

•Scout

• Laravel Core Team

Laravel

• With Laravel since 1.5.9

• Core Team Member

• Past: Quality Team

• Now: Community engagement and support

Laravel

• With Laravel since 1.5.9

• Core Team Member

• Past: Quality Team

• Now: Community engagement and support

• Dayle still owes me 9000 support points!

Software Design PatternsEach pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.

-- Christopher Alexander, AIS+77

“ ”

Software Design Patterns

Software Design Patterns

The Patterns

• Builder

• Chain of Responsibility

• Command

• Facade

• Factory

• Iterator

• Mediator

• Observer

• Presenter

• Repository

• Singleton

• Strategy

Notations

ClassNotations

Sub-classNotations

Abstract ClassNotations

Class InstanceNotations

ImplementationNotations

Building Blocks

Interfaces

namespace Illuminate\Auth;

interface UserInterface {

public function getAuthIdentifier();

public function getAuthPassword();

}

class GenericUser implements UserInterface {

• 244 interfaces in Laravel (including vendors)

• 25 from Laravel Core

• 65 from Symfony

• 73 from Swiftmailer

Interface

Abstract Classes

namespace Illuminate\Database\Eloquent\Relations;

abstract class Relation {

abstract public function addConstraints();

abstract public function getResults();

}

class BelongsTo extends Relation {

Abstract Classes

• 128 abstract classes in Laravel (including vendors)

• 14 from Laravel Core

• 15 from Swiftmailer

• 40 from Symfony

Iterator

PHP’s Iterator Interface

Laravel’s Iterators

• Collection

• Paginator

Observer

Dispatchernamespace Illuminate\Events;

Dispatchernamespace Illuminate\Events;

class LoginHandler {

function handle($user) {

// do something with $data

}

}

Event::listen(‘user.login’, ‘LoginHandler’);

Event::fire(‘user.login’, $user);

Singleton

AliasLoadernamespace Illuminate\Foundation;

Multiton

DatabaseManagernamespace Illuminate\Database;

Facade

Facadenamespace Illuminate\Support\Facades;

Inputnamespace Illuminate\Support\Facades;

Routenamespace Illuminate\Support\Facades;

Responsenamespace Illuminate\Support\Facades;

Strnamespace Illuminate\Support;

Facade vs Singleton

Decorator

BootstrapPresenternamespace Illuminate\Pagination;

BootstrapPresenternamespace Illuminate\Pagination;

Repository

• Examples:

• Illuminate\Cache\Repository

• Illuminate\Config\Repository

Builder (aka Manager)

• Examples:

• Illuminate\Auth\AuthManager

• Illuminate\Cache\CacheManager

• Illuminate\Queue\QueueManager

• Illuminate\Session\SessionManager

Factory

• Examples:

• Illuminate\Database\DatabaseManager

• Illuminate\Database\Connectors\ ConnectionFactory

• Illuminate\Validation\Factory

Service Provider

• Core to Laravel’s IoC

• See Illuminate\Support\ServiceProvider

• Examples:

• Illuminate\Auth\AuthServiceProvider

• Illuminate\Hash\HashServiceProvider

• Illuminate\Log\LogServiceProvider

Strategy

• Examples:

• Illuminate\Cache\StoreInterface

• Illuminate\Config\LoaderInterface

• Illuminate\Database - Builders & Grammars

• Illuminate\Translation\LoaderInterface

• Illuminate\View\ViewFinderInterface

s/PhillSparks//