jeudi 17 août 2017

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. Each class has a List<Car> build(List<CarPart>) method

Now a requirement comes up that for any given List<CarPart> the optimal list of Cars must be returned.

Would it be an anti-pattern to put the logic in 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?. Is there a typical design pattern I can use to solve this problem?

Aucun commentaire:

Enregistrer un commentaire