I want to use an observer pattern for a logging system.
We have logObserversand logObservables.
A class that will have to register something will implement iLogObservableand include the following methods:
private $logObservers = array();
public function addLogObserver($logObserver) {
$this->logObservers[] = $logObserver;
}
public function removeLogObserver($logObserver) {
this->logObservers[] = $logObserver;
}
public function write($type, $message) {
foreach($this->logObservers as $logObserver) {
$logObserver->log($level, $message);
}
}
Then I noticed that many classes that will use logging will have these methods, and I need to copy the insert. So it isn’t better for these methods to be in a class that I am calling, LogObservableor simply Log, and then use a strategy (create an instance of this class inside all the classes that will have to register). When I change the methods in Log, everyone logObservableswill be affected.
, , - , .
?