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
MailTo
andSmsTo
don't have any superclass in common. - an
Output
interface, that in turn is implemented by MailTo and SmsTo. SinceMailTo
andSmsTo
don't have anything in common,Output
interface 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?