开发者

Should I be using the observer pattern?

I have a situa开发者_如何学运维tion where I think the Observer pattern would make the most sense to use, but I'm not sure the best way to implement.

Say I have 2 tables

User
  id
  name
  email
  ...
Order
  id
  user_id
  total
  ...

Both are classes that extend from an ORM, and the parent class has a save() method

class User extends ORM { }

$user = new User(1);
$user->name = 'New Name'
$user->save();

I also have an xml feed, that stores some columns from user and some columns from order. When the feed is first accessed, it pulls the needed info from the database, and generates an xml feed, and stores the objects in memcached. Any subsequent hits to the feed pulls from memcached.

What I want to do is, if certain fields are changed in either user or order, I want to update the memcached key. Since I'm not storing all the fields from user, or all the fields from order, I don't want it to happen on just any save() call (also, the xml generation script is a little db-intensive)

From reading about the Observer pattern, I was thinking that the User/Order objects would also implement SplSubject, and in their constructors, attach an XMLObserver. I would have to override the save() method to also notify any attached observers. I would only call notify if certain fields were changed. The XMLObserver would update memcached.

class User extends ORM implements SplSubject
{
    private $observers = array();

    function __construct()
    {
        $this->attach(new XMLObserver);
        parent::__construct();
    }

    function attach(SplObserver $observer)
    {
        $id = spl_object_hash($observer);
        $this->observers[$id] = $observer;
    }

    function detach(SplObserver $observer)
    {
        $id = spl_object_hash($observer);
        unset($this->observers[$id]);
    }

    function notify()
    {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }

    function save()
    {
       if (in_array($this->changed, array('name')) {
         $this->notify();
       }
       parent::save();
    }
}

class XMLObserver implements SplObserver
{
    function update(SplSubject $subject)
    {
        // code here to update memcached key
    }
}

I'm not sure if this makes the most sense to me. Based on what I have so far, why wouldn't I just call a static method based on if those fields are changed (something like XMLFeed::update($key)). I'm not really seeing how using the observer is beneficial, but I might be going about this the wrong way.

Anyone know the best way to handle this?


Based from our comments, maybe you should consider writing a function that determines if a change should be propagated to memcache, (depending on the fields that were changed) instead of going about using an observer pattern.

Since memcache will be your only subscriber, that needs to always be subscribed, I don't think you'll benefit much from the benefits of the this pattern. From http://www.research.ibm.com/designpatterns/example.htm :

The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing their observers, and vice versa. It lets you add observers without modifying the subject or other observers.

There are also other benefits but I don't think you would benefit much from them in your scenario.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜