
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Capturing Standard Output for a Process
|
475
classes. The Bar class contains no tracing code, the BarTraceInst class contains trac-
ing code only in its constructor (and potentially in its destructor), and the
BarTraceBehavior class contains tracing code only in specific methods. (The class
hierarchy provided in the Solution section is much simpler than classes that you
would create; this allows you to focus more on the design pattern and less on the
class hierarchy from which the factory creates classes.)
A
TraceFactory class that will act as your factory to create objects inheriting from the
abstract
Foo class is created. The TraceFactory class contains a single public method
called
CreateObj. This method attempts to instantiate an object that inherits from Foo
based on the preprocessor symbols defined in your application. If the following line
of code exists:
#define TRACE_BEHAVIOR
the BarTraceBehavior class is created. If this line exists:
#define TRACE_INSTANTIATION
the BarTraceInst class is created. If neither of these exists, the Bar class is created.
Once the correct class is created, it is returned to the caller. The caller never needs to
know which exact object is instantiated, only that it is of type
Foo. This allows you to
add even more classes to handle varying types and amounts ...