When there is more than one "application" available in one basic form
I have two substantially different applications for configuring two hardware sold by this company.
I was asked to combine two applications so that they can be obtained from one program in such a way that they are available. An analogue of the idea would be to open a program, select a file to open, then open a document editor or spreadsheet editor, depending on the file type.
It's not particularly difficult for most aspects of the application, but my problem is mainly with the main toolbar for the consolidated application. Each of the two different applications has its own set of functions related to a common set of buttons, and also has its own set of buttons.
I could separate the button responses in code using typechecking i.e.
If documentOpened.isSpreadsheet
spreadSheetFunction()
Else
documentFunction()
but it seems like a mess to me including the functions of two classes into one bloated interface class.
Is there some way to achieve a greater degree of encapsulation here, given that both interfaces are so different?
a source to share
What comes to mind is the visitor's design pattern (although it probably doesn't apply in the strict sense of the word). If you defined an interface that implemented all the buttons on the toolbar, then implemented a class for each document type, that would work fine as Daniel suggests. The outline code will be
public interface IToolbarHandler {
void ButtonOne;
void ButtonTwo;
//etc
}
public class SpreadsheetToolbarHandler : IToolbarHandler {
public void ButtonOne {
//Do Some action
}
public void ButtonTwo {
//Do Some action
}
}
public class DocumentToolbarHandler : IToolbarHandler {
public void ButtonOne {
//Do Some action
}
public void ButtonTwo {
//Do Some action
}
}
Then simply inject a form-level variable that is of type IToolbarHandler and set an instance of the appropriate handler for the document being viewed. Then your toolbar event handlers simply call the appropriate method on that variable.
a source to share
I don't know .Net, so this is general OO programming advice. However, it looks like you are asking a general OO design question.
It looks like you are trying to do double dispatch ; that is, choose the actual code to be executed based on the runtime type of two different things.
The traditional clean-OO method for double dispatch is template. Don't be fooled by the name or examples describing the pattern to pave some kind of containing strucutre; it's also very useful even if you only have one object at a time!
Basically, what you would do is define some kind of interface - CommonButtonHandler, and then implement for both tables and documents. When the generic "twiddle" button is clicked, you do:
// Inside CommonTwiddleButton.onClick()
currentCommonButtonHandler.twiddleButtonClicked()
There are some nice examples of online double-submit visitor template; just make sure you look for both, or you will either end up with a bunch of example visitor templates that suggest you have to go through some sort of heterogeneous collection, or a bunch of lisp weenies bragging about how their object system just does multiple submissions by default.
a source to share