Allowing a class function to easily be replaced
<p>I often have a class where I want to allow a functionality to be selected. For example I have a class that has a <code>GetNextNode()</code> function which is used like <code>MyClass::DoIteration(){GetNextNode(); }</code> . I want to allow the user to select from one of many possible implementations of GetNextNode to determine how the next node to process should be determined. I also want to allow a user of my code to easily provide their own implementation.</p>

<p>So far, the answer is to make <code>GetNextNode()</code> virtual and re-implement it subclasses of MyClass...</p>

<p>My problem arises when I have two such interchangeable functions. If I have <code>Function1()</code> and <code>Function2()</code> which both have N possible implementations, then I would have to provide 2N subclasses to allow the user to pick which pair of these functions to use. Generally, it is much worse (if there are more than 2 such functions).</p>

<p>Note that these functions need access to data inside MyClass.</p>

<p>Is there a "pattern" that I am missing that allows "plugins" like this to be selected?</p>
