I have a Winform singleton class that contains a number of buttons. The button click events are handled and monitored in seperate classes that have a reference/instance to the singleton winform class. So o开发者_如何学Cbviously when the user clicks the buttons then the tasks associated are handled in the relevant classes.
My question is, the classes that monitor the events are fully encapsulated, meaning that no methods are required to be called for them to work. The only thing they need is the event to be triggered and the tasks are carried out. Where should I therefore create the objects for these classes? Should I simply create the objects within the winform when it is loaded? No further interaction is required with the objects, they simply need an instance to monitor the button click events. Hope i've explained this clearly enough.
Thanks.
If you are making a real observer model, then the association belongs in the creator of the control.
If you are just pushing logic into separate classes, then it is more of a delegate pattern and it should be the control that owns the references.
Yes, I think that the form's Load
event is a logical place to create these objects. Or perhaps the constructor.
You cannot create them before the form has been constructed, because there won't be any buttons then. And you shouldn't create it much later either, because then you might miss some events. So the form's own events is the best place to create them. Or at least bind them to the events. Are these classes singletons as well?
精彩评论