[英]Is extending a factory class an anti-pattern?
Let's say that there is a CarFactory class
and it is extended by classes
like FerrariFactory
and VolvoFactory
. 比方说,有一个CarFactory class
,它是由扩展classes
像FerrariFactory
和VolvoFactory
。 Each class has a List<Car> build(List<CarPart>)
method 每个类都有一个List<Car> build(List<CarPart>)
方法
Now a requirement comes up that for any given List<CarPart>
the optimal list of Cars must be returned. 现在出现了一个要求,对于任何给定的List<CarPart>
,必须返回最佳的Cars列表。
Would it be an anti-pattern to put the logic in CarFactory.build
? 将逻辑放入CarFactory.build
是一种反模式吗?
CarFactory
List<Car> build(List<CarPart>)
split car part list into optimal car part sub-lists for each factory
instantiate car factories and pass in the sub-list of car parts
return collection of cars
Or would it be better to have another class like a CarBuilder
class does this?. 还是让另一个类(例如CarBuilder
类)这样做会更好? Is there a typical design pattern I can use to solve this problem? 有没有可以用来解决此问题的典型设计模式?
I think, any logic in builder is anti pattern, because, instantiate new object should be very fast and quite. 我认为,构建器中的任何逻辑都是反模式的,因为实例化新对象应该非常快速且相当。 I mean, that maximum code in builder - only one operator new ClassName(params...)
. 我的意思是,构建器中的最大代码-只有一个运算符new ClassName(params...)
。 if you need any logic - move it to new small class, which solve this: 如果您需要任何逻辑,请将其移至新的小类中,这可以解决此问题:
List <Car> CarFactory.build (params...) {
return new CarLogicSolver(params).list();
}
you can see great examples in java.util.stream.Collectors.toSet, .toList, toMap etc. 您可以在java.util.stream.Collectors.toSet,.toList,toMap等中查看出色的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.