Description
- Create các object mà không cần chỉ định class cấu trúc.
Example:
Create view template for the page
interface ViewFactory
{
public function getPageTemplate(): PageTemplate;
public function getRenderer(): Renderer;
}
Create view template for the page
interface ViewFactory
{
public function getPageTemplate(): PageTemplate;
public function getRenderer(): Renderer;
}
Adapter is a structural design pattern, which allows incompatible objects to collaborate.
Xử lí cache (kết hợp abstract factory và adapter)
abstract class AdapterFactory
{
abstract public function getInstance(): AdapterInterface;
}
Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently.
Builder is a creational design pattern, which allows constructing complex objects step by step.
Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request.
Command is behavioral design pattern that converts requests or simple operations into objects.
Xử lí lưu post với các trạng thái yêu cầu là draft, no approval, pusblish,..
Class xử lí business logic cho post:
class PostReceiver
{
public function draft()
{
// do something
}
public function noApproval()
{
// do something
}
public function publish()
{
// do something
}
}
Command:
interface Command
{
public function excute();
}
Concreate command: class implement command define thực thi của Receiver tương ứng với case request.
class DraftCommand implements Command
{
protected PostReceiver $receiver;
public function __construct(PostReceiver $receiver)
{
$this->receiver = $receiver;
}
public function execute()
{
$this->receiver->saveDraft();
}
}
class NoApprovalCommand implements Command
{
protected PostReceiver $receiver;
public function __construct(PostReceiver $receiver)
{
$this->receiver = $receiver;
}
public function execute()
{
$this->receiver->saveNoApproval();
}
}
class PublishCommand implements Command
{
protected PostReceiver $receiver;
public function __construct(PostReceiver $receiver)
{
$this->receiver = $receiver;
}
public function execute()
{
$this->receiver->savePublish();
}
}
Invoker: Liên kết với command, gửi request đến command.
class Invoker
{
private Command $command;
public function setCommand(Command $cmd)
{
$this->command = $cmd;
}
public function run()
{
$this->command->execute();
}
}
Usage:
$invoker = new Invoker();
$postReceiver = new PostReceiver();
$invoker->setCommand(new DraftCommand($postReceiver));
$invoker->run();
To dynamically add new functionality to class instances.
Add function for the booking room.
Send notification
abstract class Notification
{
abstract public function getDriver(): NotificationDriver;
public function send(string $message): void
{
$driver = $this->getDriver();
$driver->connect();
$driver->send($message);
}
}
Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.
Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.