I have an interface of edge:
public interface IEdge<TPoint, TFactory>
where TPoint : IPoint
where TFactory : IEdgeFactory<TPoint>
{
TPoint Begin { get; }
TPoint End { get; }
void Divide();
}
The edge can be divided those producing nested edges. New edges are created using a factory pattern:
public interface IEdgeFactory<TPoint>
where TPoint : IPoint
{
IEdge<TPoint> Create(TPoint begin, TPoint end)
}
I want to be able to instantiate a factory inside my IEdge
implementations. Normally I would do it using i.e. public static IEdgeFactory<TPoint> Instance { get; }
, but I can't define this in interface.
So is there a way to pass the singleton factory as type parameter and give implementations a way to instantiate it?
You can simply pass factory to classes implementing the interface:
public interface IEdge<TPoint>...
class MyEdge : IEdge<MyPoint>
{
IEdgeFactory<MyPoint> factory;
public MyEdge(IEdgeFactory<MyPoint> factory)
{
this.factory = factory;
}
}