I have two essentially separate applications for configuring two pieces of hardware sold by this company.
I’ve been asked to put the two applications together so that they can be accessed from within the same program in such a way that both are accessible. An analogy to the idea would be opening a program, selecting a file to open, then opening a document editor or a spreadsheet editor depending on the type of the file.
This isn’t particularly difficult for most aspects of the application, but my problem comes mostly from the main toolbar for the consolidated application. Each of the two different applications has its own set of functions relating to a common set of buttons, as well as having its own set of buttons.
I could separate the button responses in code with typechecking, i.e.
If documentOpened.isSpreadsheet
spreadSheetFunction()
Else
documentFunction()
but this seems messy to me, incorporating the functions of two classes into one bloated interface class.
Is there some way to achieve a greater degree of encapsulation, here, given that the two interfaces are so different?
The thing that springs to mind is the Visitor design pattern (though it’s probably not applicable in the strictest sense). If you defined an interface that embodied all the buttons on your toolbar and then implemented a class for each document type that would work fine as Daniel suggests. Outline code would be
Then simply have a form level variable which is of type IToolbarHandler and set to an instance of the relevant handler for the document currently being viewed. Then your toolbar event handlers simply invoke the relevant method of that variable.