Design pattern in Symfony2 - Nanos gigantium humeris insidentes

57
DESIGN PATTERN INSIDE SYMFONY2 Nanos gigantium humeris insidentes

description

Giorgio Cefaro and Giulio De Donato - Symfony day in Rome 2014.

Transcript of Design pattern in Symfony2 - Nanos gigantium humeris insidentes

Page 1: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DESIGN PATTERN INSIDE SYMFONY2

Nanos gigantium humeris insidentes

Page 2: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

GIANTS

Page 3: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

liurrrgio

GIORGIO CEFARO

@giorrrgio

GIULIO DE DONATO

@liuggio

Page 4: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DESIGN PATTERN?

Page 5: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

A solution of a problem in a context

James O. Coplien (1994)

DESIGN PATTERN

Page 6: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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

advice about a topic “

DESIGN PATTERN

Page 7: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

#1

Page 8: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INTENT:

A controller that handles all requests for a Web site

FRONT CONTROLLER

Page 9: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FRONT CONTROLLER

web/app.php

Page 10: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FRONT CONTROLLER ... and ?

web/app.php

Page 11: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INTENT:

Attach additional responsibilities to an object dynamically.

Decorators provide a flexible alternative to subclassing for

extending functionality.

DECORATOR

Page 12: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DECORATORSymfony\Component\HttpKernel\HttpCache\HttpCache

Page 13: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

U DUNNO HttpKernelInterface?

Symfony\Component\HttpKernel\HttpKernelInterface

Page 14: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

KERNEL AND SERVICE CONTAINER

Symfony\Component\HttpKernel\Kernel

Page 15: Design pattern in Symfony2 - Nanos gigantium humeris insidentes
Page 16: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

1988INVERSION OF CONTROL

Page 18: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

“ 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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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

INVERSION OF CONTROL

HOLLYWOOD PRINCIPLE:

Page 20: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INVERSION OF CONTROL

Page 21: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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

constructors, methods, or directly into fields."

PICO CONTAINER DEFINITION:

Page 22: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INJECTING A TRANSPORT INSIDE A MAILER

Page 24: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

- FIGHT OF THE DAY -

SERVICE CONTAINERvs

DEPENDENCY INJECTION

Page 25: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INVERSION OF CONTROL

INTENT:

DECOUPLEDECOUPLEDECOUPLE

DECOUPLE

DECOUPLEDECOUPLE

DECOUPLE

DECOUPLE

DECOUPLEDECOUPLEDECOUPLE

Page 26: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

??

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FACTORY METHOD - DO IT WITH THE DIC

Page 29: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INTENT:

Provide an interface for creating families of related or dependent

objects withoutspecifying their concrete classes.

ABSTRACT FACTORY

Page 30: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ABSTRACT FACTORY - SecurityFactoryInterface

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

Page 31: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INTENT:

Separate the construction of a complex object from its

representation so that the same construction process can create

different representations.

BUILDER

Page 32: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FORM BUILDER

Symfony\Component\Form\FormBuilderInterface

Page 33: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FORM BUILDER

Page 34: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

INTENT:

Provide a surrogate or placeholder for another

object to control access to it.

PROXY

Page 35: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DOCTRINE PROXY

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

Page 36: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

MEDIATOR

RouterListener

Kernel :: REQUEST EVENT DISPATCHER

SessionListener

FirewallLocaleListener

ProfilerListener

Fragm

entLi

stene

r

Page 38: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

FragmentRendererInterface

Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface

Page 40: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

Fragment Renderers

Symfony\Component\HttpKernel\Fragment\...

Page 41: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

VALIDATION VISITOR

Symfony\Component\Validator\..

Page 44: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

MVC

Controller

ViewModel

Page 45: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

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: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ANTI PATTERNS

DESIGNFOR THE SAKE OF DESIGN

“I only ship the perfection"

Page 47: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ANTI PATTERNS

OVER ENGINEERING

OVERUSE OF PATTERNS

Page 48: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ANTI PATTERNS

THE BLOB

BIG BALL OF MUD

Page 49: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ANTI PATTERNS

GLOBALIZING

STANDING ON THE SHOULDER OF THE MIDGETS

Page 50: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

ANTI PATTERNS

REINVENTING THE WHEEL

NIH SYNDROME

Page 51: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

SPAGHETTI CODESPAGHETTI WITH MEATBALL

LASAGNA CODERAVIOLI CODE

RAVIOLI CODE

Page 52: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

code spends more time being read and maintained than

being created

Page 53: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DECOUPLE

Page 54: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DECOUPLE

Page 55: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

DECOUPLE.

Page 56: Design pattern in Symfony2 - Nanos gigantium humeris insidentes

GOOD QUESTIONS ARE BETTER THANGOOD ANSWERS