Re: All those constructors...

From: Michael Klaus (Michael.Klaus_at_gmx.net)
Date: 02/02/05


Date: Wed, 02 Feb 2005 23:42:51 +0100

Paul Chapman wrote:
> I have this fairly large class heirarchy, with all but the leaves
> abstract. One of the base class's fields is to be initialized with a value
> provided to a static factory method which selects the right subclass,
> *except* in one leaf case where it is replaced with something else.

I had a quite similar case and solved it (in my eyes fairly clean) by:
- providing a protected default constructor in the node class
- creating package visible setXXX() for the init parameters
- creating a public init() method which subclasses use for their constructor
code
- putting the factory into the same package with the node classes

The drawback is, you can not programatically ensure implementors use the
init() method instead of the constructor. This has to be done via
documentation. As I said: fairly clean ;o)



Relevant Pages

  • Re: Convenience constructors and non-final setters
    ... comfort severely (ok, I guess that argument doesn't count for OO-purists), ... init() method which would have been called before setSeed. ... constructor runs, initruns, setSeedruns, and the subclass ... In my opinion they should have made Random abstract, ...
    (comp.lang.java.programmer)
  • Re: is such exception handling approach good?
    ... an oracle database in the ctor then what should you do ... of them in constructor; sometimes all of them. ... void AllInOne::RetrieveData ... assert("Cannot init the buffer twice!"); ...
    (microsoft.public.vc.language)
  • Re: is such exception handling approach good?
    ... "Alex Blekhman" wrote: ... of them in constructor; sometimes all of them. ... void AllInOne::RetrieveData ... assert("Cannot init the buffer twice!"); ...
    (microsoft.public.vc.language)
  • Re: Error - Declaring static arrays of structs in a class (VC6)
    ... But I think it is also a good style to supply a constructor, and init the ... VC 7.1 is sometimes too forgiving to not-so-good style code. ...
    (microsoft.public.vc.language)
  • Re: Remoting service wont start
    ... I often see those kinds of errors in services because of things like syntax errors in the App.config file, or in the constructor, or on init. ... Does anyone know what might be causing it? ...
    (microsoft.public.dotnet.general)