How to return different types using singleton instance method

How can I return an instance of another class in my contractor class?
my pseudo code:

class test
{
    private test() {}

    public static test getInstance() 
    {
        if(Condition)
        {
            return new otherClass();
        }
        else
        {
            return new test();
        }
    }
}

      

+3


source to share


2 answers


first and foremost: classes in java start with an uppercase letter (by convention)

in general, a parent explicitly creating their own son is bad practice, but what you can do is get a factory that will build it for you, so the "son" will not be created by the parent.

The factory can be loaded from an external source (ETC file props) or specified in the getInstance signature, in which case it should not be a member of the class.



note that the factory will need to be able to construct your class, so it must either be a subclass of Test or in the same package, either way the constructor must be protected, or you will have to provide a way to build in a protected ...

the example below shows 2 methods for this: in the first case the logic is in the "Test" class and in the other the logic is in the factory, both have their pros and cons, I would go with the 1st since it encapsulates the logic more " the right "way ...

class Test
{
    private TestFactory factory = null
    protected Test(...) {...}

    public static Test getInstance() 
    {
        if(Condition)
        {
            return factory.getOtherClass();
        }
        else
        {
            return factory.getTestClass();
        }
    }

    public static Test getInstance2() 
    {
       return factory.construct(Condition_Params);
    }
}

public abstract class TestFactory {

    public static Test getTestClass(){
        return new Test();
    }

    public static Test getOtherClass(){
        return new OtherTest();
    }
}

      

0


source


The return type of your method getInstance()

should reflect the general type of all the class types that you can return, i.e. it will be a supertype or generic interface common to all of these classes.

eg.

public Vehicle getInstance() {
   if (...) {
     return new Car();
   }
   else {
     return new Bus();
    }
}

      

So, in the example above, Car

and Bus

will derive from Vehicle

( Vehicle

could be superclass or interface). Otherwise, the contract between the return type of the method and the return type will be violated. Note that you can choose any super type in the inheritance hierarchy - anything that is common to both (if you return Object

or something similar simplified, you cannot usefully work with the object without discarding it, and that the sign of that, that something is wrong in your design)



Your calling code just waits and receives Vehicle

(and doesn't care Car

or Bus

)

   Vehicle v = Factory.getInstance();
    v.turnIgnitionOn();
    v.drive();

      

See this article for details . We use the same abstraction Vehicle

as I described above.

+3


source







All Articles