DEV Community

Discussion on: Abstract Factory with builder

Collapse
 
dakujem profile image
Andrej Rypo • Edited

I believe your example is wrong.

Imagine a builder as an object that allows us to configure the object inside the builder that comes out at the end.

Imagine a factory as an interface with a create method and a type that comes out.

That's it, really.

If you need to combine the two,

$builder = new FactoryBuilder();

$factory = $builder
    ->installDieselEngines() // the factory will install diesel engines
    ->installAutomaticTransmissions() // with automatic transmissions
    ->build();   // build the factory
$car1= $factory->create();
$car2= $factory->create();

$anotherFactory = $builder
    ->installPetrolEngines() // this factory will install petrol engines
    ->installManualTransmissions() // with manual transmissions
    ->addPerformancePackages() // and will produce sports cars
    ->build();
$sportsCar= $anotherFactory->create();
Enter fullscreen mode Exit fullscreen mode

Please note that both factories should have the same interface (or be the same class), and the cars produced should have one too.

I will come back to you tomorrow, if it's not clear to you. I'm on a phone now. Let me know.

(I edited the answer for more explanation and legibility)