Skip to content

Instantly share code, notes, and snippets.

@tnqsoft
Created August 11, 2017 08:02
Show Gist options
  • Save tnqsoft/60498946d7c69442a52b04214dcd875e to your computer and use it in GitHub Desktop.
Save tnqsoft/60498946d7c69442a52b04214dcd875e to your computer and use it in GitHub Desktop.
PHP Design Pattern
<?php
//http://www.phptherightway.com/pages/Design-Patterns.html
class Automobile
{
private $vehicleMake;
private $vehicleModel;
public function __construct($make, $model)
{
$this->vehicleMake = $make;
$this->vehicleModel = $model;
}
public function getMakeAndModel()
{
return $this->vehicleMake . ' ' . $this->vehicleModel;
}
}
class AutomobileFactory
{
public static function create($make, $model)
{
return new Automobile($make, $model);
}
}
// have the factory create the Automobile object
$veyron = AutomobileFactory::create('Bugatti', 'Veyron');
print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"
<?php
// http://phpenthusiast.com/blog/the-singleton-design-pattern-in-php
class ConfigSingleton
{
/**
* @var [type]
*/
private static $instance = null;
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName = 'default';
// The constructor is private
// to prevent initiation with outer code.
private function __construct()
{
echo 'Created instance Config Object.';
}
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new ConfigSingleton();
}
return static::$instance;
}
public function getFullName(): string
{
return $this->firstName.' '.$this->lastName;
}
/**
* Get the value of First Name
* @return string
*/
public function getFirstName() {
return $this->firstName;
}
/**
* Set the value of First Name
* @param string $firstName
* @return self
*/
public function setFirstName(string $firstName) {
$this->firstName = $firstName;
return $this;
}
/**
* Get the value of Last Name
* @return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Set the value of Last Name
* @param string $lastName
* @return self
*/
public function setLastName(string $lastName) {
$this->lastName = $lastName;
return $this;
}
}
// Test
$config1 = ConfigSingleton::getInstance();
$config1->setFirstName('Nguyen');
$config1->setLastName('Nhu Tuan');
echo $config1->getFullName();
echo '<br/>';
$config2 = ConfigSingleton::getInstance();
echo $config2->getFullName();
echo '<br/>';
try {
$config3 = new ConfigSingleton();
} catch(Error $e) {
echo 'Can not implement create instance ConfigSingleton: '.$e->getMessage();
}
<?php
// http://www.phptherightway.com/pages/Design-Patterns.html
/*
Strategy
With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for instantiating a particular algorithm to have no knowledge of the actual implementation. There are several variations on the strategy pattern, the simplest of which is outlined below:
This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe just an array of data:
*/
interface OutputInterface
{
public function load();
}
class SerializedArrayOutput implements OutputInterface
{
public function load()
{
return serialize($arrayOfData);
}
}
class JsonStringOutput implements OutputInterface
{
public function load()
{
return json_encode($arrayOfData);
}
}
class ArrayOutput implements OutputInterface
{
public function load()
{
return $arrayOfData;
}
}
// -----------------------------------------------------------------------------
class SomeClient
{
private $output;
public function setOutput(OutputInterface $outputType)
{
$this->output = $outputType;
}
public function loadOutput()
{
return $this->output->load();
}
}
$client = new SomeClient();
// Want an array?
$client->setOutput(new ArrayOutput());
$data = $client->loadOutput();
var_dump($data);
// Want some JSON?
$client->setOutput(new JsonStringOutput());
$data = $client->loadOutput();
var_dump($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment