I keep running into this problem when programming, and I wonder if someone has written about it before, or maybe developed some ideas about it.
For an example program, lets say I have a graphics program that stores, in a data file, a Canvas, Layers, and Shapes. Each Canvas has a number of Layers, and each Layer has a number of Shapes. The shapes are things like polygons, curves, etc. – geometric things drawn on screen. Layers represent a group of shapes that can be stacked on top of the other layers, hidden, or made transparent.
When I query the database to get a list of Layers in a Canvas, I might want to create a Layer type that is a pretty simple record. Something like:
struct Layer {
id: UUID
name: String
canvasID: UUID
zOrder: Int
opacity: Float
}
Later, when the user is drawing on the screen, I might want another Layer class that is more complicated. It will have a list of Shapes. It might contain cached pointers to things like the last clicked mouse position, half-drawn shapes, data structures related to GPU rendering, etc.
I find this makes the overall design confusing, because you end up with either:
- Multiple types named
Layer. - One type named
Layerthat becomes a kind of "god class", handling too many things.
How to best deal with this problem?