初学者对于设计模式肯定存在着很多不明白之处,今天刚好周末,就抽出来点时间写了一个单件模式结合命令链模式打造系统核心的文章,可能对于部分人来说,文章内容过于浅显,这是送给初学者的教程,因为时间比较紧(要陪老婆逛街,呵呵),其中出现了设计不规范的,代码书写不规范的,bug等等还望各路大侠指出来,方便大家共同进步.本人水平有限.^_^ 相信大家都已经读过很多关于在php中应用设计模式的书籍或是文章,但是很少有直接给予实例,大部分看完之后有种迷迷糊糊的感觉,如果没有项目实践,很难将设计模式部分弄清楚. 为避免代码过于复杂.没有添加异常处理等内容. /* APP类用单件模式设计,它是系统的核心部分.相信大家看代码就知道了Load方法是载入APP类实例,相当于有些书籍中的getInstance 静态方法.他有addCommand,runCommand,removeCommand三个public方法.runCommand是核心部分.同时也是命令链模式的核心启动程序.具体实现请看源代码.代码写的已经很清楚了,就此不再赘述. 下面是系统的运行流程: APP startup addCommand(new someClass) Tips: This is the end of the explanation and examples. How you understand it and how to use this idea depends on your own understanding. Everything must be done by yourself. (ps: It can be made into a single unit in the framework. Entry file, whether to implement MVC or not depends on what you think.)
单件模式以及命令链模式的基础知识,大家自己google一下.不详细讲了.下面直接看实例:
*@author:NoAngels
*@time:08年08月30日
*/
interface IRunAction{
//获取类中定义的可以被APP中run的方法
static function LoadActions();
//类中的入口函数调用该类中其他函数用
function runAction($action, $args);
}
/*
*APP类系统的核心部分
*/
class APP{
static private $__instance = null;
static private $__commands = array();
static private $__flag = 1;
private function __construct(){}
//单件模式设计获取该类的唯一实例
static function Load(){
if(self::$__instance == null) self::$__instance = new APP;
return self::$__instance;
}
//添加命名到APP的$__instance中每次添加新命令的时候检查是否之前已经添加过一个该类的实例
//如果有就忽略操作如果没有就添加进来
public function addCommand($cmdName){
foreach(self::$__commands as $cmd){
if(strtolower(get_class($cmd)) == strtolower(get_class($cmdName))){
self::$__flag = 0;
break;
}
}
if(self::$__flag == 1) self::$__commands[] = $cmdName;
self::$__flag = 1;
}
//命令链模式设计的核心部分调用实例的入口函数
//首先检查是否在类中允许调用该操作如果没有就提示未定义操作退出
public function runCommand($action, $args){
self::$__flag = 0;
foreach(self::$__commands as $cmd){
if(in_array($action, $cmd->LoadActions())){
self::$__flag = 1;
$cmd->runAction($action, $args);
}
}
if(self::$__flag == 0){
self::$__flag = 1;
exit("undefined action by action : $action");
}
}
//删除某个类的实例,只要指定类的名字即可
public function removeCommand($className){
foreach(self::$__commands as $key=>$cmd){
if(strtolower(get_class($cmd)) == strtolower($className)){
unset(self::$__commands[$key]);
}
}
}
//供大家测试用看看是否添加以及删除成功
public function viewCommands(){
echo(count(self::$__commands));
}
}
//类User实现接口IRunAction
class User implements IRunAction{
//定义可以调用的操作
static private $__actions = array(addUser, modifyUser, removeUser);
//获取可以调用的操作,实际过程中不要直接就爱你个$__actions设计成public调用
//而应该设计一个LoadActions函数获取$__actions的值
static public function LoadActions(){
return self::$__actions;
}
//运行指定函数
public function runAction($action, $args){
//不明白这个函数使用的可以参看手册
call_user_func(array($this,$action), $args);
}
//测试函数而已
protected function addUser($name){
echo($name);
}
}
//类Test同类User
class Test implements IRunAction{
static private $__actions = array(addTest, modifyTest, removeTest);
static public function LoadActions(){
return self::$__actions;
}
public function runAction($action, $args){
call_user_func(array($this,$action), $args);
}
protected function addTest($name){
echo($name);
}
}
//以下是测试代码
APP::Load()->addCommand(new User);
APP::Load()->addCommand(new User);
APP::Load()->addCommand(new User);
APP::Load()->addCommand(new User);
APP::Load()->runCommand(addUser, NoAngels);
APP::Load()->addCommand(new Test);
APP::Load()->runCommand(addTest, null);
类User,Test实现了接口IRunAction,这两个类中都定义了一个静态私有变量$__actions,为一数组,其中包含了可以被APP的runCommand函数调用的操作.
-------addCommand, add the class to which the operation to be run belongs to the APP. If the added class is designed in singleton mode, you can add addCommand(SingletonClass:: Load()). Otherwise, you can adjust it as follows
-------runCommand. Run operations. For example, there is an operation addUser in the User class. I can directly enable runCommand($acttion, $args). Loop through the APP $__commands array, if an instance of a class has the operation, call the runAction function of the instance. If you do not add an instance of a class using addCommand, it will prompt an undefined operation and exit.
In the class RunAction in User and class Test calls call_user_func, a very commonly used function. Call the corresponding function in this class.