I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything...
This is a common technique for implementing the strategy pattern,
especially in other languages. See Modern C++ by Alexandrescu.
Here is a contrived example that uses graphical visio like objects.
class BaseNode
{
public Rectangle Bounds;
public Point Location;
public Size Size;
public virtual void Paint(Graphics g){}
}
class SolidNode
{
public Color BackColor;
public override void Paint(Graphics g)
{
using(SolidBrus h brush = new SolidBrush(Back Color)
g.FillRectangle (brush, bounds);
}
}
class GradientNode
{
public Color StartColor;
public Color EndColor;
public float Angle;
public override void Paint(Graphics g)
{
using(LinearGra dientBrush brush ... )
g.FillRectangle (brush, bounds);
}
}
class EllipticalNode< TBackgroundType > : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// set eliptical clip region
// paint background
base.Paint(g);
// paint elliptical border
g.DrawElipse(.. .)
}
}
class BoxNode<TBackgr oundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// paint background
base.Paint(g);
// paint rectangular border
g.DrawRectangle (...)
}
}
The purpose of the templates is to alter the behavior of the leaf
classes via base class selection. Here, the base class dictates how the
background of the node is painted. Without generics, the two leaf
classes would balloon to four classes (GradientBox, SolidBox,
GradientElipse, SolidElipse), or the classes would need to use
aggregation (ie you would pass them an object that is responsible for
painting the background).
H^2