strategy for observer pattern?

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-05-06T13:33:55Z Indexed on 2010/05/06 13:38 UTC
Read the original article Hit count: 281

Filed under:
|

I want to use observer pattern for a logging system.

We have got logObservers and logObservables.

The class that will have to log something will implement iLogObservable and include these 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 a lot of classes that will use logging will have these methods and I have to copy paste. So isn't it better to have these methods in a class I call LogObservable or just Log and then use strategy (instantiate this class inside all classes that will have to log). When I change the methods in Log, all logObservables will be affected.

However, I have not seen anyone use observer pattern with strategy pattern yet, but it seems to be very efficient and remove the duplications.

What do you think?

© Stack Overflow or respective owner

Related posts about design-patterns

Related posts about oop