Tip*_*ipx 5 c# generics design-patterns class
我正在研究一种引擎,该引擎可由用户(不是最终用户,库用户)配置以使用不同的组件.例如,假设该类Drawer必须具有ITool,Pencil或Brush,和IPattern,Plain或Mosaic.此外,比方说,一个Brush必须拥有的IMaterial任一Copper或Wood
让我们说选择的效果Pencil或Brush实际上是非常不同的,IPattern类型也是如此.Drawer使用如下通用概念对类进行编码是不是一个坏主意:
public class Drawer<Tool, Pattern> where Tool: ITool where Pattern : IPattern { ... }
public class Brush<Material> where Material : IMaterial { ... }
Run Code Online (Sandbox Code Playgroud)
然后可以使用该类,如:
Drawer<Pencil, Mosaic> FirstDrawer = new Drawer<Pencil, Mosaic>();
Drawer<Brush<Copper>, Mosaic> SecondDrawer = new Drawer<Brush<Copper>, Mosaic>();
Run Code Online (Sandbox Code Playgroud)
我主要使用泛型来收集等等,而且还没有真正看到泛型用于那种事情.我是不是该?