Design Patterns: Hello Readers, This is a small blog on design patterns , hope this blog will help you to understand this topic. Microsoft design pattern Theory is, "The document introduces patterns and then presents them in a repository, or catalogue, which is organized to help you locate the right combination of patterns that solves your problem".
Example:
Singleton: A Class has one instance, It provides a global access point to it, The below written code will explain the singleton concept.
<?php
class Singleton {
public static function getInstance() {
static $instance = null;
if (null === $instance) {
$instance = new static();
}
return $instance;
}
protected function __construct() {
}
private function __clone() {
}
private function __wakeup() {
}
}
class Singleto
Design Patterns: Hello Readers, This is a small blog on design patterns , hope this blog will help you to understand this topic. Microsoft design pattern Theory is, "The document introduces patterns and then presents them in a repository, or catalogue, which is organized to help you locate the right combination of patterns that solves your problem".
nChild extends Singleton {
}
$obj = Singleton::getInstance();
var_dump($obj === Singleton::getInstance());
$anotherObj = SingletonChild::getInstance();
var_dump($anotherObj === Singleton::getInstance());
var_dump($anotherObj === SingletonChild::getInstance());
?>
Factory: A Class Simple Creates the object and you want to use that object,The below written code will explain about factory design pattern. The main difficulty with factory pattern is that it will increase the complexity and it is not reliable for good programmers.
<?php
class Automobile {
private $bikeMake;
private $bikeModel;
public function __construct($make, $model) {
$this->bikeMake = $make;
$this->bikeModel = $model;
}
public function getMakeAndModel() {
return $this->bikeMake . ' ' . $this->bikeModel;
}
}
class AutomobileFactory {
public static function create($make, $model) {
return new Automobile($make, $model);
}
}
$pulsar = AutomobileFactory::create('ktm', 'Pulsar');
print_r($pulsar->getMakeAndModel());
class Automobile {
private $bikeMake;
private $bikeModel;
public function __construct($make, $model) {
$this->bikeMake = $make;
$this->bikeModel = $model;
}
public function getMakeAndModel() {
return $this->bikeMake . ' ' . $this->bikeModel;
}
}
class AutomobileFactory {
public static function create($make, $model) {
return new Automobile($make, $model);
}
}
t$pulsar = AutomobileFactory::create('ktm', 'Factory: A Class Simple Creates the object and you want to use that object,The below written code will explain about factory design pattern. The main difficulty with factory pattern is that it will increase the complexity and it is not reliable for good programmers.pulsar');
print_r($pulsar->getMakeAndModel());
?>
Strategy pattern: Strategy pattern makes a family algorithm and encapsulates each algorithm. In this each algorithm should be inter-changeable within the family.
<?php
$elements = array(
array(
'id' => 2,
'date' => '2011-01-01',
),
array(
'id' => 1,
'date' => '2011-02-01'
)
);
$collection = new ObjectCollection($elements);
$collection->setComparator(new IdComparator());
$collection->sort();
echo "Sorted by ID:\n";
print_r($collection->elements);
$collection->setComparator(new DateComparator());
$collection->sort();
echo "Sorted by date:\n";
print_r($collection->elements);
?>
0 Comment(s)