Description:
Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request.
- Xây dựng 1 chuỗi quá trình xử lí cho đến khi 1 trong số đó xử lí được yêu cầu.
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();
Thay đổi hành vi (behavior) của object khi trạng thái bên trong của object thay đổi
Quản lí Order:
OrderManager:
class OrderManager
{
private OrderState $state;
public function __construct(OrderState $state)
{
$this->setState($state);
}
public function setState(OrderState $state)
{
$this->state = $state;
}
public function nextProceed()
{
$this->state->nextProceed($this);
}
public function handle()
{
return $this->state->handle();
}
}
OrderState:
interface OrderState
{
public function nextProceed(OrderManager $context);
public function handle();
}
CreatedState:
class CreatedState implements OrderState
{
public function nextProceed(OrderManager $manager)
{
$manager->setState(new PaymentState());
}
public function handle()
{
// do something
}
}
PaymentState:
class PaymentState implements OrderState
{
public function nextProceed(OrderManager $manager)
{
$manager->setState(new DoneState());
}
public function handle()
{
// do something
}
}
DoneState:
class DoneState implements OrderState
{
public function nextProceed(OrderManager $manager)
{
}
public function handle()
{
// do something
}
}
Xác định một nhóm thuật toán, đặt mỗi thuật toán vào một lớp riêng biệt và làm cho các đối tượng của chúng có thể hoán đổi cho nhau.
Xử lí tạo Post, sau khi tạo thành công thì gửi mail thông báo cho các member khác.
Post
class Post
{
public function create()
{
// do something to create a post
// Send mail
$this->notify();
}
protected function notify()
{
// send mail
}
}
Tách behavior ra khỏi object, từ đó có thể thêm behavior mới vào hệ thống mà không làm thay đổi code hiện tại
Quản lí các role: administrator và member. administrator thì có thể xem thông tin của member, còn member thì chỉ có thể xem thông tin của mình.