Alternatives to using overridden methods in constructors, Java

拥有回忆 提交于 2020-01-24 10:22:13

问题


In a Java project I am coding I have ended up using methods that are overridden in constructors. Something like:

class SuperClass {
    SuperClass() {
        intialise();
    }

    protected void initialise() {
        //Do some stuff common to all subclasses
        methodA();
        methodB();
    }

    protected abstract void methodA();

    protected abstract void methodB();
}

class SubClass1() {
    SubClass() {
        super();
    }
    protected void methodA() { //Do something }
    protected void methodB() { //Do something }

}

class SubClass2() {
    SubClass() {
        super();
    }
    protected void methodA() { //Do something else }
    protected void methodB() { //Do something else}

}

I now realise, that although in my case it works fine, it is somewhat dangerous since SubClass methods are called on an object that has currently only been constructed as a SuperClass object (something that may be overlooked when new classes that extend SuperClass are added in the future). It also wouldn't work in c++ due to differences in how objects are created.

The only way I can think to get round this is to move the initialise method call down to the concrete classes constructor:

   class SuperClass {
    SuperClass() {            
    }

    protected void initialise() {
        methodA();
        methodB();
    }

    protected abstract void methodA();

    protected abstract void methodB();
}

class SubClass1() {
    SubClass() {
        super();
        initialise();
    }
    protected void methodA() { //Do something }
    protected void methodB() { //Do something }

}...

Is this the common way to over come this issue? It seems a shame (and easy to forget) that all further classes that extend SuperClass need to remember to call initialise().

I also found myself doing something similar in a more complicated situational that uses a Factory Method in a constructor, which is overridden in subclasses to decide which concrete class to implement. The only other way I can think to get round this and keep the design pattern as is, is to perhaps construct in a two phase process; i.e. construct with the bare minimum, and then call a second method to finish off the job.


回答1:


This is really not a good idea as your Subclass will not be properly constructed when its methodA() and methodB() are called. That would be very confusing for people extending the class. Recommend you use an abstract init() instead, as suggested by dlev in his/her comment.




回答2:


Objects that need initialization as complex as this would really need to be created through factory methods. You do mention a factory, but being called from a constructor, so that doesn't sound like the straightforward approach, either. If you simply had a factory in the base class, publicly invisible constructors, and a mechanism to decide which concrete class to return, that factory would easily enforce the initialization policy.




回答3:


I wonder if you're making things more complicated than they need to be.

In your example, the stuff done by each implementation of methodA could be moved to the constructor of the class that does the implementation. So instead of using SubClass::methodA, just move the logic into the SubClass constructor.

If you do this you gain clarity at the expense of some potentially hard to understand control over the order the various initialization bits get executed.




回答4:


What Bruce Eckel suggests in his "Thinking in Java" is making your methodA() and method B() of the class SuperClass final or private (which is implicitly final) so they will be accessible from the superclass constructor though no derived class will have access to these methods and, thus, no dangerous overriding will be possible - any mehtods with similar signature declared in any derived classes will be simply new methods.



来源:https://stackoverflow.com/questions/11585424/alternatives-to-using-overridden-methods-in-constructors-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!