2

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

3 Answers 3

3

I think the direction should be a Creational Pattern, page 33 in GoF book presents a help table for building a variable design. From the page:

Design Pattern         Aspect(s) That Can Vary
--------------         -----------------------
Abstract Factory (68)  families of product objects
Builder (75)           how a composite object gets created
Factory Method (83)    subclass of object that is instantiated
Prototype (91)         class of object that is instantiated

From this table, Prototype seems to fit the best it basically lets you to

specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

EDIT: nice detailed info here: http://sourcemaking.com/design_patterns/prototype

Please elaborate on your problem if this design is not enough...

1

Your question looks like a textbook example of the benefits of polymorphism, I don't think there is a named pattern to implement this functionality as this is a fairly basic OO principle. Your idea to create an interface for both classes to implement is the best idea allowing you to call myInterface.doStuff to handle both types of communication and allow for more to be added if necessary.

5
  • I don't think so. Perhaps the example is not clear in showing that MailTo and SmsTo have nothing in common but being a "output". The essence of polymorphism is to use an interface or a superclass and let the implementors decide the exact behavior of an inherited method. In those cases, programming to an "interface" is perfect. BUT the most the common aspects between the two classes are few, the more likely you will need to deal with specific aspects of your class. At the end of the line, we have an empty interface that contains no methods. How can you use polymorphism?
    – AgostinoX
    Jan 23, 2012 at 20:18
  • As I understand your question, you have an App that is intended to send a communication(s) to someone. MailTo and SmsTo are related in that they are your methods of communication; this is all the relation they need. I am more familiar with .NET where creating an interface called 'communication' with a method called 'send' for both classes to implement would be the best solution, so both classes can be held in the same collection of type communication, this might not be possible in java, but a class with a virtual method would achieve the same but less extensible result.
    – Ryathal
    Jan 23, 2012 at 20:31
  • the factory pattern would be a good way of creating the classes as need though as kevin junghans suggested.
    – Ryathal
    Jan 23, 2012 at 20:31
  • Factory pattern is very useful when the set of possible implementors of an interface is "open-ended", and reveals to be quite burdensome when you have two or three or even ten implementors classes, but being the set unlikely to be extended. I think that this would be "push in" the factory pattern, so you feel the code is "right" because it respects a DP. But factory pattern pays for itself only when the set of classes is growing. A situation that occurs often in frameworks, not so often in common applications.
    – AgostinoX
    Jan 23, 2012 at 20:48
  • The factory pattern is not just used when "possible implementors of an interface is open-ended." It also used when a specific object interface can implement more than one class of objects and knowing which class is not necessary. It is also used for dependency injection. And saying you can predict when the "set of classes will grow" is like saying your application will never evolve. Jan 24, 2012 at 18:38
1

I agree with Ryathal that this is basic polymorphism and therefore you will not find a design pattern for it. Although you could use something like a factory pattern to create the appropriate class based on the configuration settings. I disagree that the email and sms service do not have anything in common. The base class could be something like "communications service" and these services would always have a location for the service (ex: smtp server location or url for web service or API) and some credentials for authentication. The "email" class and "sms" class would inherit from the "communications service" class. The method for sending a message would differ in the derived classes.

6
  • Maybe I haven't been clear on the fact that i am not looking for an explanation on polymorphism, but i'm trying to create an example (perhaps insufficient)that shows a "difficult" application of polymorphism. I've choosen two "systems", mail and sms that are rather different. You can say that there is something in common, but as long as there is something that is different, polymorphism will be lame. If I send an email the text can be long, while an sms should be short. So while they are both "output" methods, but they have neither the sendText(String text) in common. The caller should decide.
    – AgostinoX
    Jan 23, 2012 at 20:33
  • 1
    That depends on your level of granularity. If the interface method was sendOutput(String)..... Jan 23, 2012 at 20:44
  • 1
    I have implemented a notification system using polymorphism in just this manner. It had even more communications channels such as voice and IM. Also the SMS and voice might use different services that had different API's. This was all encapsulated beautifully in an abstract class that had one method called sendMessage. I used the factory DP to create the instances of these objects. The class that handled going through the list of contacts did not care what the destination service was, it just needed to send a message and get a response. I think you picked a bad example of difficult poly. Jan 23, 2012 at 20:53
  • @Martijn Verburg:If the name was different? :-) The problem is that in polymorphism the "low-level-class" decides. Instead, here i am showing that the client of the class should have some level of awareness of the type of implementation because it (and not the implementor) should take certain decisions.
    – AgostinoX
    Jan 23, 2012 at 20:57
  • @Kevin Junghans. Maybe, picking examples is always difficult :-). What if the message itself is different among different channels? What would be the abstract class in that case?
    – AgostinoX
    Jan 23, 2012 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.