On 4/3/02 1:09 PM, "Jon Scott Stevens" <[EMAIL PROTECTED]> wrote:

> on 4/3/02 8:51 AM, "Geir Magnusson Jr." <[EMAIL PROTECTED]> wrote:
> 
>>>  http://jakarta.apache.org/avalon/framework/inversion-of-control.html
>>> 
>> 
>> I knew that's what the Avalon-ers call it, and that's where the idea came
>> from, but I didn't want to stir that up...
> 
> Part of the problem with this Avalon design *IMHO* is that you more easily
> risk an NPE...for example, in the above URL, it has the following code:
> 
> class MyComponent
>   implements Component, LogEnabled
> {
>   Logger logger;
> 
>   public enableLogging(Logger newLogger)
>   {
>       this.logger = newLogger;
>   }
> 
>   myMethod() 
>   {
>       logger.info("Hello World!");
>   }
> }
> 
> Now, if the parent instantiates MyComponent and forgets to call
> enableLogging() *before* myMethod() is called, an NPE will be thrown and it
> will not be clear as to why that NPE was thrown unless you go and look at
> the source code. There is no open coding contract that says that
> enableLogging() needs to be called first.

Right - and with what I am thinking about, be defensive.  Don't just use
'logger' - wrap it with a check.

> 
> If you don't want the NPE to be thrown, then every time you want to log, you
> would have to do something like this:
> 
>   if (logger != null)
>   {
>       logger.info("Hello World!");
>   }
> 
> A real pain in the ass.

Or have an internal log method that does that....

> 
> I really don't understand why that is better than the Pull model which would
> be more like this:
> 
> class MyComponent
>   implements Component, LogEnabled
> {
>   private static Logger logger = Log.getLogger();
> 
>   myMethod() 
>   {
>       logger.info("Hello World!");
>   }
> }
> 
> Seems so much cleaner of a design to me and it is impossible to get an NPE.

Except that forces there to be something called Log in the classpath.  In
the case where you are using the component/tool in an environment where that
doesn't exist, you'll get a different exception, out of your control...



-- 
Geir Magnusson Jr.                                     [EMAIL PROTECTED]
System and Software Consulting
The question is : What is a Mahnamahna?


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to