Strategy Pattern with Type Reflection affecting Performances ?

Posted by Aurélien Ribon on Stack Overflow See other posts from Stack Overflow or by Aurélien Ribon
Published on 2010-04-30T08:31:32Z Indexed on 2010/04/30 8:37 UTC
Read the original article Hit count: 295

Hello !

I am building graphs. A graph consists of nodes linked each other with links (indeed my dear). In order to assign a given behavior to each node, I implemented the strategy pattern.

class Node {
    public BaseNodeBehavior Behavior {get; set;}
}

As a result, in many parts of the application, I am extensively using type reflection to know which behavior a node is.

if (node.Behavior is NodeDataOutputBehavior)
    workOnOutputNode(node) ....

My graph can get thousands of nodes.

  • Is type reflection greatly affecting performances ?
  • Should I use something else than the strategy pattern ?

I'm using strategy because I need behavior inheritance. For example, basically, a behavior can be Data or Operator, a Data behavior can IO, Const or Intermediate and finally an IO behavior can be Input or Output.

So if I use an enumeration, I wont be able to test for a node behavior to be of data kind, I will need to test it to be [Input, Output, Const or Intermediate]. And if later I want to add another behavior of Data kind, I'm screwed, every data-testing method will need to be changed.

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns