R – Best practice for writing a code generator

code generation

I'm about to writing an app which will be making use of the state and command patterns. The States will act as a facade to the commands.

There are 7 states and about 50 Commands which not all the states can execute, any method for which the command cannot be executed will throw an execption, otherwise It will create the command, execute it and return the result.

Since there are such a lot of classes that are going to be so similar I'm thinking of writing some code to auto generate the code. I've got a matrix mapped out in Excel of which states allow which commands to be executed, and I was planning on outputting that to csv and useing that as the basis for the code generation.

My idea is to manually write the interfaces that the various classes implement and then use that as a template for the code generator.

Is this a good idea? Does anyone have any tips on the best way to go about doing this?

I'll be codeing in Java however I think the basic principles apply to code generation in any OO language.

Best Answer

Well, if the classes are really that similar, why not derive them from a common Command object instead of using code generation?

What you're talking about seems to be a basic rules engine -- the Rules Engine you're making is basically what runs each command.