
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Enabling and Disabling Complex Tracing Code
|
473
Solution
Use the TraceFactory class shown in Example 8-6, which implements the factory
design pattern to allow creation of an object that either generates tracing informa-
tion or does not.
The class hierarchy for the
Bar, BarTraceInst, and BarTraceBehavior classes is shown
next. The
BarTraceInst class contains only the constructor tracing code, the
BarTraceBehavior class contains tracing code only within specific methods, and the
Bar class contains no tracing code:
public abstract class Foo
{
public virtual void SomeBehavior( )
{
//...
}
}
public class Bar : Foo
{
Example 8-6. TraceFactory class
#define TRACE
#define TRACE_INSTANTIATION
#define TRACE_BEHAVIOR
using System.Diagnostics;
public class TraceFactory
{
public TraceFactory( ) {}
public Foo CreateObj( )
{
Foo obj = null;
#if (TRACE)
#if (TRACE_INSTANTIATION)
obj = new BarTraceInst( );
#elif (TRACE_BEHAVIOR)
obj = new BarTraceBehavior( );
#else
obj = new Bar( );
#endif
#else
obj = new Bar( );
#endif
return (obj);
}
}