Upload
swiftotter-studios
View
97
Download
1
Tags:
Embed Size (px)
Citation preview
WELCOME!• Name is: Joseph Maxwell
• Example code:
• https://github.com/JosephMaxwell/DesignPatterns
DESIGN PATTERNS• Borrowed from the building
industry.
• A means to label common and industry-tested architectural patterns.
"Joy Oil gas station blueprints" by Joy Oil Co Ltd - This image is available from the City of Toronto Archives, listed under the archival citation Series
410, File 505, Item 8.
DESIGN PATTERN BENEFITS
• Presents a common solution to a problem.
• Makes some problem solving more plug-and-play, thus writing code faster.
• Recognizable to other developers.
• Encourages more legible and maintainable code.
TERMINOLOGY• Interface:
• a means to provide an outline for a class. Classes must provide functionality to this outline.
• Abstract:
• a class that cannot be created, but must be extended.
• Concrete:
• an extension of a abstract class. Usually customize functionality.
FACTORYAllows centralization of code for class construction.
FACTORY• We don’t make our own
phones.
• A factory does.
• “A building or group of buildings where goods are manufactured or assembled chiefly by machine.” — Google
PROBLEM:• Infinite types of
products
• Don’t want to know what product is created
• Each product should bring their own functionality.
FACTORY DESIGN:
Model needs “Child Select” product
Factory
Simple Product
Child Select Product
Bundled Product
FACTORY SOLUTION:• Make a class to act as the factory.
• Force all classes instantiated to inherit a type or implement an interface.
• Return the newly created class.
• Best: use a configuration file (Yaml/Xml) to control the creation of the classes.
EXAMPLE
SIMPLEST FACTORY
FLEXIBLE SOLUTION:
YAML EXAMPLE:
USE CASES:• Assists with multiple types of different but
similar objects (products, image types, etc.).
• Gives means for allowing other developers to extend the functionality of your software.
TESTING:
SINGLETONSAnti-pattern: a global means to access one instance of an
object
PROBLEM:• Your class needs to interact
with a specific instance of another class.
• How do you create or access that specific instance throughout your class?
PROBLEM:
SINGLETON DESIGNModel needs
session
class Session
Create instance and store it.
self::$_instance? Return instance.
No
Yes
SOLUTION?• Use static class methods to track single
instance of object:
Using Static:
SINGLETON’S PROBLEMS:
• Hard links the session to the class.
• Makes testing difficult or impossible.
• Breaks Single Responsibility Principle managing its own instance.
DEPENDENCY INJECTION (DI)
Providing classes the variables they need.
Title Text
DI DESIGN
new Model(Session $session);
new Session();
DI• We create the objects and then “inject”
them into each class that needs it.
• Benefits:
• Easier to follow Single Responsibility Principle.
• Makes testing easier: we can inject a testing-tuned class.
• Good visibility into what each class needs.
DI
DI (MANUAL)
DI CONTAINERS
new Model(); new Session() DI Container
__construct(Session $session)
DI CONTAINERS• Pimple (pimple.sensiolabs.org)
• Super simple container but is rather manual.
• PHP-DI (http://php-di.org/)
• More complex to setup but injection is automatic.
• Similar to Symphony Dependency Injection
component: https://github.com/symfony/DependencyInjection
TESTING:
USE CASES:• Database connections
• Sessions
CHAIN OF RESPONSIBILITY
Handling logic through multiple layers of functionality.
PROBLEM:• Multiple layers of caching.
• How to get the value from the list?
• Multiple renderers for a component.
• How to have renderers determine who should render?
SOLUTION #1: LOOPS• Easier, but less flexible.
• Iterate through each member of an array of the items and have that member check to determine its legibility.
SOLUTION #1: LOOPS
SOLUTION #2: COR• More complex, but offers greater flexibility.
• Each layer inherits from an abstract object.
• Abstract object handles chaining.
• Concrete objects inherit abstract and provide functionality for processing and whether to proceed.
COR DESIGN
Need cache value
DBMemcachedRedis
processed? processed? processed?
No
Yes!
Base Object
TESTING:
USE CASES:• Layers of caching
• Rendering views with a standard input, but giving each member the equal opportunity to display.
REVIEW
Factory DI CoR
Chooses and creates object from
options.
Ensures object is only created once.
Interacts with options to retrieve
output.
All use specific class to maintain Single Responsibility Principle.
RESOURCES• Design Patterns: Elements of Reusable
Object-Oriented Software• (Gamma, Helm, Johnson, Vlissides; published by Addison-
Wesley)
• Design Patterns PHP• http://designpatternsphp.readthedocs.org/en/latest/
FEEDBACK?https://joind.in/14702