Suppose I have an XML config file like the following:
<myapp>
<settings/>
<output>
<mailto>mail service configuration parameters</mailto>
OR
<smsto>sms service configuration parameters</smsto>
<output>
While the settings node will have always the same structure, the output node may have different structures, depending on whether it is a mail or an SMS output.
The Java classes will be:
class MyAppConfig {
String settingXXX;
{what type?} output;
}
Obviously I will have a class MailTo {} and a class SmsTo {}.
Since output may either be a MailTo or a SmsTo object, it can be:
- an Object type, since
MailToandSmsTodon't have any superclass in common. - an
Outputinterface, that in turn is implemented by MailTo and SmsTo. SinceMailToandSmsTodon't have anything in common,Outputinterface will be empty and will "collapse" to a marker interface.
I think that, given this situation, this is the only implementation possible.
The client code will typecast Object or Output to MailTo or SmsTo, that must be handled differently.
Nevertheless, I'm puzzled because design patterns (GoF book) seem not consider this kind of situations.
What is the "real world" pattern that you would apply in this case?