mardi 24 décembre 2019

How to use builder pattern when the resulting product requires different input

Let's say I want to create builders for ModernCar and FlintstonesCar, but they requires different materials for example

class ModernCarBuilder
{
    BuildEngine(Steel);
    BuildDoor(Steel);
    BuildTire(Rubber);
}

class FlintstonesCarBuilder
{
    BuildEngine(Wood);
    BuildDoor(Wood);
    BuildTire(Stone);
}

How can I create a common interface between the 2?

One way I can think of is like below, but it seems anti-pattern to me

class ModernCarBuilder
{
    ModernCarBuilder(Steel, Rubber); // constructor
    BuildEngine();
    BuildDoor();
    BuildTire();
}

class FlintstonesCarBuilder
{
    FlintstonesCarBuilder(Wood, Stone);
    BuildEngine();
    BuildDoor();
    BuildTire();
}

Aucun commentaire:

Enregistrer un commentaire