jeudi 13 août 2020

How to avoid type-checking parameters in this scenario

I wanted to implement a basic Graph structure that could later be extended to more specific types of Graph (Dialogue, Behavior...) which would each also need specific types of nodes which would derive from a Node base class.
My Graph base class looks like this right now

public abstract class Graph {

  private Dictionary<Node, List<Node>> adjList;
  
  public abstract void AddNode(Node node);
  public abstract void RemoveNode(Node node);
  public abstract void AddEdge(Node node, Node destNode);
  public abstract void RemoveEdge(Node node, Node destNode);

}

My problem is that I would have to typecheck in the implementation of those methods to prevent users from adding, for example, a DialogueNode to a QuestGraph. Are there any design patterns to fix this? Or should I just do without a base class and write some code multiple times?

Aucun commentaire:

Enregistrer un commentaire