310 likes | 423 Views
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.
E N D
Design patterns in PHP Your time is more valuable than your server’s PHP South Africa 2017 Brad Mostert /bsinkwa /mostertb
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
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
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
WHO AM I Senior developer at Afrihost PHP Joburg Organizer Server Shepard CS Graduate Gave the Advanced Composer workshop on Wednesday
UML Class Diagrams Class Name Properties Methods
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)
Singleton: PHP Extra Credit: Late Static Binding
Template method SO MUCH CODE REUSE! MUCH CLARITY! WOW!
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
Template method: PHP AbstractClass : Contract ConcreteClass : Implementation
Template method: PHP Add Functionality Nest Template Methods
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
Strategy: PHP Switch out the algorithm by instantiating a different object
State: Centralized Orchestration
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
composite // extra logic in here Each child could itself be a composite instead of a leaf
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
Factory method Common Interface to return something lower in the hierarchy
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)
Factory method: Example Some logic common to all Template Method Many Subclasses (shared, cloud, dedicated, etc.) Hide giant switch statement down here
Factory method: Example Extra Credit: Abstract Factory
Questions? PHP South Africa 2017 Brad Mostert /bsinkwa /mostertb
https://joind.in/talk/3b79a /bsinkwa /mostertb
phpsa2017profiles.co.za /bsinkwa /mostertb