Observer pattern

<?php

/* Observer Interface */
interface Observer{
function register_change($subject,$args);
}

/* Observable Interface */
interface Observable{
function add_observer($observer);
}

/* Inbox Notifier */
class InboxNotifier implements Observer{
public function register_change($subject,$args){
echo "A new message has been added to your inbox: <br>$args";
}
}

/* Inbox Subject */
class MyInbox implements Observable{
private $_observers = array();
public function add_observer($observer){
$this->_observers []= $observer;
}
public function add_message($message){
foreach($this->_observers as $observer) $observer->register_change($this,$message);
}
}

$my_inbox = new MyInbox();
$my_inbox->add_observer(new InboxNotifier());
$my_inbox->add_message('This is a test message');
?>

0 Comments

You must be logged in to post a comment.