
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
366
|
Chapter 16: The Observer Design Pattern
The Logger Class
As the subject of our Observer implementation, the Logger class extends the
Observable class, using it to handle the grunt work of managing observers and broad-
casting state changes (in this case, log messages).
Here’s the skeleton for the Logger class:
class logger.Logger extends Observable {
}
To ensure that each application creates only one Logger instance, we use the Single-
ton design pattern, which we’ll study in the next chapter. The following aspects of
the Logger class are all part of the Singleton design pattern; we’ll skip consideration
of these items for now and return to them next chapter:
• The Logger class stores an instance of itself in a static property called
log
private static var log:Logger = null;
• The Logger constructor function is private so that Logger instances cannot be
created by outside code
• Logger instances can be created only via Logger.getLog( )
public static function getLog( ):Logger {
if (log == null) {
log = new Logger( );
}
return log;
}
As we learned earlier, the Logger class maintains a log severity level that is used to fil-
ter log messages. The severity levels are stored in static properties, as follows:
public static var FATAL:Number = 0;
public static var ERROR:Number = 1;
public static ...