Download presentation
Presentation is loading. Please wait.
1
Software Design Patterns (2)
four commonly used design patterns using php (singleton, factory, adapter & decorator)
2
the singleton pattern (1)
when only one object needs to be responsible for a particular task. exclusive resource with only one type of this resource. most commonly used in PHP to limit connections to the database throughout the codebase. singleton pattern considered a responsibility pattern because it delegates control for creation to a single point. All singleton patterns have at least three common elements: - they must have a constructor, and it must be marked private - they contain a static member variable that will hold an instance of the class - they contain a public static method to access the instance
3
the singleton pattern (2)
basic form: class Singleton { private static $instance = null; private static function __construct() { } public static function getInstance() { if(empty(self::$instance) { self::$instance = new Singleton(); } return self::$instance;
4
the singleton pattern (3)
class Database { private $_db; static $_instance; private function __construct() { $this->_db = mysql_connect('localhost', 'root', ''); mysql_select_db('phobias'); } private function __clone() {} public static function getInstance() { if(!(self::$_instance instanceof self)) { self::$_instance = new self(); return self::$_instance; public function query($sql) { return mysql_query($sql, $this->_db);
5
the singleton pattern (4)
test singleton: $db = Database::getInstance(); $result = $db->query("SELECT * FROM phobias WHERE def LIKE '%pain%'"); while ($row = mysql_fetch_assoc($result)) { echo $row["term"].' : ' $row["def"].'<br/>'; } view code run test
6
the factory pattern (1) the factory pattern is a class that creates objects. a factory is typically used to return different classes that conform to a similar interface. usually, a factory pattern has one key ingredient: a static method, which is by convention named “factory”. the static method may take any number of arguments and must return an object. factories are critically important to the practice of polymorphic programming.
7
the factory pattern (2) basic form: class Driver {
# The parameterized factory method public static function factory($type) { if (include_once 'Drivers/' . $type . '.php') { $classname = 'Driver_' . $type; return new $classname; } else { throw new Exception('Driver not found'); } # Load a MySQL Driver $mysql = Driver::factory('MySQL'); # Load an SQLite Driver $sqlite = Driver::factory('SQLite');
8
the factory pattern (3) interface DB { public function connect();
public function query(); public function disconnect(); } class DBmysql implements DB { public function connect() { //database connection stuff } public function query() { //perform database query stuff } public function disconnect() { //disconnect database } class DBoracle implements DB {
9
the factory pattern (4) class DBfactory { public static $_DB;
public static function &factory($szType = "") { if(!is_object(self::$_DB)) { switch($szType) { case 'mysql': self::$_DB = new DBmysql; break; case ‘oracle': self::$_DB = new DBoracle; default: } return self::$_DB;
10
the factory pattern (5) factory can now create different driver objects (default mysql): db = DBfactory::factory("mysql"); $db->query(".."); DBfactory::factory()->query("..");
11
the factory pattern (6) aside : Type Hinting
12
the adapter pattern (1) the adapter pattern simply converts the interface of one class to what another class expects. for this reason, it is often referred to as a "wrapper" pattern. adapters are helpful when a class doesn't have quite the exact methods needed, and its not possible to change the original class. the adapter can take the methods accessible in the original class, and adapt them into the methods needed.
13
the adapter pattern (2) class SimpleBook { private $author;
private $title; function __construct($author_in, $title_in) { $this->author = $author_in; $this->title = $title_in; } function getAuthor() { return $this->author; function getTitle() { return $this->title;
14
the adapter pattern (3) view code run test class BookAdapter {
private $book; function __construct(SimpleBook $book_in) { $this->book = $book_in; } function getAuthorAndTitle() { return $this->book->getTitle() . ' by ' . $this->book->getAuthor(); $book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns"); $bookAdapter = new BookAdapter($book); echo 'Author and Title: '.$bookAdapter->getAuthorAndTitle(); view code run test
15
the decorator pattern (1)
the point of the decorator pattern is to enhance an object with some functionality without modifying the structure of the original object. the decorator pattern is often used an example of the principle of preferring "composition over inheritance"
16
the decorator pattern (2)
class Cupcake { public $Toppings = array('sprinkles' => 0, 'frosting' => 'none'); public function __construct($int, $str) { $this->Toppings['sprinkles'] = $int; $this->Toppings['frosting'] = $str; } abstract class Decorator_Wrapper { abstract function Add($mixed); abstract function Remove($mixed);
17
the decorator pattern (3)
class Sprinkle_Decorator extends Decorator_Wrapper { public function __construct(Cupcake $c) { $this->Cupcake = $c; } public function Add($int) { $this->Cupcake->Toppings['sprinkles'] += $int; public function Remove($int) { $this->Cupcake->Toppings['sprinkles'] -= $int;
18
the decorator pattern (4)
class Frosting_Decorator extends Decorator_Wrapper { public function __construct(Cupcake $c) { $this->Cupcake = $c; } public function Add($str) { $this->Cupcake->Toppings['frosting'] = $str; public function Remove($str) { echo "Hrmm.. We cant seem to remove your $str, it's stuck on the muffin!";
19
the decorator pattern (5)
# Test the decorator pattern echo '<p>Make a new cupcake!</p>'; $cupcake = new Cupcake(5, 'none'); print_r($cupcake->Toppings); echo '<p>Add more sprinkles!</p>' ; $sprinkle = new Sprinkle_Decorator($cupcake); $sprinkle->Add('55'); echo '<p>Remove 25 sprinkles!</p>' ; $sprinkle->Remove('25'); echo '<p>Add some frosting :)</p>'; $frosting = new Frosting_Decorator($cupcake); $frosting->Add('Chocolate');print_r($cupcake->Toppings); view code run test
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.