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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…