50

PHP: 3 Design Patterns to Make Better Code

Embed Size (px)

Citation preview

Page 1: PHP: 3 Design Patterns to Make Better Code
Page 2: PHP: 3 Design Patterns to Make Better Code

WELCOME!• Name is: Joseph Maxwell

• Example code:

• https://github.com/JosephMaxwell/DesignPatterns

Page 3: PHP: 3 Design Patterns to Make Better Code

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.

Page 4: PHP: 3 Design Patterns to Make Better Code

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.

Page 5: PHP: 3 Design Patterns to Make Better 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.

Page 6: PHP: 3 Design Patterns to Make Better Code

FACTORYAllows centralization of code for class construction.

Page 7: PHP: 3 Design Patterns to Make Better Code

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

Page 8: PHP: 3 Design Patterns to Make Better Code

PROBLEM:• Infinite types of

products

• Don’t want to know what product is created

• Each product should bring their own functionality.

Page 9: PHP: 3 Design Patterns to Make Better Code

FACTORY DESIGN:

Model needs “Child Select” product

Factory

Simple Product

Child Select Product

Bundled Product

Page 10: PHP: 3 Design Patterns to Make Better Code

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.

Page 11: PHP: 3 Design Patterns to Make Better Code

EXAMPLE

Page 12: PHP: 3 Design Patterns to Make Better Code

SIMPLEST FACTORY

Page 13: PHP: 3 Design Patterns to Make Better Code

FLEXIBLE SOLUTION:

Page 14: PHP: 3 Design Patterns to Make Better Code

YAML EXAMPLE:

Page 15: PHP: 3 Design Patterns to Make Better Code

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.

Page 16: PHP: 3 Design Patterns to Make Better Code

TESTING:

Page 17: PHP: 3 Design Patterns to Make Better Code

SINGLETONSAnti-pattern: a global means to access one instance of an

object

Page 18: PHP: 3 Design Patterns to Make Better Code

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?

Page 19: PHP: 3 Design Patterns to Make Better Code

PROBLEM:

Page 20: PHP: 3 Design Patterns to Make Better Code
Page 21: PHP: 3 Design Patterns to Make Better Code

SINGLETON DESIGNModel needs

session

class Session

Create instance and store it.

self::$_instance? Return instance.

No

Yes

Page 22: PHP: 3 Design Patterns to Make Better Code

SOLUTION?• Use static class methods to track single

instance of object:

Using Static:

Page 23: PHP: 3 Design Patterns to Make Better Code

SINGLETON’S PROBLEMS:

• Hard links the session to the class.

• Makes testing difficult or impossible.

• Breaks Single Responsibility Principle managing its own instance.

Page 24: PHP: 3 Design Patterns to Make Better Code

DEPENDENCY INJECTION (DI)

Providing classes the variables they need.

Page 25: PHP: 3 Design Patterns to Make Better Code

Title Text

Page 26: PHP: 3 Design Patterns to Make Better Code

DI DESIGN

new Model(Session $session);

new Session();

Page 27: PHP: 3 Design Patterns to Make Better Code

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.

Page 28: PHP: 3 Design Patterns to Make Better Code

DI

Page 29: PHP: 3 Design Patterns to Make Better Code

DI (MANUAL)

Page 30: PHP: 3 Design Patterns to Make Better Code

DI CONTAINERS

new Model(); new Session() DI Container

__construct(Session $session)

Page 31: PHP: 3 Design Patterns to Make Better Code

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

Page 32: PHP: 3 Design Patterns to Make Better Code

TESTING:

Page 33: PHP: 3 Design Patterns to Make Better Code
Page 34: PHP: 3 Design Patterns to Make Better Code

USE CASES:• Database connections

• Sessions

Page 35: PHP: 3 Design Patterns to Make Better Code

CHAIN OF RESPONSIBILITY

Handling logic through multiple layers of functionality.

Page 36: PHP: 3 Design Patterns to Make Better Code

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?

Page 37: PHP: 3 Design Patterns to Make Better Code

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.

Page 38: PHP: 3 Design Patterns to Make Better Code

SOLUTION #1: LOOPS

Page 39: PHP: 3 Design Patterns to Make Better Code
Page 40: PHP: 3 Design Patterns to Make Better Code

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.

Page 41: PHP: 3 Design Patterns to Make Better Code

COR DESIGN

Need cache value

DBMemcachedRedis

processed? processed? processed?

No

Yes!

Base Object

Page 42: PHP: 3 Design Patterns to Make Better Code
Page 43: PHP: 3 Design Patterns to Make Better Code
Page 44: PHP: 3 Design Patterns to Make Better Code
Page 45: PHP: 3 Design Patterns to Make Better Code
Page 46: PHP: 3 Design Patterns to Make Better Code

TESTING:

Page 47: PHP: 3 Design Patterns to Make Better Code

USE CASES:• Layers of caching

• Rendering views with a standard input, but giving each member the equal opportunity to display.

Page 48: PHP: 3 Design Patterns to Make Better Code

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.

Page 49: PHP: 3 Design Patterns to Make Better Code

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/

Page 50: PHP: 3 Design Patterns to Make Better Code

FEEDBACK?https://joind.in/14702