Type list iteration

I find myself doing this kind of thing from time to time, and I wonder if it has a smell design, or if there is a better design that I can use.

There is a multi-step process that is known at compile time, but is likely to change along the way. I fix the commonality in the abstract Step class, write StepLister, which returns a list of steps, one for each derived class of Step, and then StepsRunner, which calls StepLister, then iterates through the list and starts each step. Sometimes one step will depend on the result of the previous step, sometimes not.

Any suggestions?

+6
oop design-patterns
source share
2 answers

Your approach sounds reasonable to me (a combination of iterators / strategies).

Sometimes one step will depend on the result of the previous step, sometimes not.

This point may be interesting. Since I do not know what each step should be done in particular, I cannot give anything but very general ideas.

Dependencies between all steps can be modeled using the interpreter syntax tree as instead of subsequent steps. Therefore, your StepRunner will be discarded in favor of context / interpretation methods.

Another idea might be to use monads , which allows you to sequentially glue the steps together, but I don’t know how easy it is to integrate your existing object-oriented concept (since monads are commonly used in functional programming).

You may not need to (overly) complicate things at all;)

+2
source share

I'm not sure I understood the question correctly, but since you are following the steps in the sequence, I will assume that there is some contextual information, and therefore you are talking about choosing the next step based on the result of the current one.

This, in fact, is what concerns the machine. You have different states connected by transitions.

It is easy to ask each step about returning some tag , perhaps only a string or a specific type.

Then you define the automaton by defining the next step for each of the possible outputs of the current step.

For example, I actually use a framework (at work) that accepts these transitions as an xml file ... although I completely dislike the fact that the check is performed incorrectly or not ..

Please note that in C ++ it can be checked at compile time (I am thinking about using Boost.Variant and some metatheme programming tricks).

+1
source share

All Articles