Last active
December 19, 2015 12:29
-
-
Save paulbarbu/5955600 to your computer and use it in GitHub Desktop.
A few design patterns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dummy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo{ | |
public $foo = TRUE; | |
} | |
class Factory{ | |
public static function create($Product){ | |
return new $Product; | |
} | |
} | |
var_dump(Factory::create('Foo')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Singleton{ | |
private static $instance; | |
private $var = 0; | |
private function __construct(){ | |
} | |
public function __clone(){ | |
trigger_error('Cloning of singletons is not allowed!', E_USER_ERROR); | |
} | |
public function __wakeup(){ | |
static::$instance = self::$instance; | |
} | |
public static function getInstance(){ | |
if(!isset(self::$instance)){ | |
self::$instance = new Singleton(); | |
} | |
return self::$instance; | |
} | |
public function process(){ | |
return $this->var++; | |
} | |
} | |
/* | |
class Singleton implements Serializable{ | |
private static $instance; | |
private $var = 0; | |
private function __construct(){ | |
} | |
public function __clone(){ | |
trigger_error('Cloning of singletons is not allowed!', E_USER_ERROR); | |
} | |
public function serialize(){ | |
return serialize($this->var); | |
} | |
public function unserialize($serialized){ | |
$this->var = unserialize($serialized); | |
} | |
public static function getInstance(){ | |
if(!isset(self::$instance)){ | |
self::$instance = new Singleton(); | |
} | |
return self::$instance; | |
} | |
public function process(){ | |
return $this->var++; | |
} | |
} | |
*/ | |
$bar = Singleton::getInstance(); | |
$bar->process(); | |
$baz = Singleton::getInstance(); | |
$baz->process(); | |
var_dump(serialize($baz)); | |
$baz = unserialize(serialize($baz)); | |
$baz->process(); | |
var_dump($bar, $baz); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Strategy{ | |
public function compute(); | |
} | |
class FooAlgorithm implements Strategy{ | |
public function compute(){ | |
return 42; | |
} | |
} | |
class BarAlgorithm implements Strategy{ | |
public function compute(){ | |
return 42+42; | |
} | |
} | |
class Consumer{ | |
public $algo; | |
public function __construct(Strategy $strategy){ | |
$this->algo = $strategy; | |
} | |
public function doJob(){ | |
return $this->algo->compute(); | |
} | |
} | |
if(!TRUE){ | |
$a = new Consumer(new BarAlgorithm); | |
} | |
else{ | |
$a = new Consumer(new FooAlgorithm); | |
} | |
var_dump($a->doJob()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment