Tuesday, March 15, 2011

Best practice: initialize same component in multiple classes.

This is my first question so please be patient :)

Background: I'm implementing an observer pattern and I have about 20 classes where I would eventually implement it. In order to use the subject and observer I need to: 1: initialize observer classes 2: create delegates 3: add delegates to the events

This is probably very simple, but I don't want to initialize all those 3 steps in each class, so I'm leaning toward using the base class to initialize those components. But would that be a good practice? Because the base class would not have anything to do with my other classes, it would just do initialization. Or would it better to just create another class and just create an instance and use those components through that class, but that again I would need to make same instance in 20 classes.

Thanks your feedback.

From stackoverflow
  • This is a place to favor composition over inheritance. Create the component add that component to each class that needs it.

0 comments:

Post a Comment