传递属性到工厂方法 [英] Passing Properties to Factory method

查看:145
本文介绍了传递属性到工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回实现接口的工厂方法。事实是 - 实现具有不同的构造函数参数。



我的问题是 - 如何通过工厂方法将参数传递给接口的不同实现?



我有一个想法,但我不知道是否有意义 - 将Properties对象传递给工厂方法?这样,每个接口实现都可以获得它的构造函数所需的属性,而工厂界面将被统一起来。



这是否有意义,还是有更好的解决方案?



我决定加起来一个例子,所以我可以更好地澄清问题。



有接口 SomeAlgorithm ,我们有具体的算法,每个算法可能有不同的参数,例如

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

我想要做一些像

  SomeAlgorithm algo = AlgoFactory.getAlgo(algoName); 

我处理不同参数的方法是

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

然后,如果算法有参数,AlgoFactory可以将适当的属性传递给具体的算法构造函数Algo1没有参数)。如果某些属性不存在,则可以传递默认值(如果该算法中需要该值)。



正如你所看到的,我想要能够动态地改变算法。用户可以在运行时选择算法,并将适当的参数传递给属性对象。



这是否有意义?

解决方案

已修改问题的更新(rev43552065-8ee8-47e8-bc96-c660c3836998):



您的示例不是典型的工厂模式。如果您有三种算法需要通过名称引用,并为特定算法提供特定参数,那么为什么要使用工厂?您应该阅读着名的有效Java一书中的项目1:考虑静态工厂方法而不是构造函数。它描述了工厂方法的优点,没有一个我可以在你的例子中看到。






这里有很多解决方案问题,您可以在各种受欢迎项目中找到数百个例子。



例如,$ code DriverManager ,使用一个类似URL的字符串,其中包含可变格式的连接详细信息以及附加的属性对象,具有高级选项(示例)。



工厂方法通常应为足够抽象以获得一个工作实现,而不必为特定实现指定任何附加参数。它应该是隐藏实现细节。



如果事实证明是必要的,以传递附加/可选属性,通常是属性对象。



有不同的策略。例如, UrlConnection 是一个抽象类。可以通过调用 URL.openConnection()来检索实例,但是,许多选项只能通过转换返回的 UrlConnection 到一个特定的子类型,例如 HttpUrlConnection



我相信没有一个适合所有案例的解决方案,我很确定许多解决方案那可能甚至在Java标准库中都远远不够完美,但是你应该真正实现一些简单的东西,而不是浪费太多的时间来解决这些问题。


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?

解决方案

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.

这篇关于传递属性到工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆