简体   繁体   English

使用Enum或String作为静态工厂方法?

[英]Use Enum or String for a static factory method?

Is it better to use enum or String to dispatch to the right object to create in a static factory method ? 使用enumString调度到正确的对象以在static工厂方法中创建是否更好?

Example with String : String示例:

public static Car createCar(String carName){
    if(carName.equals("Ferrari")){
        return new Ferrari();
    }
    else if(carName.equals("Porsche")){
        return new Porsche();
    }
    ....
}

Example with enum : enum示例:

public static Car createCar(CarEnum carEnum){
    if(CarEnum.FERRARI.equals(carEnum)){
        return new Ferrari();
    }
    else if(CarEnum.PORSCHE.equals(carEnum)){
        return new Porsche();
    }
    ....
}

For now, according to me : 就目前而言,据我说:

Benefit of using enum : 使用enum好处

  • Avoid that the user calls the factory with a non-treated carName . 避免用户使用未经处理的carName调用工厂。

Drawback of using Enum : 使用Enum缺点:

Increase dependencies because a change into enum ( FERRARI becoming ENZO_FERRARI for instance) will require modification on client. 增加依赖性,因为更改为enum (例如, FERRARI变为ENZO_FERRARI )将需要在客户端上进行修改。 However, with String we could redirect Ferrari to Enzo Ferrari instance without recompile client code. 但是,使用String我们可以将Ferrari重定向到Enzo Ferrari实例,而无需重新编译客户端代码。 Of course, we could make the same thing for client using old enum values with a redirection of FERRARI to ENZO-FERRARI enum but for me it implies that enum would have to keep old values to make client compatible... no make sense for me. 当然,我们可以为客户端使用旧的enum值和FERRARI重定向到ENZO-FERRARI enum做同样的事情,但对我而言,它意味着enum必须保留旧值以使客户端兼容......对我来说没有意义。

I would like to know what are your opinions on this subject? 我想知道你对这个问题有什么看法?

If, as you do in this case, you have a fixed number of acceptable arguments to a method, then it's best to enforce that constraint as best as you can on the clients of the method. 如果像在这种情况下那样,对方法有一定数量的可接受参数,那么最好在方法的客户端上尽可能地强制执行该约束。 In this case, accepting any old string would cause more failures than using an enum. 在这种情况下,接受任何旧字符串将导致比使用枚举更多的失败。

However, you might also consider, in a limited case like this, using named factory methods for each type of car: like createFerrari(), createPorsche() etc. 但是,在这种有限的情况下,您可能还会考虑为每种类型的汽车使用命名工厂方法:如createFerrari(),createPorsche()等。

Neither. 都不是。 Use an Enum and make the method an instance method of the Enum itself. 使用Enum并使该方法成为Enum本身的实例方法。 No if's required. 不,如果需要的话。

I would use Enums ; 我会用Enums ; I don't like Strings used in this case. 我不喜欢在这种情况下使用的字符串。 The advantage with enums is that you could switch over them (which will not work with strings) and you don't need to handle false input. 枚举的优点是你可以切换它们(这不适用于字符串),你不需要处理错误的输入。

I wouldn't add instance methods to enums. 我不会向枚举添加实例方法。 Today we saw strange unexpected behaviour when using static blocks in enums and refering to enums. 今天我们在枚举中使用静态块并引用枚举时看到了奇怪的意外行为。 Suddenly an enum instance was null!!!??? 突然一个枚举实例是空的!!! ???
I would use an abstract factory pattern. 我会使用抽象的工厂模式。 This way you could use the same factory for two enum instances. 这样,您可以为两个枚举实例使用相同的工厂。
I would also use a hashmap to store the factories. 我还会使用hashmap来存储工厂。 This way you don't have the case or if's which add cyclic complexity. 这样你就没有这种情况,或者是否会增加循环复杂性。

enum另一个好处是它允许你使用switch/case语法而不是无限if/else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM