0

a child class extending a mother class which, in its constructor call a template method instianted in the child class because it need a value obtained in the child constructor.

How Can I do something like this, without changing my Parent constructor (Foo, here. I cannot do any changes in the real Foo class):

public abstract class Foo{
    public Foo(){
        register();
    }

    public abstract void register();

    public void aMethod(int aValue){
        // body
    }

}

public class Bar extends Foo{

    public Foo(int aValue){
        // body
    }

    register(){
        aMethod(aValue);
    }
}

Here, even if I put aValue in a field, aValue is not even created at

aMethod(aValue);.

How can I solve my problem?

I am looking for any pattern, ULM, solution.

Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72

3 Answers3

2

a child class extending a mother class which, in its constructor call a template method instianted in the child class because it need a value obtained in the child constructor.

Why doesn't the subclass constructor simply pass the value up to the superclass constructor?

public abstract class Foo {
    protected Foo(int value) {
       ... use value ...
   }
}

public class Bar extends Foo {
    public Foo(int value) {
        super(value);
    }
    ...
}

Note that calling virtual methods in a constructor is a dangerous practice, as the subclass won't have had the opportunity to initialize itself yet.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 for calling subclass methods being sketchy at best. And let's face it, you need the rep. – Dave Newton Aug 22 '12 at 17:35
  • Because the superclass is use in a private framework I use at my job and the class is read-only. I know it's a dangerous practice, but I did't find a better way.. Maybe there's a better way of doing it? – Pier-Alexandre Bouchard Aug 22 '12 at 17:40
  • @Pier-alexandreBouchard: That's impossible to tell without more context. If the class is fundamentally badly designed, you should campaign to fix the design. Otherwise it basically doesn't sound fit for purpose... – Jon Skeet Aug 22 '12 at 17:44
  • I fixed the design instead of doing a template method pattern. Thank you! – Pier-Alexandre Bouchard Aug 23 '12 at 15:06
0

Your class structure is pretty messed up. Do you have something this in legacy code that you cannot change parent ? If not change it:-)

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
0

Calling abstract method in a constructor is a bad design. Some tools like PMD indicates it as warning by default (or maybe even error). So it's hard to talk about any pattern here I think. What i would do is to implement Bar class as below.

public class Bar extends Foo{

    private int aValue;
    private boolean initialized = false;


    public Bar(int aValue){
        this.aValue = aValue;
        initialized = true;
        register();
    }

    public void register(){
       if (initialized)
       aMethod(aValue);
    }


}
czajek
  • 714
  • 1
  • 9
  • 23