php設(shè)計模式之命令模式使用示例
命令類:
1.命令角色:聲明了一個給所有具體命令類的抽象接口。這是一個抽象角色。
2.具體命令角色:定義一個接受者和行為之間的弱耦合;實現(xiàn)execute方法,負(fù)責(zé)調(diào)用接受的相應(yīng)操作。execute()方法通常叫做執(zhí)行方法
3.客戶角色:創(chuàng)建一個具體命令對象并確定其接受者。
4.請求者角色:負(fù)責(zé)調(diào)用命令對象執(zhí)行請求,相關(guān)的方法叫做行動方法。
5.接受者角色:負(fù)責(zé)具體實施和執(zhí)行一個請求。
作用:
1.抽象出待執(zhí)行的動作以參數(shù)化對象。
2.在不同的時刻指定、排列和執(zhí)行請求。
3.支持取消操作
4.支持修改日志
<?php
//命令接口
interface Command{
public function execute();
}
//具體命令
class ConcreteCommand implements Command{
private $_receiver;
public function __construct($receiver){
$this->_receiver = $receiver;
}
public function execute(){
$this->_receiver->action();
}
}
//接受者
class Receiver{
private $_name;
public function __construct($name){
$this->_name = $name;
}
//行動方法
public function action(){
echo $this->_name.'do action .<br/>';
}
}
//請求者
class Invoker{
private $_command;
public function __construct($command){
$this->_command = $command;
}
public function action(){
$this->_command->execute();
}
}
//客戶端
class Client{
public static function main(){
$receiver = new Receiver('jaky');
$command = new ConcreteeCommand($receiver);
$invoker = new Invoker($command);
$invoker->action();
}
}
Client::main();
?>
相關(guān)文章
ThinkPHP中pathinfo的訪問模式、路徑訪問模式及URL重寫總結(jié)
這篇文章主要介紹了ThinkPHP中pathinfo的訪問模式、路徑訪問模式及URL重寫總結(jié),是ThinkPHP路由訪問的基礎(chǔ)知識,在ThinkPHP開發(fā)中非常重要,需要的朋友可以參考下2014-08-08ThinkPHP頁面跳轉(zhuǎn)success與error方法概述
這篇文章主要介紹了ThinkPHP頁面跳轉(zhuǎn)success與error方法概述,需要的朋友可以參考下2014-06-06phpstudy2018升級MySQL5.5為5.7教程(圖文)
這篇文章主要介紹了phpstudy2018升級MySQL5.5為5.7教程(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Symfony2實現(xiàn)在controller中獲取url的方法
這篇文章主要介紹了Symfony2實現(xiàn)在controller中獲取url的方法,實例分析了Symfony獲取URL的常用方法與使用技巧,需要的朋友可以參考下2016-03-03