Decision making, difficult conditions and ease of planning

I am trying to find an elegant way to implement a decision-making algorithm that makes it easy to maintain, because the conditions for making decisions can often change.

I will try to clarify an example:

Let's say I'm trying to manage a team of chefs in a restaurant kitchen.

Each chef knows how to cook 3 types of pies: apple pie, pumpkin pie and raspberry pie and 2 types of pizza: cheese pizza and bacon pizza. They all know how to cook everything.

Now I would like to send orders to these bosses regarding what will come for clients.

Conditions:

The manager can only cook one pie at a time. If I order a chef to make an apple pie, for example, I cannot order him to make a raspberry pie or pumpkin pie if the apple pie is not made, or I sent a request to cancel the apple pie.

I can ask the chef to cook up to 5 pizzas at the same time, considering it for different customers.

I would like to create an algorithm that returns a set of orders that I am allowed to send to a particular chef regarding what he is already preparing.

I am working with C ++. I could write a simple switch / case statement, but maintenance would not be easy if the conditions changed or new cakes were added, and therefore ...

, , .

?

+4
2

switch/case, , , ...

, , .

?

/ / / .

:

variable_t variable; // initialized elsewhere
switch(variable) {
case value1:
    do_action1();
    break;
case value2:
    do_action2();
    break;
// ...
}

:

struct Actor // actor is your "abstract chef"
{
    virtual ~Actor();
    virtual bool matches(variable_t const v) const = 0;
    virtual void do_action() = 0;
};

:

struct SweedishChef: public Actor {
    bool matches(variable_t const v) const override
    {
         return v == 1;
    }

    void do_action() override
    {
         std::cerr << "bork! bork!\n";
    }
};

.

:

std::vector<std::unique_ptr<Actor>> actors;
actors.emplace_back( new SweedishChef{} };
// adding a new type of chef simply means adding _one_ line of
// code here, for the new type

( switch):

// using std::find, begin, end
variable_t variable; // initialized elsewhere
const auto chef = find(begin(actors), end(actors),
    [&v](const std::unique_ptr<Actor>& a) { return a->matches(v); });

if(chef != end(actors))
    chef->do_action();
else
{
    // here goes whatever was in the default part of the switch code
}

, :

  • -

  • - ( )

  • / ( )

  • ( , ).

+4

. . . " SweedishChef {}". .

-1

All Articles