How should one manage a superclass with a very large number of subclasses?
I was reading through older questions and this one made me wonder how you would go about managing such a large number of subclasses (100+ in the example question) as there were several comments saying that it was very bad practice to do this with classes.
Simple example, a game like Minecraft - everything is a block, so there is a clear superclass. But each block type has its own attribute values for density, texture, weight, chance of appearing, etc, etc, as w开发者_如何学编程ell as many which are subclass-specific. So if the game had several dozen types of blocks, how do you manage that in a "proper", OOP way?
Is there a better data structure to use rather than pure classes? If it's just tons of numerical attributes you could use something simpler (even just a config file), but chances are there will be code specific to many of the subclasses as well.
NB Since this is a slightly more general question and not something I have tried to implement, I'm not sure if this is better suited to programmers.stackexchange.com. If it is, let me know in a comment and I'll close the question and ask it there instead. I'm also not sure how to tag this, so feel free to edit!
One could maybe use a graph data structure where the nodes are the "blocks" and node properties are the node's properties. If a "block" has some sub-structure then this might be expressed using (sub-)nodes associated with their parent node. Then you use factory methods that create the different kind of block nodes.
This is a very flexible approach, because the graph data structure remains always the same, but you shift complexity into managing and handling the differently configured nodes / graphs...
精彩评论