I currently have a number of classes (~20) that all do the same thing (abstractl
ID: 644008 • Letter: I
Question
I currently have a number of classes (~20) that all do the same thing (abstractly), namely, generate an instance of a particular class from an xml file.
The way that they use the content of the xml file varies quite a bit, and the data gets stored into different substructures (fields, groups, etc) that are shared across the classes, so I took all of the parsing and field information out of the files, and created a helper class (as is the final constructed type)
All of the above classes and methods are static, as they have no need to contain any state.
I then wanted to have all the classes derive from one common class so that I am not referencing a seemingly unrelated class constantly, only to discover that inheritance and static do not work together well.
What is the appropriate way of setting this up? Its bascially a large set of factory methods for a type that require no state.
Update: The control flow is currently as follows:
Pick one of ~20 factory methods
Read in an XML file (all tags, attributes are the same across the files, but the files are distict across the methods).
Convert the content in the XML files to a class that has some data structures as members (List<Field>, List<Group>, Dictionary<string,string> etc). These data structures use types that are nested in the helper.
Add these collections to the uninitialized object.
Return the object to caller.
Right now, the 20 factory methods are thier own static classes, and all return objects of the same type, as even though there might be some semantic difference between the "types" all of the memebers and functions are the same. The code that parses the XML file is in a helper, as this logic is common to all of the factory methods. Step 3 has to be different for all the methods, so that part is found in the static classes.
Explanation / Answer
I failed to understand why one might need 20 classes here. I think it takes one class with 20+ methods.
class XMLMaker {
public OutputClassA ParseA(File input) { ... }
public OutputClassB ParseB(File input) { ... }
// etc, however many classes you need to create
//
// reusable helper methods for the public methods above
protected XML.Node _ParseFoo(...) {...}
protected string _ParseBar(...) {...}
//
// I can imagine you might want it, too:
public OutputClass Parse(File input) {
var file_kind = _DetectKind(input);
if (file_kind == KIND_A) return ParseA(input);
// etc
}
}
I can also imagine that you can spread ParseA, ParseB, etc into a manageable set of separate files by making XMLMaker a partial class.
XMLMaker does not need to store any state, so it can be instantiated once by a caching factory, or by a DI mechanism.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.