Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- */
Add Comment
Please, Sign In to add comment