Design Pattern trong PHP Nguyên lý Design Pattern của Microsoft là: “Tài liệu giới thiệu các Pattern và sau đó trình bày chúng trong Repository hoặc Catalogue, mà được tổ chức để giúp bạ
Trang 1Design Pattern trong PHP
Nguyên lý Design Pattern của Microsoft là: “Tài liệu giới thiệu các Pattern và sau đó trình bày chúng trong Repository hoặc Catalogue, mà được tổ chức để giúp bạn xác định vị trí kết nối chính xác của các Pattern mà giải quyết vấn đề của bạn.”
Singleton Pattern trong PHP
Một Class có một instance (sự thể hiện) Nó cung cấp truy cập global trỏ tới nó Ví dụ sau sẽ giải thích về khái niệm singleton trong PHP
<?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()
{
}
Trang 2}
class SingletonChild extends Singleton
{
}
$obj = Singleton::getInstance();
var_dump($obj === Singleton::getInstance());
$anotherObj = SingletonChild::getInstance();
var_dump($anotherObj === Singleton::getInstance());
var_dump($anotherObj === SingletonChild::getInstance());
?>
Ví dụ trên được triển khai dựa trên việc tạo phương thức tĩnh là getInstance()
Factory Pattern trong PHP
Một Class tạo đối tượng và bạn muốn sử dụng đối tượng đó Ví dụ sau giải thích về Factory trong PHP
<?php
class Automobile
{
private $bikeMake;
private $bikeModel;
public function construct($make, $model)
{
$this->bikeMake = $make;
$this->bikeModel = $model;
}
public function getMakeAndModel()
{
Trang 3return $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
Trang 4{
public static function create($make, $model)
{
return new Automobile($make, $model);
}
}
t$pulsar = AutomobileFactory::create('ktm', 'pulsar');
print_r($pulsar->getMakeAndModel());
?>
Khó khăn chính với Factory Pattern là nó sẽ tăng tính phức tạp và nó không đáng tin cậy cho các lập trình viên giỏi
Strategy pattern trong PHP
Strategy Pattern tạo một Family Algorithm và bao đóng mỗi giải thuật Ở đây, mỗi giải thuật nên là
có thể trao đổi bên trong Family đó
<?php
$elements = array(
array(
'id' => ,
'date' => '2011-01-01',
),
array(
'id' => ,
'date' => '2011-02-01'
)
);
$collection = new ObjectCollection($elements);
$collection->setComparator(new IdComparator());
$collection->sort();
Trang 5
echo "Sorted by ID:\n";
print_r($collection->elements);
$collection->setComparator(new DateComparator());
$collection->sort();
echo "Sorted by date:\n";
print_r($collection->elements);
?>
Mô hình MVC trong PHP
MVC là viết tắt của Model View Control Trong đó, View hoạt động như GUI, Model hoạt động như Back End và Control hoạt động như một Adapter Ở đây, 3 phần được liên kết với nhau Nó sẽ truyền data và truy cập data giữa chúng với nhau