C# generics in interface, writing generic algorithm

Viewed 152

I need to make a program that uses several algorithms to create random mazes. I've defined the following class:(It's a data type the algorithms will use, and T is the type used in a specific implementation, for example if I used 2D matrix to represent maze, T will be a 2D point)

class Entry<T>
{
    private T elem; //the data saved in each entry
    public T Elem
    {
        get { return elem; }
        set { this.elem = value; }
    }

    public Entry(T elem)
    {
        Elem = elem;
    }

    public override int GetHashCode()
    {
        return Elem.GetHashCode();
    }
}

And the following interfaces:

interface IRandomGeneretableMaze<T>
{
    void SetRandomEntrance();
    void SetRandomExit();
    List<Entry<T>> GetNextPossibleEntries(Entry<T> entry);
    void MakePath(Entry<T> entry1, Entry<T> entry2);
    void RemovePath(Entry<T> entry1, Entry<T> entry2);
}

It defines a behavior a randomly generatble maze must supply.

interface IMaze<T>
{
    Entry<T> GetEntrance();
    Entry<T> GetExit();
    List<Entry<T>> GetNextMoves(Entry<T> entry);
}

It defines some general methods mazes work with. (The mazes are being implement in several different ways)

interface IRandomMazeGenerator<T>
{
    IMaze<T> Generate(Type t);
}

It defines just a maze generator, each algorithm will be implemented differently, while t is just the type of maze to be created. (Might be 1 for Matrix base implementation, 2 for graph based implementation and so on). Let's assume I've made the following classes:

class MatrixMaze : IMaze<Point2D>, IRandomGeneratable<Point2D> {...}
class GraphMaze : IMaze<Node>, IRandomGeneratable<Node> {...}

My Problem is when I try to implement the method generate, compiler ask me for a concrete T, while the algorithms aren't (and shouldn't be) dependent on T, and I don't want to write the same algorithm, only for different T's. Is there a way writing single piece of code, that will work independently of T?

1 Answers
Related