Download - Design attern in php
![Page 2: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/2.jpg)
Design pattern in PHP
Web developer
Working in @ideato since 2009
XP and Kanban user
![Page 3: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/3.jpg)
Design pattern in PHPWhat is a design pattern?
![Page 4: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/4.jpg)
Design pattern in PHP
“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of 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
What is a design pattern?
![Page 5: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/5.jpg)
Design pattern in PHP
“A pattern is a [general] solution to a problem in a context”
Gang of four
What is a design pattern?
![Page 6: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/6.jpg)
Design pattern in PHP
Name
Used to describe a design problem, its solution and its consequences in a word or two
Elements of a design pattern
![Page 7: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/7.jpg)
Design pattern in PHP
Problem
Describes when to apply a pattern
Elements of a design pattern
![Page 8: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/8.jpg)
Design pattern in PHP
Solution
Describes the elements that make up the design, their relationships, responsibilities, and collaborations
Elements of a design pattern
![Page 9: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/9.jpg)
Design pattern in PHP
Consequences
The results and trade-offs of applying the pattern
Elements of a design pattern
![Page 10: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/10.jpg)
Design pattern in PHPSingleton
![Page 11: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/11.jpg)
Design pattern in PHPSingleton
Ensures that a class has one instance onlyProvides a global point of access to it
![Page 12: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/12.jpg)
Design pattern in PHPSingleton
Access controlOnly one instance
YES!
![Page 13: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/13.jpg)
Design pattern in PHPSingleton
Used to replace global variables[changing the name does not change the problem]
Violates the Single Responsibility Principle[creation + singleton class functionality]
NO!
![Page 14: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/14.jpg)
Design pattern in PHPSingleton
private function __construct() {}
![Page 15: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/15.jpg)
Design pattern in PHPSingleton
private static $instance = false;
![Page 16: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/16.jpg)
Design pattern in PHPSingleton
public static function getInstance() { if (false == self::$instance) { self::$instance = new self(); }
return self::$instance;}
![Page 17: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/17.jpg)
Design pattern in PHPSingleton
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();
![Page 18: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/18.jpg)
Design pattern in PHPFactory method
![Page 19: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/19.jpg)
Design pattern in PHPFactory method
Classes delegate responsibility of building objetsLocalize the knowledge of which class is the delegate
![Page 20: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/20.jpg)
Design pattern in PHPFactory method
PHP most used implementation:Parameterized factory method
![Page 21: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/21.jpg)
Design pattern in PHPFactory method
class Factory {
public function build($condition) {...}
}
![Page 22: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/22.jpg)
Design pattern in PHPFactory method
interface Document { ...}
![Page 23: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/23.jpg)
Design pattern in PHPFactory method
MyDoc implements Document {...}
YourDoc implements Document {...}
TheirDoc implements Document {...}
![Page 24: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/24.jpg)
Design pattern in PHPFactory method
class Factory {
/* @return Document */ public function build($condition) {...}
}
![Page 25: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/25.jpg)
Design pattern in PHPFactory 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); }
![Page 26: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/26.jpg)
Design pattern in PHPAdapter
![Page 27: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/27.jpg)
Design pattern in PHPAdapter
Converts the interface of a class into another interface
![Page 28: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/28.jpg)
Design pattern in PHPAdapter
An existing class interface does not match what you need
To create a reusable class
![Page 29: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/29.jpg)
Design pattern in PHPAdapter
interface AdapterInterface { ...}
![Page 30: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/30.jpg)
Design pattern in PHPAdapter
by class inheritance
Adapter exteds Adaptee implements AdapterInterface { ...}
![Page 31: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/31.jpg)
Design pattern in PHPAdapter
by objects composition
Adapter implements AdapterInterface { public function __construct(Adaptee $adaptee){ $this->adaptee = $adaptee; }
...}
![Page 32: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/32.jpg)
Design pattern in PHPAdapter
interface FileOperationsInterface {
public function getContent($filename);
public function putContent($filename, $data);
public function removeFile($filename);
}
![Page 33: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/33.jpg)
Design pattern in PHPAdapter
by class inheritance
class FTPFileAdapter extends FTPFileRepository implements FileOperationsInterface {
public function getContent($filename) { … } public function putContent($local_file, $data) { … } public function removeFile($filename) { … }}
![Page 34: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/34.jpg)
Design pattern in PHPAdapter
by objects compositionclass FTPFileAdapter implements FileOperationsInterface {
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); }}
![Page 35: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/35.jpg)
Design pattern in PHPTemplate method
![Page 36: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/36.jpg)
Design pattern in PHPTemplate method
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses
![Page 37: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/37.jpg)
Design pattern in PHPTemplate method
Common behavior localized in a common class
Implements the invariant parts of an algorithm onceLeaves it up to subclasses the behavior that can vary
To control subclasses extensions
![Page 38: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/38.jpg)
Design pattern in PHPTemplate method
Classes implementing a template method should:
specify hooks (may be overridden)
specify abstract operations (must be overridden)
![Page 39: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/39.jpg)
Design pattern in PHPTemplate method
abstract operations (must be overridden)
ie: use prefix “DO”DoRead()DoWrite()
DoSomething()
![Page 40: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/40.jpg)
Design pattern in PHPTemplate 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 }
![Page 41: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/41.jpg)
Design pattern in PHPTemplate method
class XMLReader extends Reader {
protected function openFile($filename) { $this->xml = simplexml_load_file($filename); }
protected function readFile() { return $this->xml->description; } public function closeFile(){}
}
![Page 42: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/42.jpg)
Design pattern in PHPclass 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 }}
Template method
![Page 43: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/43.jpg)
Design pattern in PHP
class Parent { protected function hook() {}
public function doSomething() { //... $this->hook(); }}
Template methodHooks
![Page 44: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/44.jpg)
Design pattern in PHP
class Child extends Parent {
protected function hook() { //My logic to add into the doSomething method }
}
Template methodHooks
![Page 45: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/45.jpg)
Design pattern in PHPDependency Injection
![Page 46: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/46.jpg)
Design pattern in PHPDependency Injection
Components are given their dependencies through their constructors, methods, or directly into fields
![Page 47: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/47.jpg)
Design pattern in PHPDependency Injection
Those components do not get their dependencies themselves, or instantiate them directly
![Page 48: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/48.jpg)
Design pattern in PHPDependency
Injection
class A {
public function doSomething() { $b = new B(); //... }} NO!
![Page 49: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/49.jpg)
Design pattern in PHP
class A { public function construct(B $b) { $this->b = $b; }
//...} YES!
DependencyInjection
![Page 50: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/50.jpg)
Design pattern in PHP
class A { public function setB(B $b) { $this->b = $b; }
//...}
DependencyInjection
YES!
![Page 51: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/51.jpg)
Design pattern in PHP
class A { public $b;
//...}
$a = new A();$a->b = new B();
DependencyInjection
YES!
![Page 52: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/52.jpg)
Design pattern in PHP
class A implements DiInterface{}
interface DiInterface {
public function setB(B $b);
}
DependencyInjection
YES!
![Page 53: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/53.jpg)
Design pattern in PHP
Dependency Injection
Dependency Injection Container&
Inversion of control
![Page 54: Design attern in php](https://reader035.vdocument.in/reader035/viewer/2022081401/5579e4ded8b42aa54d8b4d29/html5/thumbnails/54.jpg)
ReferencesDesign Patterns: Elements of Reusable Object-Oriented Software
E. Gamma, R. Helm, R. Johnson, J. VlissidesAddison-Wesley 1974
Martin Fowler - http://martinfowler.com/bliki/InversionOfControl.html
PicoContainer Documentation: http://picocontainer.org/injection.html
SourceMaking - Design Patterns: http://sourcemaking.com/design_patterns
PHP best practices - Pattern in PHP: http://www.phpbestpractices.it