Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns in PHP Applications

Design Patterns in PHP Applications

A brief, subjective, opinionated and probably not precise review of design patterns used in PHP software. Presented at Inviqa Enlightening Lunch.

Arturas Smorgun

June 12, 2014
Tweet

More Decks by Arturas Smorgun

Other Decks in Programming

Transcript

  1. POPULAR IN PHP DEPENDENCY INJECTION (STRUCTURAL), FRONT CONTROLLER (ARCHITECTURAL), MODEL

    VIEW CONTROLLER (ARCHITECTURAL), FACTORY (CREATIONAL), SINGLETON (CREATIONAL), OBSERVER (BEHAVIOURAL).
  2. CONTROLLER THIN MID LAYER BETWEEN MODEL AND VIEW: 1. ACCEPTS

    INPUT 2. EXECUTES COMMANDS ON MODEL 3. GENERATES OUTPUT
  3. SINGLETON CREATIONAL (ANTI)PATTERN. ENSURES A CLASS HAS ONLY ONE INSTANCE.

    PROVIDES A GLOBAL POINT OF ACCESS TO IT. AVOID AT ANY COST!
  4. WHY IT’S SO BAD? GLOBAL INSTANCE - HIDING DEPENDENCIES; USUALLY

    LEADS TO TIGHT COUPLING; USUALLY CARRIES AROUND GLOBAL STATE; STATIC CALL - HARD TO MOCK.
  5. ALTERNATIVES? USE A FACTORY INSTEAD; INSTANTIATE AT THE TOP OF

    APP PASS TO COMPONENTS VIA DEPENDENCY INJECTION.
  6. DECORATOR MAINTAINS A REFERENCE TO A COMPONENT OBJECT; DEFINES AN

    INTERFACE THAT CONFORMS TO COMPONENT’S INTERFACE.
  7. CONTEXT IS CONFIGURED WITH A CONCRETE STRATEGY OBJECT; MAINTAINS A

    REFERENCE TO A STRATEGY OBJECT; DEFINES INTERFACE FOR STRATEGY TO ACCESS ITS DATA (OPTIONAL).
  8. DESIGN PATTERNS ARE NOT THAT WIDE SPREAD. MANY FRAMEWORKS MAKE

    USE OF LIMITED SUBSET OF THEM. THERE ARE ALSO N WAYS TO IMPLEMENT DESIGN PATTERN.
  9. THERE IS ALTERNATIVE TO EVERYTHING. BE AWARE OF YOUR OPTIONS

    AND KNOW WHERE TO LOOK IT UP WHEN NEEDED.
  10. KNOWING ANTIPATTERNS IS IMPORTANT TO RAISE AWARENESS AND NOT TO

    FALL INTO THE SAME TRAP AS PROCEEDERS DID.