Let's suppose i have a winform with a ribbon menu. In this ribbon there's some buttons like "New", "Save", "Delete", and so on... The application can open forms within them there are typical controls for managing data, so we could create NEW book/video/laptop, SAVE book/video/laptop... how would you keep synchronized buttons (UI) and above all what pattern would you use for use a single button to manage all NEW/SAVE/DELETE forms (read books/videos/laptops)?
I would use Chain of Responsibility pattern. Forms must implement interface(suppose, IChain) in order to accept ribbon menu object (whatever proxy object that might be - ribbon may examine it lately and create necessary menu items). On forms creation (which is event or Observer pattern) ribbon sends its menu-proxy to IChain. IChain implementation decides how to fill it.
Benefit of chain - it allows to handle method by many receivers. You can have nested forms and in that case it's hard to determine all menu combinations ahead. Let forms decide what to output in menu - that's their headache and responsibility.
You can create an interface containing common methods and properties that you need to use from the shared toolbar.
Then implement those methods in your forms. For example if you have a NewDocument method in interface, each class can have its implementation of the method.
Then in your main form, create a property ActiveForm of type of your common interface to store a reference to your active form. Also if you are using multi form infrastructure which lets you know the active window, you can cast it to your common interface type.
Then in your main form, when you click a button, like new document button you can call ActiveForm.NewDocument which will run the implementation for the active form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With