喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

[北京]代朋友公司招聘PHP高级程序员多名

上一主题 下一主题
     
标题: [原创] php面向对象设计模式(命令模式)  
 
void
注册会员
Rank: 2



UID 93558
精华 0
积分 70
帖子 78
金钱 70 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2007-5-24
状态 离线
php面向对象设计模式(命令模式)

AppMain.php
<?
//+---------------------------------------命令(Command)模式------------------------------------------------

//一般性常见代码
require_once('Receiver.php');
require_once('ICommand.php');
require_once('Command.php');
require_once('Invoker.php');

//组件装配关系如下:

//构造命令接收者对象
$receiver = new Receiver();  
$receiver->name = 'Receiver服务组件';
$receiver->desc = '提供XX业务处理';  

//构造具体命令对象,并指定命令的接收者
$command = new Command();         
$command->receiver = $receiver;

//构造命令调用者对象,并指定一个命令
$invoker = new Invoker();
$invoker->command = $command;

//调用命令
$invoker->executeCommand();

/*
以上代码属于强偶合关系,具体依赖文件路径、文件名、类名,可替换性很差(如果组件有新的实现,只有覆盖以前的代码文件)
以下代码引入IOC进行解偶(优点:没有具体的依赖,依赖控制在配置文件中)
*/

/*
引入IOC进行组件装配,装配过程就是把组件所依赖的零部件(对象)给组件安装上去
站在单个组件层面上看,它所需要的零部件是由外部给它安装的
*/
require_once('Ioc/ClassFactory.php');
$factory = new ClassFactory();  
$factory->getObject('invoker')->executeCommand();
?>

组件配置信息
DAOConfig.php
<?
//+---------------------------------------------------------------------------------------------------
  //Desc:各组件对象的配置信息


//命令接收者组件
$objects[] = array(
'id'         => 'receiver',        //容器通过id标识获取对象配置信息
'enable'     => true,              //true 代表启用该组件,false 代表禁用该组件
'source'     => 'Receiver.php',    //组件的路径(具有可替换性)
'className'  => 'Receiver',        //组件的类名(具有可替换性)
'property'   => array(
     'name'  => 'Receiver服务组件',
         'desc'  => '提供XX业务处理'
));

//具体命令组件
$objects[] = array(
'id'         => 'command',
'enable'     => true,
'source'     => 'Command.php',
'className'  => 'Command',
'import'     => array('ICommand.php'),  //命令接口
'ref'        => array('receiver'=>'receiver') //依赖的组件对象
);

//调用者组件
$objects[] = array(
'id'         => 'invoker',
'enable'     => true,
'source'     => 'Invoker.php',
'className'  => 'Invoker',
'ref'        => array('command'=>'command') //依赖的组件对象
);
?>

级联配置文件

MapConfig.php
<?
$source[]['source'] = 'Config/IocConfig/DAOConfig.php';   //业务逻辑对象配置
?>

Receiver.php
<?
//负责具体实施和执行一个请求
class Receiver
{
        private $name = null;    //组件名称
        private $desc = null;    //组件描述

        /**
         +----------------------------------------------------------
         * 类的构造子
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __construct()
        {
        }

        /**
         +----------------------------------------------------------
         * 类的析构方法(负责资源的清理工作)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __destruct()
        {
                $this->name = $this->desc = null;
        }

        /**
         +----------------------------------------------------------
         * 属性访问器(写)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function __set($name,$value)
        {
                if(property_exists($this,$name))
                {
                        $this->$name = $value ;
                }
        }

        /**
         +----------------------------------------------------------
         * 执行业务操作
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function action()
        {
                print "组件名称: $this->name <br>";
                print "组件描述: $this->desc <br>";
                print "执行业务逻辑的相关操作<br><br>";
        }
}
?>

ICommand.php
<?
//声明一个给所有具体命令类的抽象接口
interface ICommand
{
        //发送命令请求
        function execute();
}
?>

Command.php

<?
//定义一个接受者和行为之间的弱耦合,负责调用接收者的相应操作

class Command implements ICommand
{
        //命令接收对象
        private $receiver = null;  

        /**
         +----------------------------------------------------------
         * 类的构造子
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __construct()
        {
        }

        /**
         +----------------------------------------------------------
         * 类的析构方法(负责资源的清理工作)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __destruct()
        {
                $this->receiver   = null;
        }

        /**
         +----------------------------------------------------------
         * 属性访问器(写)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function __set($name,$value)
        {
                if(property_exists($this,$name))
                {
                        $this->$name = is_object($value) ? $value : null;
                }
        }

        /**
         +----------------------------------------------------------
         * 发送命令请求
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function execute()
        {
                if(is_object($this->receiver))
                {
                        $this->receiver->action();
                }
                else
                {
                        print  "执行过程中出现异常<br>";
                }
        }
}
?>

Invoker.php
<?
//负责调用命令对象执行请求
class Invoker
{
        //命令对象
        private $command = null;

        /**
         +----------------------------------------------------------
         * 类的构造子
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __construct()
        {
        }

        /**
         +----------------------------------------------------------
         * 类的析构方法(负责资源的清理工作)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */       
        public function __destruct()
        {
                $this->command   = null;
        }

        /**
         +----------------------------------------------------------
         * 属性访问器(写)
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function __set($name,$value)
        {
                if(property_exists($this,$name))
                {
                        $this->$name = is_object($value)  ? $value : null;
                }
        }

        /**
         +----------------------------------------------------------
         * 调用命令对象执行请求
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function executeCommand()
        {
                if(is_object($this->command))
                {
                        $this->command->execute();
                }
                else
                {
                        print  "执行过程中出现异常<br>";
                }
        }
}
?>
2008-6-29 09:52 PM#1
查看资料  发短消息  顶部
 
netqiu (netqiu)
新手上路
Rank: 1
初级会员



UID 66394
精华 0
积分 31
帖子 22
金钱 31 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2005-9-2
状态 离线
[推荐阅读] 不知道现在的php新的版本支持多线程么,如果有最低的版本是多少?
我来了,不错,学习
2008-6-29 10:08 PM#2
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.027115 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP