1 / 27

Design patterns in PHP - Your time is more valuable than your server’s

Programming is an artisanal trade and thus your output is maximally bound by your available time. By covering the well know (but underused) classic “Gang of Four” Design Patterns from a PHP perspective, I intend to provide a way for you to get the most out of your limited time. Design Patterns are generalized, reusable solutions to common recurring design challenges in Object Orientated programming. They essentially provide you and your team with larger abstractions with which to solve problems – thereby allowing you to more rapidly solve more complex problems. Design Patterns also help avoid subtle design issues that may not be apparent until the development of a system is well underway (and the design can no longer be easily changed). This leads to more extensible and maintainable systems. By correctly applying Design Patterns, you generally end up achieving most of the SOLID principles by mistake… My talk will focus on using a subset of the 23 Design Patterns in PHP and how they can provide some ingenious solutions to problems we regularly solve in both web-based (MVC) and CLI PHP environments. I will highlight PHP-specific language features that facilitate the patterns as well as provide code snippets to help make these highly abstract ideas more accessible. I intend to bridge everyone from UML to useful code. This is a hardcore programming talk.

bradm
Download Presentation

Design patterns in PHP - Your time is more valuable than your server’s

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design patterns in PHP Your time is more valuable than your server’s PHP South Africa 2017 Brad Mostert /bsinkwa /mostertb

  2. Gang of Four Patterns Generalized, Reusable solutions to commonly Recurring design challenges See: ACM OOPSLA ‘99 (Object-Oriented Programming, Systems, Languages & Applications) Show Trial for Crimes Against Computer Science

  3. The Design Patterns Behavioral Structural Creational • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor • Adaptor • Bridge • Composite • Decorator • Facade • Flyweight • Proxy • Abstract Factory • Builder • Factory Method • Prototype • Singleton

  4. The Design Patterns Behavioral Structural Creational • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • TemplateMethod • Visitor • Adaptor • Bridge • Composite • Decorator • Facade • Flyweight • Proxy • Abstract Factory • Builder • Factory Method • Prototype • Singleton

  5. WHO AM I Senior developer at Afrihost PHP Joburg Organizer Server Shepard CS Graduate Gave the Advanced Composer workshop on Wednesday

  6. UML Class Diagrams Class Name Properties Methods

  7. Singleton Benefits • Single Instantiation System Wide • Lazy Instantiation • Global Access to the Single Instance Examples • Limited external resource • (DB Connection Pool) • Dependancy Injection Containers • ‘Heavy’ Objects (Framework kernel)

  8. Singleton: PHP Extra Credit: Late Static Binding

  9. Template method SO MUCH CODE REUSE! MUCH CLARITY! WOW!

  10. Template method: Benefits Benefits Examples Business Process: Signup logic Check Product Availability Create new product records Bill Queue external setup Send welcome notification Sort, Pagination, Calculation ASC/DESC : Comparison function Processors for polymorphic input or output • One single algorithm defined in one place • Interface is easily enforced • Code Reuse: Only implement the differences – save typing

  11. Template method: PHP AbstractClass : Contract ConcreteClass : Implementation

  12. Template method: PHP Add Functionality Nest Template Methods

  13. Strategy

  14. Strategy: Benefits Benefits Examples Data structure algorithms Calculation functions User-selectable functionality • Maximize cohesion and minimize coupling (open-closed principle) • Fewer breaking changes • Object is easier to reuse than a function

  15. Strategy: PHP Switch out the algorithm by instantiating a different object

  16. State: Centralized Orchestration

  17. State: DE-Centralized Orchestration

  18. State: Benefits Benefits Examples Coding anything that can naturally be expressed as a State Machine or Flow Diagram Troubleshooter CLI Menu System Turn-based Game Vending Machine Processing most things that have a ‘status’ field in the DB • Easy to add new states • Avoid thousand line ‘God Class’ • Easier to find and read code that applies to particular state* HTTP is stateless so persist the state object or context to DB

  19. composite // extra logic in here Each child could itself be a composite instead of a leaf

  20. Composite: Benefits Benefits Examples Bundled Products Permissions assigned to Users and/or User Groups Nested Menu items • Treat a collection of items the same as a single item • Iteration code in one place • No explicit tree traversal for deep hierarchies • Implicit recursion

  21. Factory method Common Interface to return something lower in the hierarchy

  22. Composite: Benefits Benefits Examples Practical example on next slide Static method on base class that returns derived class (‘Virtual Constructor’) Hide complex object creation logic • User doesn’t need to know which subclass of hierarchy they are working with • Complex object creation logic is trivial to reuse • Could reuse existing object instead of creating new one (think thread pool)

  23. Factory method: Example Some logic common to all Template Method Many Subclasses (shared, cloud, dedicated, etc.) Hide giant switch statement down here

  24. Factory method: Example Extra Credit: Abstract Factory

  25. Questions? PHP South Africa 2017 Brad Mostert /bsinkwa /mostertb

  26. https://joind.in/talk/3b79a /bsinkwa /mostertb

  27. phpsa2017profiles.co.za /bsinkwa /mostertb

More Related