I've created some classes and an interface to handle events for a small MVC application. My intention is to implement the Observer pattern, but I do not know if you agree, since I still confuse it with PHP. In the following code, did I do it the right way, or could it cause some maintenance problem in the future?
<?php
# Interface do listener
interface ActionListener {
public function actionPerformed(ActionEvent $e);
}
# O evento
class ActionEvent {
private $eventSource;
private $id;
private $command;
private $time;
public function __construct($eventSource, $id, $command = '') {
$this->eventSource = $eventSource;
$this->id = $id;
$this->command = $command;
$this->time = time();
}
public function getEventSource() {
return $this->eventSource;
}
public function getID() {
return $this->id;
}
public function getCommand() {
return $this->command;
}
public function getTime() {
return $this->time;
}
}
# Uma fonte de eventos.
class EventSource {
private $listeners = [];
public function notify($id, $command = '') {
$event = new ActionEvent($this, $id, $command);
foreach ($this->listeners as $actual) {
$actual->actionPerformed($event);
}
}
public function addActionListener(ActionListener $object) {
$this->listeners[] = $object;
}
}