
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
378
|
Chapter 16: The Observer Design Pattern
both the inheritance-based and composition-based implementations of the Observer
pattern shown in this chapter.
First, we import the logger package (so we can refer to the Logger, LogMessage,
OutputPanelView, and TextFieldView classes in that package directly):
import logger.*;
Then, we create a variable, log, to store our application’s Logger instance:
var log:Logger;
Then we create the Logger instance:
log = Logger.getLog( );
Next, we create two variables to store our Logger observers:
var outputLogView:OutputPanelView;
var textLogView:TextFieldView;
Then we create our observer instances, passing each the Logger instance for this
application:
outputLogView = new OutputPanelView(log);
textLogView = new TextFieldView(log, someMovieClip, 0, 50, 50, 300, 200);
Finally, we use addObserver( ) to register our observers to receive updates from the
Logger instance:
log.addObserver(outputLogView);
log.addObserver(textLogView);
Our log’s ready to go! Let’s now try logging some messages:
log.fatal("This is a non-recoverable problem.");
log.error("This is a serious problem that may be recoverable.");
log.warn("This is something to look into, but probably isn't serious.");
log.info("This is a general bit of application information.");
log.debug("This is a note that ...