Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

java - Passing Properties to Factory method

I have a factory method which returns implementation of an interface. The thing is - implementations have different constructor parameters.

My question is - how to pass parameters through factory method to different implementations of the interface?

I have an idea, but I'm not sure if it makes sense - pass Properties object to factory method? This way each of interface implementations can get the properties that needs for its constructor, while factory interface will be unified.

Does this make sense, or there is a better solution?

I decided to add-up an example, so I could clarify the problem better.

Let's say we have interface SomeAlgorithm and we have concrete algorithms, where each algorithm may have different parameters, e.g.

SomeAlgorithm algo = new Algo1();
SomeAlgorithm algo = new Algo2(noOfIterations);
SomeAlgorithm algo = new Algo3(precision, boundary);

I would like to be able to do something like

SomeAlgorithm algo = AlgoFactory.getAlgo("algoName");

My approach to handle different parameters would be

SomeAlgorithm algo = AlgoFactory.getAlgo("algoName", properties); 

Then, AlgoFactory could pass appropriate properties to concrete algorithm constructor, if the algorithm has parameters at all (e.g. Algo1 doesn't have parameters). If some property is not present a default value could be passed (if that value is required in the algorithm).

As you can see I would like to be able to dynamically change algorithm. User would select algorithm in runtime and pass appropriate parameters which would be put into properties object.

Would this make sense?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update for edited question (rev43552065-8ee8-47e8-bc96-c660c3836998):

Your example is not a typical factory pattern. If you have three algorithms which you need to reference by name AND provide specific parameters for a particular algorithm, why would you want to use a factory? You should probably read "Item 1: Consider static factory methods instead of constructors" from the famous book "Effective Java". It describes the advantages of factory methods, none of which I can see in your example.


There are many solutions to this problem, and you can find hundreds of examples in all kinds of popular projects.

For example, the DriverManager class, uses an URL-like string which contains connection details in a variable format and an additional Properties object with advanced options (example).

A factory method should usually be abstract enough to get a working implementation without having to specify any additional parameters for a specific implementation. It is supposed to "hide" implementation details.

If it turns out to be necessary to pass additional / optional properties, it is quite common to pass a Properties object.

There are different strategies. For example, UrlConnection is an abstract class. Instances can be retrieved by calling URL.openConnection(), however, many options can only be set by casting the returned UrlConnection to a specific sub-type, e.g. HttpUrlConnection.

I believe there is no single solution which suits all cases and I am pretty sure that many solutions out there, possibly even in the Java standard library, are far from perfect, but you should really implement something simple instead of wasting too much time with such problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...