Yesterday i figured out a potential place in my current project where i can implement the Observer pattern.Yes!!! i did it immediately and happy to see my code has been so much decoupled.Let me share.
First of all what is Observer pattern and how it works.The answer is simple and straight-"Within the observer pattern, there are subjects, and there are observers. When something specific happens to the subject, all of the observers are notified.Subject doesn't have any idea about the observer's implementation". Interesting!!!!! isn't it?This simply reminded me the spy thriller where intel bugs the subject for close observation and later they are being notified of his/her every possible move.
Lets go to the point straight.Currently i am working in a system where user can post their thoughts and the friends of his/her circle can make comment on these posts.So when someone leaves a comment, three email sending activity get fired off:
First of all what is Observer pattern and how it works.The answer is simple and straight-"Within the observer pattern, there are subjects, and there are observers. When something specific happens to the subject, all of the observers are notified.Subject doesn't have any idea about the observer's implementation". Interesting!!!!! isn't it?This simply reminded me the spy thriller where intel bugs the subject for close observation and later they are being notified of his/her every possible move.
Lets go to the point straight.Currently i am working in a system where user can post their thoughts and the friends of his/her circle can make comment on these posts.So when someone leaves a comment, three email sending activity get fired off:
- The author of the post gets notified about the comment.
- All the friends of commenter will be notified.
- The commenter will get notified.
Subject:
namespace services;
class Comment\Comment extends Comment\ApplicationComment implements \SplSubject
{
protected $_observers=array();
public function postComment($data)
{
//get the form object
if($form->isValid($data))
{
//save comment
$this->notify();
}
}
//Other functions...
public function attach(SplObserver $observer)
{
$id = spl_object_hash($observer);
$this->_observers[$id] = $observer;
}
public function detach(SplObserver $observer)
{
$id = spl_object_hash($observer);
unset($this->_observers[$id]);
}
public function notify()
{
foreach($this->_observers as $observer)
{
$observer->update($this);
}
}
}
Observers:
namespace services;
/**
* Sends mail to the author of the posts
*/
class Mail\PostAuthor extends Mail\Mailer implements \SplObserver
{
public function update(Service\Comment $subject)
{
//get the comment details and call;i.e,$this->send($data)
}
public function send($data)
{
//Sends Mail
}
}
namespace services;
/**
* Sends mail to all the friends of commenter
*/
class Mail\CommenterFriends extends Mail\Mailer implements \SplObserver
{
public function update(Service\Comment $subject)
{
/**
* get the comment details
* get the friends of commenter
* notify each of them;ie,$this->send($data)
*/
}
public function send($data)
{
//Sends Mail
}
}
namespace services;
/**
* Sends mail to the commenter
*/
class Mail\Commenter extends Mail\Mailer implements \SplObserver
{
public function update(Service\Comment $subject)
{
/**
* get the comment details;ie,$this->send($data)
*/
}
public function send($data)
{
//Sends Mail
}
}
Testing the application
$serviceComment = new Comment\Comment(); $serviceComment->attach(new Mail\PostAuthor()); $serviceComment->attach(new Mail\CommenterFriends()); $serviceComment->attach(new Mail\Commenter()); //Wnen a comment is made $serviceComment->postComment($data);
Enjoy happy coding :).

No comments:
Post a Comment