nanos gigantium humeris insidentes (design patterns inside symfony 2)

57
DESIGN PATTERNS INSIDE SYMFONY2 Nanos gigantium humeris insidentes

Upload: giorgio-cefaro

Post on 18-May-2015

3.113 views

Category:

Technology


2 download

DESCRIPTION

Talk presented by me and Giulio De Donato at SymfonyDayIt 2013

TRANSCRIPT

Page 1: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DESIGN PATTERNS INSIDE SYMFONY2

Nanos gigantium humeris insidentes

Page 2: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

GIANTS

Page 3: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

liurrrgio

GIORGIO CEFARO

@giorrrgio

GIULIO DE DONATO

@liuggio

Page 4: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DESIGN PATTERN?

Page 5: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

A solution of a problem in a context

James O. Coplien (1994)

DESIGN PATTERN

Page 6: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

“ For me a pattern is primarily a way to chunk up

advice about a topic “

DESIGN PATTERN

Page 7: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

#1

Page 8: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

A controller that handles all requests for a Web site

FRONT CONTROLLER

Page 9: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FRONT CONTROLLER

web/app.php

Page 10: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FRONT CONTROLLER ... and ?

web/app.php

Page 11: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Attach additional responsibilities to an object dynamically.

Decorators provide a flexible alternative to subclassing for

extending functionality.

DECORATOR

Page 12: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DECORATORSymfony\Component\HttpKernel\HttpCache\HttpCache

Page 13: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

U DUNNO HttpKernelInterface?

Symfony\Component\HttpKernel\HttpKernelInterface

Page 14: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

KERNEL AND SERVICE CONTAINER

Symfony\Component\HttpKernel\Kernel

Page 15: Nanos gigantium humeris insidentes (design patterns inside symfony 2)
Page 16: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INVERSION OF CONTROL

INTENT:Inversion of control is a style

of software construction where reusable code controls the execution

of problem-specific code

Page 17: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

1988INVERSION OF CONTROL

Page 18: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

“ Inversion of control is a common characteristic of frameworks,

so saying that these lightweight containers are special because they use inversion of

control is like saying my car is special because it has wheels.”

Page 19: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

“ DON’T CALL US WE’LL CALL YOU ”

INVERSION OF CONTROL

HOLLYWOOD PRINCIPLE:

Page 20: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INVERSION OF CONTROL

Page 21: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

"Dependency Injection is where components are given their dependencies through their

constructors, methods, or directly into fields."

PICO CONTAINER DEFINITION:

Page 22: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

Dependency injection allows the removal of hard-coded dependencies

and makes it possible to change them, whether at run-time or compile-

time.

Page 23: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INJECTING A TRANSPORT INSIDE A MAILER

Page 24: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

- FIGHT OF THE DAY -

SERVICE CONTAINERvs

DEPENDENCY INJECTION

Page 25: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INVERSION OF CONTROL

INTENT:

DECOUPLEDECOUPLEDECOUPLE

DECOUPLE

DECOUPLEDECOUPLE

DECOUPLE

DECOUPLE

DECOUPLEDECOUPLEDECOUPLE

Page 26: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

??

DEPENDENCY INJECTION AND FRAMEWORKS

Grafico con spaghetti, infermiera che fa la puntura, lista della spesa per l'antipattern service locator, qualcosa di galattico per l'enhanced injection (tipo pulp fiction)

DI

DIC

http://www.loosecouplings.com/2011/01/dependency-injection-using-di-container.html

Page 27: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Define an interface for creating an object, but let subclasses decide which class

to instantiate. Factory Method lets a class defer instantiation to

subclasses.

FACTORY METHOD

Page 28: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FACTORY METHOD - DO IT WITH THE DIC

Page 29: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Provide an interface for creating families of related or dependent

objects withoutspecifying their concrete classes.

ABSTRACT FACTORY

Page 30: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ABSTRACT FACTORY - SecurityFactoryInterface

Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface

Page 31: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Separate the construction of a complex object from its

representation so that the same construction process can create

different representations.

BUILDER

Page 32: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FORM BUILDER

Symfony\Component\Form\FormBuilderInterface

Page 33: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FORM BUILDER

Page 34: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Provide a surrogate or placeholder for another

object to control access to it.

PROXY

Page 35: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DOCTRINE PROXY

app/cache/doctrine/odm/Proxies/ ...

Page 36: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their

interaction independently.

MEDIATOR

Page 37: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

MEDIATOR

RouterListener

Kernel :: REQUEST EVENT DISPATCHER

SessionListener

FirewallLocaleListener

ProfilerListener

Fragm

entLi

stene

r

Page 38: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Defines a family of algorithms, encapsulate each one, and make them interchangeable.

Strategy lets the algorithm vary independently from clients that use it.

STRATEGY aka POLICY

Page 39: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

FragmentRendererInterface

Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface

Page 40: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

Fragment Renderers

Symfony\Component\HttpKernel\Fragment\...

Page 41: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper

itself.

DATA MAPPER

Page 42: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

INTENT:

Represent an operation to be performed on the elements of an object structure.

Visitor lets you define a new operation without changing the classes of the elements on which

it operates.

VISITOR

Page 43: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

VALIDATION VISITOR

Symfony\Component\Validator\..

Page 44: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

MVC

Controller

ViewModel

Page 45: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI-PATTERNANTI-PATTERNANTI-PATTERN

AbstractionInversion, AccidentalComplexity, AccidentalInclusion, AddingEpicycles, AlcoholFueledDevelopment, AmbiguousViewpoint, AnalogyBreakdownAntiPattern, AnalysisParalysis, AnAthena, AppointedTeam, ArchitectsDontCode, ArchitectsPlayGolf, ArchitectureAsRequirements, ArchitectureByImplication, AsynchronousUnitTesting, AutogeneratedStovepipeAntiPattern, BandAid, BearTrap?, BigBallOfMud, BlameStorming, BlowhardJamboree, BoatAnchor, CargoCult, CarParkSyndrome, CascadingDialogBoxesAntiPattern, ContinuousObsolescence, ConfusionOfObjectives, ControlFreak, CopyAndPasteProgramming, CornCob, CoverYourAssets, CreepingFeaturitis, CrciCards, CryptoCracy, DeadEnd, DeathByPlanning, DecisionByArithmetic, DesignByCommittee, DesignForTheSakeOfDesign., DiscordantRewardMechanisms, DoerAndKnower, DryWaterhole, EgalitarianCompensation, EmailIsDangerous, EmperorsNewClothes, EmpireBuilding, ExceptionFunnel, FalseEconomy, FalseSurrogateEndpoint, FearOfSuccess, FireDrill, FloatingPointCurrency, FloatingPointFractions, FoolTrap, FunctionalDecomposition, FungibleTeams, FungibleProjectManager, GeographicallyDistributedDevelopment, GiveMeEstimatesNow, GlassWall, GodClass, GoldenHammer, GroundHogDayProject, HalfDoneIsEnough, HeirApparent, HeroCulture, HiddenRequirements, IdiotProofProcess., IfItIsWorkingDontChange, IfOk, InappropriateTechnicalObjective, InputKludge, ItsAnOperatorProblem, JobKeeper, JumbleAntipattern, JunkyardCoding, KillTwoBirdsWithOneStone, KitchenSinkDesign, LavaFlow, LeadingRequest, MagicContainer, ManagerControlsProcess, MushroomManagement, Nationalism, NetNegativeProducingProgrammer, NotInventedHere, NotTheAppropriateProtocol, NullFlag, OverGeneralizationOfBusinessLogic, OverUseOfPatterns, PathOfLeastResistance, ParsingHtmlWithRegex, PassingNullsToConstructors, PlugCompatibleInterchangeableEngineers, PoliticsOrientedArchitecture, PolterGeists, ReinventingTheWheel, ReinventTheWheel, RollYourOwnDatabase, RequirementsTossedOverTheWall, RubeGoldbergMachine, ScapeGoat, SeagullManagement, SecretSociety, SellingaProductYouCantRealize, ShootTheMessenger, SingleFunctionExitPoint, SmokeAndMirrors, SoftwareMerger, SpaghettiCode, SpecifyNothing, StandingOnTheShouldersOfMidgets, StovepipeAntiPattern, StringWithoutLength, SumoMarriage, SweepItUnderTheRugAntiPattern, SwissArmyKnife, ThatsNotReallyAnIssue, TheBlob, TheCustomersAreIdiots, TheGrandOldDukeOfYork, TheProcessIsTheDeliverable, TheyUnderstoodMe, ThrownOverTheWall, TowerOfVoodoo, TrainTheTrainer, UntestedButFinished, VendorLockIn, ViewgraphEngineering, VietnamWarAntiPattern, VoodooChickenCoding, WalkingThroughaMineField, WarmBodies, WeAreIdiots, WolfTicket, YetAnotherMeetingWillSolveIt, YetAnotherThreadWillSolveIt, YetAnotherProgrammer, ZeroMeansNull

Page 46: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI PATTERNS

DESIGNFOR THE SAKE OF DESIGN

“I only ship the perfection"

Page 47: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI PATTERNS

OVER ENGINEERING

OVERUSE OF PATTERNS

Page 48: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI PATTERNS

THE BLOB

BIG BALL OF MUD

Page 49: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI PATTERNS

GLOBALIZING

STANDING ON THE SHOULDER OF THE MIDGETS

Page 50: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

ANTI PATTERNS

REINVENTING THE WHEEL

NIH SYNDROME

Page 51: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

SPAGHETTI CODESPAGHETTI WITH MEATBALL

LASAGNA CODERAVIOLI CODE

RAVIOLI CODE

Page 52: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

code spends more time being read and maintained than

being created

Page 53: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DECOUPLE

Page 54: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DECOUPLE

Page 55: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

DECOUPLE.

Page 56: Nanos gigantium humeris insidentes (design patterns inside symfony 2)

GOOD QUESTIONS ARE BETTER THANGOOD ANSWERS