1

I learn Java and wonder if there any difference between following implementation of class initialization.

[OPTION 1]

public class LaunchHandler implements SomeItf{

   public static LaunchHandler create(ArrayList<String> params){
        LaunchHandler self = new LaunchHandler(params);
        return self;
    }

   private LaunchHandler(ArrayList<String> params){
        mParams = params;
    }
}

So I call it as:

SomeItf  launch = LaunchHandler.create(params);

[OPTION 2]

public class LaunchHandler implements SomeItf{ 

   public LaunchHandler(ArrayList<String> params){
        mParams = params;
    }
}

I call it as:

SomeItf  launch = new LaunchHandler(params);

For me both options do the same but 1st option I saw it in big project.

What is the advantage of 1st Option?

Can somebody spread the light?

snaggs
  • 5,543
  • 17
  • 64
  • 127
  • 2
    You gotta have factory methods for everything. [Enterprise quality code, right here.](https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition) – Sotirios Delimanolis Dec 15 '16 at 18:05
  • Other duplicates: http://stackoverflow.com/questions/194496/static-factory-methods-vs-instance-normal-constructors http://softwareengineering.stackexchange.com/questions/240351/using-a-simple-static-factory-vs-instantiating-directly-new-thing-vs-factor – assylias Dec 15 '16 at 18:08

1 Answers1

2

As Sotirios Delimanolis wrote in the comments, the first one is the static factory pattern. Using this pattern your class cannot be extended as there's no access to the constructor.

Further, the static factory pattern can be used in other ways, for example, to control the number of instances that are created, or to return instances of different classes (which usually apply the same interface).

One private use-case is the famous Singleton pattern. Another one is object pooling.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • So As I understand you right the best practice is to use Factory pattern because it gives me additional options, right? – snaggs Dec 15 '16 at 18:12
  • @snaggs yes, it encourages coding against an interface instead of a specific class (better abstraction), which gives you a better flexibility in your implementation and modifications that you might want to apply along the way. Using a static factory is encouraged in the [effective java](http://www.informit.com/articles/article.aspx?p=1216151) - a book which is highly recommended for any Java programmer! – Nir Alfasi Dec 15 '16 at 18:20
  • if an interface is required yes. Otherwise no. Creating an interface just for creating an interface is noise. Look at this : http://stackoverflow.com/questions/956011/useless-interfaces – davidxxx Dec 15 '16 at 18:31
  • @davidxxx that's a matter of opinion. Make sure to read the last sentence (in brackets) of the accepted answer - in the link you posted. – Nir Alfasi Dec 15 '16 at 18:32
  • Indeed, I gave my opinion :) I read. It is in fact not very clear how it is written. I stress rather on the XP practice that the author refers : YAGNI (You ain't gonna need it) : don't add something which is not required. Interfaces may be defined for multiple reasons. If you don't have any concrete reason to use it but you use it, you add something is not required. This falls under YAGNI. – davidxxx Dec 15 '16 at 18:50