5

I have an object Carconstructed using builder pattern. Issue was a lot of code redundancy.

    Car.Builder builder = new Car.Builder("Mercedes");
    builder.numDoors(carConfig.getNumDoors()
    builder.engineType(carConfig.getEngineType())
    builder.isManual(isManual)
    builder.build())

    Car.Builder builder = new Car.Builder("BMW");
    builder.numDoors(carConfig.getNumDoors()
    builder.engineType(carConfig.getEngineType())
    builder.isManual(isManual)
    builder.build())

    Car.Builder builder = new Car.Builder("FlyingCar");
    builder.numDoors(carConfig.getNumDoors()
    builder.engineType(carConfig.getEngineType())
    builder.wing(TypeName)
    builder.build())

So thought of an idea to reduce code clutter using factory pattern. Using the factory below, I can create Car easily.

Car mercedes = CarFactory.createNonFlyingCar("Mercedes");

Car bmw = CarFactory.createNonFlyingCar("BMW");

Car flyCar = CarFactory.createFlyingCar("FlyingCarBrand");

public class CarFactory {

    // brand can be BMW or Mercedes or Audi etc.
    public static Car createNonFlyingCar(String brand, CarConfiguration carConfig, Engine engine) {
        Car.Builder builder = new Car.Builder(brand);
        append(builder, carConfig.getNumDoors(), carConfig.getEngineType(), engine.isManual()).build();
    }

    public static Car createFlyingCar(CarConfiguration carConfig, Wings wings) {
        Car.Builder builder = new Car.Builder("NewFlyingCar");
        append(builder, carConfig.getNumDoors(), carConfig.getEngineType()).wings(wing.getType()).build(); // added extra 'wings' method here.
    }

    // overload append.
    private static void append(
        Car.Builder builder,
        int numDoors,
        Sting engineType,
        boolean isManual;
    )
    {
        return append(builder, numDoors, engineType).isManual(isManual);

    }

    private static void append(
        Car.Builder builder,
        int numdoors,
        String engineTye
    )
    {
        return builder.numdoors(numdoors).engineType(engineTyoe);
    }

}
  1. One thing which I did in the factory was overloading append method. When common fields were involved, used them. Eg: numdoors and enginetype was shared between flying and nonflying cars. so I created an append(numdoors, engineType) which was called by append with more param.

  2. Next thing is the wing which was unique to flying car was left out of append.

Is this a known pattern ? Good ? Bad ? And Why ?

4
  • 3
    Why don't you just pass your car config object to the constructor of your Car class and be done with it? Jan 28, 2017 at 2:03
  • These oversimplified examples are always nonsensical. Jan 29, 2017 at 2:58
  • Sorry if this sounds cynic and unhelpful, but: as long as you understand, what you are doing and as long the complexity of your application - meaning: others could understand what and why you were doing it this way - doesn't suffer, you are free to mix patterns to your likings. There is no law against it. Jan 29, 2017 at 12:36
  • I don't about the above example but I have seen few people doing the same i.e. using factory for getting the type of object to create and then using builder as a common class to create all sorts of object which seems too much clutter to me and. if you are having the builder for each type class then again you are doing the coding duplication. Nov 18, 2020 at 19:33

1 Answer 1

2

Factories really have to do with abstractions and polymorphism, e.g. create an instance for some interface or abstract class without specifying the concrete implementation. You have only one concrete class Car in this case.

So both your variants are builders, the first one has a lot more clarity. The second one, the one you call factory is not communicating intent (I really need to analyze it thoroughly to figure out what it is doing). I don't like it at all, it's make some very simple convoluted.

Why do you spawn of a builder from a new instantiated Car? That seems weird to me, builder should instantiate Car, not the other way around.

Leading from this. If your Car has a default parameterless constructor then why do you even have the builder? You could just new up the Car and set properties against it coming from carConfig? The builder does not seems to provide any guarantee, defaults or otherwise complete logic on how to construct a car.

You could consider introducing a FlyingCar class with a wing property, the patterns will start to make more sense then. You builder could expose wing configuration options, only when you are constructing a FlyingCar etc.

1
  • Why do you spawn of a builder from a new instantiated Car? - I dont, it was a typo Jan 29, 2017 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.