I am working on an engine that is intended to be configured by the user (and not the end user, the library user) to use different components. For example, suppose a class Drawermust have ITool, Pencilor Brushand IPattern, Plainor Mosaic. In addition, let a Brushmust have IMaterialeither Copper, orWood
Let's say the effect of the choice Pencilor is Brushreally very different, and the same thing applies to the type IPattern. It would be bad code to encode a class Drawerwith such universal concepts:
public class Drawer<Tool, Pattern> where Tool: ITool where Pattern : IPattern { ... }
public class Brush<Material> where Material : IMaterial { ... }
Then you can use this class, for example:
Drawer<Pencil, Mosaic> FirstDrawer = new Drawer<Pencil, Mosaic>();
Drawer<Brush<Copper>, Mosaic> SecondDrawer = new Drawer<Brush<Copper>, Mosaic>();
Mostly I use generic for collections and such, and actually don't see generics used for this kind of thing. Should I?
source
share