使用带有MVC / Codeigniter网站的Observer模式

时间:2011-08-10 18:20:36

标签: codeigniter observer-pattern

我有一个网站,我正在转换为Codeigniter,我想简化和解耦。我喜欢我所读到的关于“新调查创建”(触发新的帮助票据,触发电子邮件等)的Observer模式。

但是如何在Code Igniter中实现这样的功能呢?我看到了Symfony组件,但此时我并不关心如何理解系统以及如何在控制器和模型中使用它。由于其他原因,我已经扩展了CI_Model和CI_Controller。将Observer模式代码放在哪里会是最好的吗?

我想象这样的观点:有人点击网站并产生一个请求,该请求被路由到控制器/操作:http://localhost/test/save_changes

// warning, pseudo-code!

class Test extends MY_Model
{
    public function __construct ()
    {
        // do I put this here?!? - or maybe in MY_Model?
        // Should it be a singleton?
        $this->load->library('dispatcher'); 
        // where do I attach what I want... here?
        $this->load->library('emailer');
        $this->dispatcher->attach($this->emailer); 
        // what if I have 50 possible things that might happen
        // based on any given event, from adding a user to 
        // deleting a survey or document? There has got to be a
        // way to attach a bunch of observers that trickle
        // down to each object, right?
    }

    public function save_changes ()
    {
         $this->load->model('user');
         $this->user->init($this->session->userdata('user.id'))->save();            
    }
}



class User extends MY_Model
{
    public function __construct ()
    {
        parent::__construct ();
        // do I put this here?!?
        $this->load->library('dispatcher'); // just something to call it
    }

    public function init($id)
    {
        if($this->_loadUser ($id))
        {
            $this->dispatcher->notify($this, 'user.loaded');
        }
    }

    public function save($id)
    {
        if(parent::save())
        {
            $this->dispatcher->notify($this, 'user.saved');
        }
    }



}

class Emailer
{
    public function update ($caller,$msg) 
    {
        switch ($msg)
        {
            case 'user.saved':
        // send user an email
        // re-cache some stuff
        // other things that we might want to do, including more of these:
        $this->dispatch->notify('user-saved-email-sent'); 
            break;
        }
    }
}

class Dispatcher
{
    public function notify ($caller, $msg) { ...foreach attached do $obj->update($caller,$msg) ...}
    public function attach ($obj) { ... }
    public function detach ($obj) { ... }
}

我可以看到它有多强大。但我不确定如何简化所有这些监听器/观察者的设置和附加。

也许我应该有一个工厂来创造它们?它似乎是肯定的,它们将与它当前的工作方式分离,但似乎管理我必须在每个控制器或方法中“附加”的所有不同对象将以不同的方式耦合。

谢谢, 汉斯

1 个答案:

答案 0 :(得分:0)

您建议的结构必须是:

$this->load->library('observer_factory', 'of'); // factory for creating observers
// Observer_factory would have knowledge/access to different classes which relate
// to the pattern.
$ync = $this->of->getNotifier( $some_variable ) );
$ync->attach( $this->of->getObserver( $some_other_variable ) );
$ync->attach( $this->of->getObserver( $some_final_variable ) );

$ync->someMethod(); // someMethod calls notify

但我很想知道。你有一个慢慢变得无所不知的工厂类。它开始篡夺Loader的功能。为什么在Observer_factory可以通过执行完全相同的操作来处理库时加载库?

我认为你最好使用一个库或一个知道它应该做什么并且设计得很好的模型,然后添加这个类结构。我认为收益不会超过成本。