
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
372
|
Chapter 16: The Observer Design Pattern
if (logLevel >= msgSeverity) {
lastMsg = new LogMessage(msg, msgSeverity);
setChanged( );
notifyObservers(lastMsg);
}
}
// Here's a revised message-sending method, debug( ),
// showing how handleMessage( ) would be used. (Other
// message-sending methods are not shown.)
public function debug(msg:String):Void {
handleMessage(msg, Logger.DEBUG);
}
Inheritance Misuse?
As we’ve just seen, in our implementation of the Logger class, we extend Observable.
In our situation, the Logger class doesn’t need to inherit from any other class, so it
can extend Observable without issue. But what if the Logger class were already a sub-
class of some other class (say, MessageManager)? It wouldn’t be able to inherit from
the Observable class! Here, we encounter a classic misuse of inheritance—extending
a class simply to borrow its functionality. We first saw this kind of misuse in
Chapter 6 and later showed how to avoid it in Chapter 8, under “Multiple Type
Inheritance with Interfaces.” Specifically, the Logger class doesn’t have a legitimate
“Is-A” relationship with the Observable class. That is, the Logger class is not a spe-
cialized kind of update broadcaster. Other classes won’t use it in place of Observable
for its additional broadcast features. On the contrary, ...