I\'m trying to find an elegant way to implement a decision making algorithm that
ID: 658915 • Letter: I
Question
I'm trying to find an elegant way to implement a decision making algorithm that allow easy maintenance because the conditions for the decision making might change often.
I'm going to try and be more specific with an example here :
Let's say I'm trying to manage a team of Cooking chefs in a restaurant kitchen.
Every 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 chief regarding what's coming for the clients.
The conditions are :
A chief can only cook one pie at a time. If I order a chef to cook an apple pie for example, I cannot order him to cook a raspberry pie or a pumpkin pie unless the apple pie is done or I send an cancel request for the apple pie.
I can ask a chef to cook up to 5 pizzas at a time, given it's for different clients.
I woud like to create an algorithm that returns the set of orders I'm allowed to send to a specific chef, regarding what he already is cooking.
I'm working with c++. I could write a simple switch/case statement, but the maintenance would not be easy if conditions change or new pies are added, and so...
I'm kinda stuck and really don't see how I could encapsulate the conditions and decision making to decrease couplign beetween conditions and to allow easy maintenance on the conditions of pie cooking.
How would you handle complexe decision making algorithm implementation?
Explanation / Answer
The data consists of Chefs, Orders and a single Schedule. Note that Chefs rarely change, but Orders are constantly being created, removed and optionally edited, and the Schedule has to change in response.
The logic for the new Schedule really depends on all of the data including Chefs, Orders and the old Schedule. For that reason you really need to keep the scheduling logic together in a single place such as a Scheduler class.
Using test-driven development is really well suited to this problem. Start by writing unit tests for the simple cases and then build up to more complex ones where the Chefs already have stuff in their queue and then new orders come in or get removed or edited. Then in your Scheduler class, just write the most straightforward logic you can think of to express the rules. You are completely free to use whatever internal architecture or structure you want. Don't get too complicated or tricky. Whenever you see an opportunity to make the logic simpler and easier to understand, do it, and just make sure all your tests still pass.
Now when you want to make a change, you add a new test case, and modify existing no-longer-correct test cases, and then you modify the logic of Scheduler until all the tests pass again. In this way you know you won't break existing assumptions or logic.
I have used this successfully on several similar problems.