开发者

using Observer pattern with a MVC/Codeigniter web site

I have a web site I'm converting to Codeigniter and I want to simplify and decouple. I like what I've read about the Observer pattern for things like "new survey created" (which triggers a new help ticket, which triggers an email, etc).

But how do I implement such a thing in Code Igniter? I see the Symfony component but at this point I'm not concerned about understanding the system as much as figuring out how to use it in controllers and models. I have extended both CI_Model and CI_Controller already for other reasons. Would putting Observer pattern code there be the best?

I imagine a point like this: someone hits the web site and spawns a request which gets routed to a controller/action: 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?
    }

    pub开发者_如何转开发lic 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) { ... }
}

I can see how powerful that would be. But I'm not sure how to simplify the setup and attaching of all of these listener/observers.

Maybe I should have a factory to create them all? It just seems like yes, they would be decoupled from the way it currently works, but it seems managing all the different objects that I'd have to 'attached' in each controller or method would become coupled in a different way.

Thanks, Hans


Your proposed structure would have to be something like:

$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

But I wonder about it. You'd have a factory class that slowly becomes all-knowing. It starts usurping the functionality of the Loader. Why load a library when my Observer_factory can handle it by doing exactly the same thing?

I think you're better off with a library or a model that knows what it is supposed to do and is well designed, then adding this class structure. I do not see the gains outweighing the costs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜