Last active
August 29, 2015 14:09
-
-
Save gokure/6e48b555df231c26fc31 to your computer and use it in GitHub Desktop.
Design patterns snippets
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 | |
abstract class AbstractMobileFactory { | |
abstract public function createMobile(); | |
} | |
class AppleFactory extends AbstractMobileFactory { | |
public function createMobile() { | |
return new AppleMobile(); | |
} | |
} | |
class SamsungFactory extends AbstractMobileFactory { | |
public function createMobile() { | |
return new SamsungMobile(); | |
} | |
} | |
interface IMobile { | |
public function getName(); | |
public function getRom(); | |
} | |
class AppleMobile implements IMobile { | |
public function getName() { | |
return "iPhone 6"; | |
} | |
public function getRom() { | |
return "iOS 8.1.1"; | |
} | |
} | |
class SamsungMobile implements IMobile { | |
public function getName() { | |
return "Galaxy Note 4"; | |
} | |
public function getRom() { | |
return "Android 4.4.4"; | |
} | |
} | |
class MobileFactory { | |
public static function build($vendor) { | |
$klass = $vendor.'Factory'; | |
if (class_exists($klass)) { | |
return new $klass; | |
} else { | |
throw new Exception('Invalid argument'); | |
} | |
} | |
} | |
$vendors = array('Apple', 'Samsung'); | |
foreach ($vendors as $vendor) { | |
$factory = MobileFactory::build($vendor); | |
$mobile = $factory->createMobile(); | |
printf("%s rom is %s\n", $mobile->getName(), $mobile->getRom()); | |
echo "<br>"; | |
} |
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 SocialAdapter { | |
function send($msg); | |
} | |
class Twitter { | |
function twitteMessage($msg) { | |
echo $msg; | |
} | |
} | |
class Facebook { | |
function updateStatus($msg) { | |
echo $msg; | |
} | |
} | |
class TwitterAdapterImpl implements SocialAdapter { | |
private $twitter; | |
function __construct(Twitter $twitter) { | |
$this->twitter = $twitter; | |
} | |
public function send($msg) { | |
$this->twitter->twitteMessage($msg); | |
} | |
} | |
class FacebookAdapterImpl implements SocialAdapter { | |
private $facebook; | |
function __construct(Facebook $facebook) { | |
$this->facebook = $facebook; | |
} | |
public function send($msg) { | |
$this->facebook->updateStatus($msg); | |
} | |
} | |
$twitter = new TwitterAdapterImpl(new Twitter()); | |
$twitter->send("Post to twitter"); | |
$facebook = new FacebookAdapterImpl(new Facebook()); | |
$facebook->send("Post to facebook"); |
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 MobileFactory { | |
public static function createMobile($mobile) { | |
$klass = $mobile.'Mobile'; | |
if (class_exists($klass)) { | |
return new $klass(); | |
} else { | |
throw new Exception('Invalid argument.'); | |
} | |
} | |
} | |
interface IMobile { | |
public function getName(); | |
public function getRom(); | |
} | |
class AppleMobile implements IMobile { | |
public function getName() { | |
return "iPhone 6"; | |
} | |
public function getRom() { | |
return "iOS 8.1.1"; | |
} | |
} | |
class SamsungMobile implements IMobile { | |
public function getName() { | |
return "Galaxy Note 4"; | |
} | |
public function getRom() { | |
return "Android 4.4.4"; | |
} | |
} | |
$vendors = array('Apple', 'Samsung'); | |
foreach ($vendors as $vendor) { | |
$mobile = MobileFactory::createMobile($vendor); | |
printf("%s rom is %s\n", $mobile->getName(), $mobile->getRom()); | |
echo "<br>"; | |
} |
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 IObserver { | |
function invoke($sender, $args); | |
} | |
interface IObservable { | |
function addObserver($observer); | |
} | |
class UserList implements IObservable { | |
private $_observers = array(); | |
public function addCustomer($name) { | |
foreach ($this->_observers as $obj) { | |
$obj->invoke($this, $name); | |
} | |
} | |
public function addObserver($observer) { | |
$this->_observers[] = $observer; | |
} | |
} | |
class UserListLogger implements IObserver { | |
function invoke($sender, $args) { | |
echo "'$args' added to user list\n"; | |
} | |
} | |
class UserListTrace implements IObserver { | |
function invoke($sender, $args) { | |
echo date("Y-m-d H:i:s\n"); | |
} | |
} | |
$ul = new UserList(); | |
$ul->addObserver(new UserListTrace()); | |
$ul->addObserver(new UserListLogger()); | |
$ul->addCustomer("Jack"); |
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 = null; | |
protected function __construct() {} | |
private function __clone() {} | |
private function __wakeup() {} | |
public static function getInstance() { | |
if (null === self::$_instance) { | |
self::$_instance = new Singleton(); | |
} | |
return self::$_instance; | |
} | |
} | |
class SingletonChild extends Singleton {} | |
$singleton = Singleton::getInstance(); | |
var_dump($singleton === Singleton::getInstance()); | |
$singletonChild = SingletonChild::getInstance(); | |
var_dump($singletonChild === SingletonChild::getInstance()); | |
var_dump($singleton === $singletonChild); |
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 IStrategy { | |
function filter($record); | |
} | |
class FindAfterStrategy implements IStrategy { | |
private $_name; | |
public function __construct($name) { | |
$this->_name = $name; | |
} | |
public function filter($record) { | |
return strcmp($this->_name, $record) <= 0; | |
} | |
} | |
class RandomStrategy implements IStrategy { | |
public function filter($record) { | |
return rand(0, 1) >= 0.5; | |
} | |
} | |
class UserList { | |
private $_list = array(); | |
public function __construct($names) { | |
if ($names != null) { | |
foreach ($names as $name) { | |
$this->_list[] = $name; | |
} | |
} | |
} | |
public function add($name) { | |
$this->_list[] = $name; | |
} | |
public function find($filter) { | |
$recs = array(); | |
foreach ($this->_list as $user) { | |
if ($filter->filter($user)) { | |
$recs[] = $user; | |
} | |
} | |
return $recs; | |
} | |
} | |
$ul = new UserList(array("Andy", "Jack", "Lori", "Megan")); | |
$f1 = $ul->find(new FindAfterStrategy("J")); | |
print_r($f1); | |
$f2 = $ul->find(new RandomStrategy()); | |
print_r($f2); |
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 ItemElement { | |
function accept(ShoppingCartVisitor $visitor); | |
} | |
class Book implements ItemElement { | |
private $price; | |
private $isbn; | |
function __construct($price, $isbn) { | |
$this->price = $price; | |
$this->isbn = $isbn; | |
} | |
public function getPrice() { | |
return $this->price; | |
} | |
public function getIsbn() { | |
return $this->isbn; | |
} | |
public function accept(ShoppingCartVisitor $visitor) { | |
return $visitor->visitBook($this); | |
} | |
} | |
class Fruit implements ItemElement { | |
private $price_per_kg; | |
private $weight; | |
private $name; | |
function __construct($price_per_kg, $weight, $name) { | |
$this->price_per_kg = $price_per_kg; | |
$this->weight = $weight; | |
$this->name = $name; | |
} | |
public function getPricePerKg() { | |
return $this->price_per_kg; | |
} | |
public function getWeight() { | |
return $this->weight; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
public function accept(ShoppingCartVisitor $visitor) { | |
return $visitor->visitFruit($this); | |
} | |
} | |
interface ShoppingCartVisitor { | |
function visitBook(Book $book); | |
function visitFruit(Fruit $fruit); | |
} | |
class ShoppingCartVisitorImpl implements ShoppingCartVisitor { | |
function visitBook(Book $book) { | |
if ($book->getPrice() > 50) { | |
$cost = $book->getPrice() - 5; | |
} else { | |
$cost = $book->getPrice(); | |
} | |
echo "Book ISBN: {$book->getIsbn()} cost={$cost}\n"; | |
return $cost; | |
} | |
function visitFruit(Fruit $fruit) { | |
$cost = $fruit->getPricePerKg() * $fruit->getWeight(); | |
echo "{$fruit->getName()} cost={$cost}\n"; | |
return $cost; | |
} | |
} | |
$items = array( | |
new Book(20, "1234"), | |
new Book(100, "5678"), | |
new Fruit(10, 2, "Banana"), | |
new Fruit(5, 5, "Apple") | |
); | |
$visitor = new ShoppingCartVisitorImpl(); | |
$sum = 0; | |
foreach ($items as $item) { | |
$sum += $item->accept($visitor); | |
} | |
echo "Total cost={$sum}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment