对具有不同参数的类使用工厂模式
我有一个非常简单的工厂,它采用枚举作为其参数之一来确定应创建的对象的类型,以及所有正在创建的对象所共有的另一个参数。
当我为工厂添加更多类型来创建对象时,构造函数的参数开始有所不同,例如:
public class someFactory {
public someFactory() {
}
public SomeObject newObject(Type type, Object data) {
return this.newObject(type, data, "");
}
public SomeObject newObject(Type type, Object data, Object stringOrObject) {
SomeObject someObject = null;
if (type != null) {
switch(type) {
case CREATE:
someObject = new CreateObject(data);
break;
case DELETE:
someObject = new DeleteObject(data, (String)stringOrObject);
break;
case EDIT:
someObject = new EditObject(data, (Object)stringOrObject);
break;
default:
break;
}
}
return someObject;
}
}
我是否应该不使用工厂并仅使用正确的参数实例化不同类型,或者可以以某种方式改进上述内容以使它更灵活吗?
I have a very simple factory which takes an Enum as one of its parameters to determine the type of object that should be created, and a other parameter that's common to all the objects being created.
As I'm adding more types for the factory to create my object constructor's parameters are starting to differ, eg:
public class someFactory {
public someFactory() {
}
public SomeObject newObject(Type type, Object data) {
return this.newObject(type, data, "");
}
public SomeObject newObject(Type type, Object data, Object stringOrObject) {
SomeObject someObject = null;
if (type != null) {
switch(type) {
case CREATE:
someObject = new CreateObject(data);
break;
case DELETE:
someObject = new DeleteObject(data, (String)stringOrObject);
break;
case EDIT:
someObject = new EditObject(data, (Object)stringOrObject);
break;
default:
break;
}
}
return someObject;
}
}
Should I not be using a factory and just instantiate the the different types with the right arguments or can the above be improved somehow to make it more flexible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准的 Java 操作是将方法添加到枚举中。
正如 @Stas Kurilin 指出的,如果您可以避免枚举并仅调用适当名称和参数的静态创建方法,那么您可以解决许多问题。
(其他一些随机点:通常抛出异常比接受
null
或未知值更好。尝试使用强类型而不是Object
。坚持使用 Java 编码约定,例如大写的类型名称。)The standard Java thing to do is to add a method to the enum.
As @Stas Kurilin points out, if you can avoid the enum and just call static creation methods of appropriate names and parameters, then you solve many problems.
(A few other random points: It's generally better to throw an exception than accept a
null
or unknown value. Try to use strong typing rather thanObject
. Stick with the Java coding conventions, such as capitalised type names.)我将创建一个看起来像这样的界面,
然后您可以拥有一个 Factory 类,其中包含用于创建、删除和编辑的三个 IFactory 的列表,并且可以查询这些工厂的列表以查找第一个对 AppliesTo 方法响应 true 的工厂。
I would create an interface that looks like
You can then have a Factory class that contains a list of three of these IFactories for Create, Delete, and Edit and can query the list of these factories for the first one that responds true to the AppliesTo method.
创建具有以下签名的接口,
并让其他对象实现该接口。这样创作就留在了物体上。工厂模式不错。但是,由于您使用枚举来标识类型,因此最好使用多态性,以便它变得可维护。
Create a interface with the following signature,
and let other objects implement this interface. So that the creation remains with the object. Factory pattern is good. But, since you are using enums to identify the type, it would be better to use polymorphism, so that it becomes maintainable.