Skip to content

Instantly share code, notes, and snippets.

@SittenSpynne
Created April 30, 2010 21:33
Show Gist options
  • Save SittenSpynne/385783 to your computer and use it in GitHub Desktop.
Save SittenSpynne/385783 to your computer and use it in GitHub Desktop.
class FooBase
{
public FooBase()
{
CreateBar();
}
// "Bar" is usually a pipeline of helper classes that process something Foo is interested in
// The pipeline varies in each derived type. Maybe "Initialize" is better.
protected abstract void CreateBar();
}
class CommonFoo
{
// CommonFoo has some dependencies
public CommonFoo(Bar bar, Baz baz) : base()
{
// validate/set fields, handle some events, etc.
}
// Implementation varies wildly in derived classes
protected override void CreateBar()
{
_firstStep = new FirstStep(_bar)
_specialStep = new SpecialStep(_baz);
_firstStep.Connect(_specialStep);
_secondStep = new SecondStep(_bar, _baz);
_specialStep.Connect(_secondStep);
}
}
/* Problem:
I want some way for CreateBar() to come *after* the derived class constructor has run, since the
derived implementations will rely on instance data of the derived types. Options I've
considered:
* An Initialize() method on FooBase. This isn't super-bad, but this is a refactor of a class
that is handling 3 different scenarios with different setup steps in the constructor. I'd
have to hunt everywhere that uses it down and add a call to Initialize().
* Parameters to CreateBar(), specifically an object parameter or maybe object[]. I'd have to
change the base constructor and it seems kind of wonky.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment