Rhs
Rhs

Reputation: 3318

Java Abstract Class Constructor Scope

I was wondering what would be the best way to approach this problem:

I have an abstract class as follows:

public abstract class AbstractTestClass
{
    public AbstractTestClass(String text)
    {
        SomeObject someObject = SomeStaticClass.getSomeObjectFromText(text);
        //etc...
        this.property1 = someObject.property1
        //basically populate properties based off of someObject
    }
}

And an example implementation:

public class AbstractTestClassImpl
{
    public AbstractTestClassImpl(String text)
    {
        super(text);
        SomeObject someObject = SomeStaticClass.getSomeObjectFromText(text);
        //do stuff with someObject that isn't done in the super constructor because it may vary per different implementations of the abstract class
    }
}

So from this example code snippet, someObject is created twice, and I was wondering if its possible to just create it once or to just leave this as is.

The obvious solution would be to make someObject a member variable in the AbstractClass, however, I also have the case where someObject is not required at all.

Consider the constructor:

public AbstractTestClass(int i, int j, char c)
{
    //do stuff where someObject is not required
}

In this case, it would not make sense to have a someObject member since its not used at all.

Assume that someObject is only used in that one constructor.

What would be the best way to approach this?

Upvotes: 1

Views: 267

Answers (3)

Sachin Thapa
Sachin Thapa

Reputation: 3719

I won't comment on design, but given you have to do what you mentioned above, here is what i would suggest:

Move code from abstract class into init method which returns reference for object, in implementation class call init method of abstract class, use returned reference to do extra processing.

Hoping this solves your problem.

Cheers !!

Upvotes: 0

Brian Blain
Brian Blain

Reputation: 912

The biggest smell in your code is calling a static class inside a constructor instead of passing the value into the constructor as a parameter. What I think would help the most is asking

How can I test this?

Look into writing some unit tests in JUnit. PowerMockito provides the capability to mock static classes but in this scenario it wouldn't provide an effective test because the test code would have to understand the code instead of understanding the behavior.

I really enjoy this article Write Testable Code and occasionally reference it myself.

Upvotes: 2

tbodt
tbodt

Reputation: 17007

First, if you have the static factory method, you can return the same object for each call to the static factory method with the same text. If you can't do that, you could either make a protected instance variable or have a protected contstructor that takes a SomeObject parameter and only makes a new one if the parameter is null. The subclass can make the SomeObject and pass it to the superclass.

Upvotes: 0

Related Questions