This question includes some math concepts but they're irrelevant to the meat of the question.
I'm implementing an API for studying automata on graphs. I have an abstract class called AutomataGraph which is inherited by the different types of AutomataGraph's I'm interested in. Each type has different rules for how it progresses through its states. For each type I have a builder/factory class inheriting from AutomataGraphBuilder for doing different interesting things with the graphs/automata. One such thing is generating all of the unlabeled graphs with n nodes. In the AutomaGraphBuilder class this method looks like:
AutomataGraphBuilder.prototype.genAllUnlabeledGraphs = function(n){
var graphs = [];
var graph;
for(all possible graphs){
graph = new AutomataGraph();
if (should add graph) {
graphs.push(graph);
}
}
return graphs;
}
This works fine except when I want to extend this call to the JohnConwayAutomataGraphBuilder I have to copy all of the code just to change the "graph = new AutomataGraph()" line to "graph = new JohnConwayAutomataGraph()".
Any suggestions? I'm more interested in looking for some general structural insights and not the fact that this is javascript.
EDIT: The pseudocode is of course fairly different from the actual algorithm and just there to demonstrate that there's excessive code copying.
AutomataGraphcreated for each concrete factories? Where the arguments (args) comes from? Are they hard-coded, are they always the same?AutomataGraph, the rest shouldn't be of it's concern. Another object could be responsible forgenAllUnlabeledGraphsand rely on an injected factory to createAutomataGraphinstances.AutomataGraphBuilderdoes not seem to have the purpose to buildAutomataGraphs.