Design attern in php

1,581 views
1,484 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,581
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
41
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Design attern in php

  1. 1. Design pattern in PHP Filippo De Santis - fd@ideato.it - @filippodesantis
  2. 2. Design pattern in PHP Web developer Working in @ideato since 2009 XP and Kanban user
  3. 3. Design pattern in PHP What is a design pattern?
  4. 4. Design pattern in PHP What is a design pattern?“Each pattern describes a problem which occurs over andover again in our environment, and then describes the coreof the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” Christopher Alexander
  5. 5. Design pattern in PHP What is a design pattern?“A pattern is a [general] solution to a problem in a context” Gang of four
  6. 6. Design pattern in PHP Elements of a design pattern NameUsed to describe a design problem, its solution and its consequences in a word or two
  7. 7. Design pattern in PHP Elements of a design pattern Problem Describes when to apply a pattern
  8. 8. Design pattern in PHP Elements of a design pattern SolutionDescribes the elements that make up the design, their relationships, responsibilities, and collaborations
  9. 9. Design pattern in PHP Elements of a design pattern ConsequencesThe results and trade-offs of applying the pattern
  10. 10. Design pattern in PHP Singleton
  11. 11. Design pattern in PHP Singleton Ensures that a class has one instance only Provides a global point of access to it
  12. 12. Design pattern in PHP Singleton Access control Only one instance YES!
  13. 13. Design pattern in PHP Singleton Used to replace global variables [changing the name does not change the problem] Violates the Single Responsibility Principle [creation + singleton class functionality] NO!
  14. 14. Design pattern in PHP Singleton private function __construct() {}
  15. 15. Design pattern in PHP Singleton private static $instance = false;
  16. 16. Design pattern in PHP Singletonpublic static function getInstance() { if (false == self::$instance) { self::$instance = new self(); } return self::$instance;}
  17. 17. Design pattern in PHP Singleton class Singleton { private static $instance = false; private function __construct() {} public static function getInstance() { if (false == self::$instance) { self::$instance = new self(); } return self::$instance; } } $instance = Singleton::getInstance();
  18. 18. Design pattern in PHP Factory method
  19. 19. Design pattern in PHP Factory method Classes delegate responsibility of building objetsLocalize the knowledge of which class is the delegate
  20. 20. Design pattern in PHP Factory method PHP most used implementation: Parameterized factory method
  21. 21. Design pattern in PHP Factory methodclass Factory { public function build($condition) {...}}
  22. 22. Design pattern in PHP Factory method interface Document { ... }
  23. 23. Design pattern in PHP Factory methodMyDoc implements Document {...} YourDoc implements Document {...} TheirDoc implements Document {...}
  24. 24. Design pattern in PHP Factory methodclass Factory { /* @return Document */ public function build($condition) {...}}
  25. 25. Design pattern in PHP Factory methodclass DocumentReaderFactory { public function build($type) { switch ($type) { case txt: return new TxtReader(); case doc: return new DocReader(); //... } }}foreach ($documents as $document) { $factory = new DocumentReaderFactory(); $reader = $factory->build($document->getType()); $reader->read($document);}
  26. 26. Design pattern in PHP Adapter
  27. 27. Design pattern in PHP AdapterConverts the interface of a class into another interface
  28. 28. Design pattern in PHP AdapterAn existing class interface does not match what you need To create a reusable class
  29. 29. Design pattern in PHP Adapter interface AdapterInterface { ... }
  30. 30. Design pattern in PHP Adapter by class inheritanceAdapter exteds Adaptee implements AdapterInterface { ...}
  31. 31. Design pattern in PHP Adapter by objects compositionAdapter implements AdapterInterface { public function __construct(Adaptee $adaptee){ $this->adaptee = $adaptee; } ...}
  32. 32. Design pattern in PHP Adapter interface FileOperationsInterface { public function getContent($filename); public function putContent($filename, $data); public function removeFile($filename); }
  33. 33. Design pattern in PHP Adapter by class inheritance class FTPFileAdapter extends FTPFileRepository implements FileOperationsInterface { public function getContent($filename) { … } public function putContent($local_file, $data) { … } public function removeFile($filename) { … } }
  34. 34. Design pattern in PHP Adapterclass FTPFileAdapter implements FileOperationsInterface { by objects composition public function __construct(FTPFileRepository $repository) { $this->repository = $repository; } public function getContent($filename) { $this->repository->download($local_file, $filename); return file_get_content($local_file); } public function putContent($local_file, $data) { file_put_contents($local_file, $data); $this->repository->upload($remote_file, $local_file); } public function removeFile($filename){ $this->repository->remove($filename); }}
  35. 35. Design pattern in PHP Template method
  36. 36. Design pattern in PHP Template methodDefines the skeleton of an algorithm in an operation, deferring some steps to subclasses
  37. 37. Design pattern in PHP Template methodImplements the invariant parts of an algorithm onceLeaves it up to subclasses the behavior that can vary Common behavior localized in a common class To control subclasses extensions
  38. 38. Design pattern in PHP Template methodClasses implementing a template method should: specify hooks (may be overridden)specify abstract operations (must be overridden)
  39. 39. Design pattern in PHP Template method abstract operations (must be overridden) ie: use prefix “DO” DoRead() DoWrite() DoSomething()
  40. 40. Design pattern in PHP Template method abstract class Reader { abstract protected function openFile($filename); abstract protected function readFile(); abstract protected function closeFile(); public function readFileAlgorithm($filename) { $this->openFile($filename); $content = $this->readFile(); $this->closeFile(); return $content }
  41. 41. Design pattern in PHPmethod Template class XMLReader extends Reader { protected function openFile($filename) { $this->xml = simplexml_load_file($filename); } protected function readFile() { return $this->xml->description; } public function closeFile(){} }
  42. 42. Design pattern in PHPmethod Template class CSVReader extends Reader { protected function openFile($filename) { $this->handle = fopen($filename, "r"); } protected function closeFile() { fclose($this->handle); } protected function readFile() { $data = fgetcsv($this->handle); return $data[3]; //description } }
  43. 43. Design pattern in PHPmethod Template Hooks class Parent { protected function hook() {} public function doSomething() { //... $this->hook(); } }
  44. 44. Design pattern in PHPmethod Template Hooks class Child extends Parent { protected function hook() { //My logic to add into the doSomething method } }
  45. 45. Design pattern in PHP Dependency Injection
  46. 46. Design pattern in PHP Dependency InjectionComponents are given their dependencies throughtheir constructors, methods, or directly into fields
  47. 47. Design pattern in PHP Dependency InjectionThose components do not get their dependencies themselves, or instantiate them directly
  48. 48. Design pattern in PHP Dependency Injection class A { public function doSomething() { $b = new B(); //... } } NO!
  49. 49. Design pattern in PHP Dependency Injection class A { public function construct(B $b) { $this->b = $b; } //... } YES!
  50. 50. Design pattern in PHP Dependency Injection class A { public function setB(B $b) { $this->b = $b; } //... } YES!
  51. 51. Design pattern in PHP Dependency Injection class A { public $b; //... } $a = new A(); $a->b = new B(); YES!
  52. 52. Design pattern in PHP Dependency Injection class A implements DiInterface{ } interface DiInterface { public function setB(B $b); } YES!
  53. 53. Design pattern in PHP Dependency Injection ContainerDependency & Injection Inversion of control
  54. 54. References Design Patterns: Elements of Reusable Object-Oriented Software E. Gamma, R. Helm, R. Johnson, J.Vlissides Addison-Wesley 1974 Martin Fowler - http://martinfowler.com/bliki/InversionOfControl.html PicoContainer Documentation: http://picocontainer.org/injection.htmlSourceMaking - Design Patterns: http://sourcemaking.com/design_patterns PHP best practices - Pattern in PHP: http://www.phpbestpractices.it
  55. 55. Thank you!Filippo De Santis - fd@ideato.it - @filippodesantis

×