You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Henrik Stahl <he...@parallelconsulting.com> on 2001/01/12 16:43:17 UTC

DOMImplementationImpl (very) minor issue?

The common way of implementing singletons is to have a private static
variable
and a private default constructor. Shouldn't it be that way in the
DOMImplementationImpl class as well?

As I said, a very minor issue :-)

/Henrik Ståhl




Re: DOMImplementationImpl (very) minor issue

Posted by Henrik Ståhl <he...@iconmedialab.se>.
Java constructors are never inherited, but they can be called by using
super() calls. However, that is not very common in singleton
subclasses.

The static singleton variable would be better off private since it would
not risk modification from a mal-behaving subclass that way. The
two existing subclasses are (probably) properly implemented, but if I were to
implement my own subclass like...

public class MyDOMImplementationImpl extends DOMImplementationImpl {
    // no static variables means we "inherit" the DOMImplementationImpl one
    // by accident

    // also, we get a default constructor, which calls the superclass's
    // constructor, further adding to the confusion

    public static MyDOMImplementationImpl getDOMImplementation() {
      if (singleton == null) singleton = new MyDOMImplementationImpl();
      return singleton;
    }
}

...I would effectively overwrite the DOMImplementationImpl.singleton variable,

creating a hard-to-find bug. (IIRC)

Regards,

Henrik Ståhl


Haider Abbas Kazmi wrote:

> I think in most case that would be ok, but here private is a problem as
> there are two subclasses, one of which overrides the constructor of
> DOMImplementationImpl. Though I believe it would still be better to have
> protected constructor in this case.
>
> Like you said, a minor issue. ;)
>
> Henrik Stahl wrote:
> >
> > The common way of implementing singletons is to have a private static
> > variable
> > and a private default constructor. Shouldn't it be that way in the
> > DOMImplementationImpl class as well?
> >
> > As I said, a very minor issue :-)
> >
> > /Henrik Ståhl
>
> --
> Haider Abbas Kazmi - Java Mobile App. Developer
>


Re: DOMImplementationImpl (very) minor issue?

Posted by Haider Abbas Kazmi <ha...@sitraka.com>.
I think in most case that would be ok, but here private is a problem as
there are two subclasses, one of which overrides the constructor of
DOMImplementationImpl. Though I believe it would still be better to have
protected constructor in this case.

Like you said, a minor issue. ;)

Henrik Stahl wrote:
> 
> The common way of implementing singletons is to have a private static
> variable
> and a private default constructor. Shouldn't it be that way in the
> DOMImplementationImpl class as well?
> 
> As I said, a very minor issue :-)
> 
> /Henrik St�hl
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

-- 
Haider Abbas Kazmi - Java Mobile App. Developer