mercredi 14 juillet 2021

i'm looking for a design pattern for this

Here is what I'm doing for the code:

class Chef {
    public void cook(std::string str) {
        if (str == "Breakfast") {
            BreakfastChef breakfastChef = new BreakfastChef();
            breakfastChef.cook();
        }
        else if (str == "Lunch") {
            LunchChef lunchChef = new LunchChef();
            lunchChef.cook();
        }
        else if (str == "Dinner") {
            DinnerChef dinnerChef = new DinnerChef();
            dinnerChef.cook();
        }
        else if (str == "Dessert")
            DessertChef dessertChef = new DessertChef();
            dessertChef.cook();
        }
    }
};

Other notes:

There are lots of food types.

Each cook method is really long and has calls to various methods, which is why I made other chef classes instead of having one.

All special chef classes extend the normal chef class to call common methods.

Question:

I feel like I'm doing it wrong. It doesn't seem like it will hold up when I add more food types. Also creating a class to call one method feels like I'm not doing it right more.

What design should I go with? Is there is a magical design pattern that can perfect represent what I have?

I feel like this question is off topic lol and will probably be deleted.

Aucun commentaire:

Enregistrer un commentaire