Guest User

Untitled

a guest
May 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. class FooBase
  2. {
  3. public FooBase()
  4. {
  5. CreateBar();
  6. }
  7.  
  8. // "Bar" is usually a pipeline of helper classes that process something Foo is interested in
  9. // The pipeline varies in each derived type. Maybe "Initialize" is better.
  10. protected abstract void CreateBar();
  11. }
  12.  
  13. class CommonFoo
  14. {
  15. // CommonFoo has some dependencies
  16. public CommonFoo(Bar bar, Baz baz) : base()
  17. {
  18. // validate/set fields, handle some events, etc.
  19. }
  20.  
  21. // Implementation varies wildly in derived classes
  22. protected override void CreateBar()
  23. {
  24. _firstStep = new FirstStep(_bar)
  25. _specialStep = new SpecialStep(_baz);
  26. _firstStep.Connect(_specialStep);
  27. _secondStep = new SecondStep(_bar, _baz);
  28. _specialStep.Connect(_secondStep);
  29. }
  30. }
  31.  
  32. /* Problem:
  33. I want some way for CreateBar() to come *after* the derived class constructor has run, since the
  34. derived implementations will rely on instance data of the derived types. Options I've
  35. considered:
  36.  
  37. * An Initialize() method on FooBase. This isn't super-bad, but this is a refactor of a class
  38. that is handling 3 different scenarios with different setup steps in the constructor. I'd
  39. have to hunt everywhere that uses it down and add a call to Initialize().
  40. * Parameters to CreateBar(), specifically an object parameter or maybe object[]. I'd have to
  41. change the base constructor and it seems kind of wonky.
  42. */
Add Comment
Please, Sign In to add comment