Upload
filippo-de-santis
View
1.266
Download
1
Tags:
Embed Size (px)
Citation preview
Design pattern in PHP
Filippo De Santis - [email protected] - @filippodesantis
Design pattern in PHP
Web developer
Working in @ideato since 2009
XP and Kanban user
Design pattern in PHPWhat is a design pattern?
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?
Design pattern in PHP
“A pattern is a [general] solution to a problem in a context”
Gang of four
What is a design pattern?
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
Design pattern in PHP
Problem
Describes when to apply a pattern
Elements of a design pattern
Design pattern in PHP
Solution
Describes the elements that make up the design, their relationships, responsibilities, and collaborations
Elements of a design pattern
Design pattern in PHP
Consequences
The results and trade-offs of applying the pattern
Elements of a design pattern
Design pattern in PHPSingleton
Design pattern in PHPSingleton
Ensures that a class has one instance onlyProvides a global point of access to it
Design pattern in PHPSingleton
Access controlOnly one instance
YES!
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!
Design pattern in PHPSingleton
private function __construct() {}
Design pattern in PHPSingleton
private static $instance = false;
Design pattern in PHPSingleton
public static function getInstance() { if (false == self::$instance) { self::$instance = new self(); }
return self::$instance;}
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();
Design pattern in PHPFactory method
Design pattern in PHPFactory method
Classes delegate responsibility of building objetsLocalize the knowledge of which class is the delegate
Design pattern in PHPFactory method
PHP most used implementation:Parameterized factory method
Design pattern in PHPFactory method
class Factory {
public function build($condition) {...}
}
Design pattern in PHPFactory method
interface Document { ...}
Design pattern in PHPFactory method
MyDoc implements Document {...}
YourDoc implements Document {...}
TheirDoc implements Document {...}
Design pattern in PHPFactory method
class Factory {
/* @return Document */ public function build($condition) {...}
}
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); }
Design pattern in PHPAdapter
Design pattern in PHPAdapter
Converts the interface of a class into another interface
Design pattern in PHPAdapter
An existing class interface does not match what you need
To create a reusable class
Design pattern in PHPAdapter
interface AdapterInterface { ...}
Design pattern in PHPAdapter
by class inheritance
Adapter exteds Adaptee implements AdapterInterface { ...}
Design pattern in PHPAdapter
by objects composition
Adapter implements AdapterInterface { public function __construct(Adaptee $adaptee){ $this->adaptee = $adaptee; }
...}
Design pattern in PHPAdapter
interface FileOperationsInterface {
public function getContent($filename);
public function putContent($filename, $data);
public function removeFile($filename);
}
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) { … }}
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); }}
Design pattern in PHPTemplate method
Design pattern in PHPTemplate method
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses
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
Design pattern in PHPTemplate method
Classes implementing a template method should:
specify hooks (may be overridden)
specify abstract operations (must be overridden)
Design pattern in PHPTemplate method
abstract operations (must be overridden)
ie: use prefix “DO”DoRead()DoWrite()
DoSomething()
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 }
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(){}
}
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
Design pattern in PHP
class Parent { protected function hook() {}
public function doSomething() { //... $this->hook(); }}
Template methodHooks
Design pattern in PHP
class Child extends Parent {
protected function hook() { //My logic to add into the doSomething method }
}
Template methodHooks
Design pattern in PHPDependency Injection
Design pattern in PHPDependency Injection
Components are given their dependencies through their constructors, methods, or directly into fields
Design pattern in PHPDependency Injection
Those components do not get their dependencies themselves, or instantiate them directly
Design pattern in PHPDependency
Injection
class A {
public function doSomething() { $b = new B(); //... }} NO!
Design pattern in PHP
class A { public function construct(B $b) { $this->b = $b; }
//...} YES!
DependencyInjection
Design pattern in PHP
class A { public function setB(B $b) { $this->b = $b; }
//...}
DependencyInjection
YES!
Design pattern in PHP
class A { public $b;
//...}
$a = new A();$a->b = new B();
DependencyInjection
YES!
Design pattern in PHP
class A implements DiInterface{}
interface DiInterface {
public function setB(B $b);
}
DependencyInjection
YES!
Design pattern in PHP
Dependency Injection
Dependency Injection Container&
Inversion of control
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
Filippo De Santis - [email protected] - @filippodesantis
Thank you!