title: OO design pattern: How to add methods dynamically?
body: <p>I have implemented a simple tree structure in C# (or Java, doesn't matter much) where everything revolves around the abstract class Node and some subclasses of it. Node offers methods and properties related to the mathematical graph theory. For example Node.Parent references the parent Node and Node.Children the child nodes  </p>
  
  <pre><code>class Node {  
      Node Parent;  
      Node[] Children;
  
      void appendNode(Node node) { ... }
  }  
  </code></pre>
  
  <p>I am using the tree to perform calculations. The calculations involve a lot of recursions and I also need to store intermediate values for each node. For each calculation I have introduced additional properties and methods to the Node class, such as </p>
  
  <pre><code>class Node {  
      Node Parent;  
      Node[] Children; 
  
      // Calculate weight
      int weight; // current weight
      void recalculateWeight() {
          // perform some heavily recursive stuff
          // involving Parent.recalculateWeight()
          // and update the value of the variable weight
      }
  
      int price; // current price 
      void recalculatePrice() {
          // perform some heavily recursive stuff
          // involving Parent.recalculatePrice()
          // and update the value of the variable price
      }
  
      void appendNode(Node node) {
          // ...
          recalculateWeight();
          recalculatePrice();
      }
  } 
  </code></pre>
  
  <p>but now I have to give up this approach since the calculated values should be added dynamically without changing the Node class. Dynamically means that somebody else should be able to implement his own calculations on a given tree relying only the "graph theoretical methods" of the Node class.</p>
  
  <p>Do you have any idea what would be a good design pattern for that?</p>