Notification Service: MessageCourierFactory
17 November 2008When we last left our little NotificationService project, I was having one of those little should-I-or-should-I-not moments related to setting up a notification DeliveryService. Since setting up the MessageCourierFactory really didn’t prevent me from doing that if I chose to do so, I decided to go ahead and attempt to to create the factory. Here’s my first draft of the interface:
package org.restafarian.notify.service;
/**
* <p>This is the MessageCourierFactory interface.</p>
*/
public interface MessageCourierFactory {
/**
* <p>Creates and returns the requested MessageCourier.</p>
*
* @param deliveryMethod the delivery method for which a courier is being
* requested
* @return the requested MessageCourier
*/
public MessageCourier getMessageCourier(String deliveryMethod);
}
… and here’s my first draft of the implementation:
package org.restafarian.notify.service.impl;
import java.util.Map;
import org.restafarian.notify.service.MessageCourier;
import org.restafarian.notify.service.MessageCourierFactory;
public class MessageCourierFactoryImpl implements MessageCourierFactory {
private Map messageCourierMap;
/**
* <p>Creates and returns the requested MessageCourier.</p>
*
* @param deliveryMethod the delivery method for which a courier is being
* requested
* @return the requested MessageCourier
*/
public MessageCourier getMessageCourier(String deliveryMethod) {
if (messageCourierMap != null) {
if (messageCourierMap.containsKey(deliveryMethod)) {
return (MessageCourier) messageCourierMap.get(deliveryMethod);
} else {
throw new IllegalArgumentException("No message courier configured for delivery method
\"" + deliveryMethod + "\".");
}
} else {
throw new IllegalArgumentException("No message courier configured for delivery method
\"" + deliveryMethod + "\".");
}
}
/**
* @return the messageCourierMap
*/
public Map getMessageCourierMap() {
return messageCourierMap;
}
/**
* @param messageCourierMap the messageCourierMap to set
*/
public void setMessageCourierMap(Map messageCourierMap) {
this.messageCourierMap = messageCourierMap;
}
}
I say first draft because I’m still not sold on the idea completely, and I’m still not sure that this is the way that I want to pursue it. In this version, I am assuming that the mapped couriers will be configured via Spring, using something along these lines:
<property name="messageCourierMap">
<map>
<entry>
<key>
<value>email</value>
</key>
<ref bean="emailMessageCourier" />
</entry>
<entry>
<key>
<value>textpage</value>
</key>
<ref bean="textpageMessageCourier" />
</entry>
</map>
</property>
… but then, I am thinking that maybe I just want to map the names instead of the actual beans, and then have Spring wire up a fresh bean based on the name every time someone places an order with the factory. A this point, I’m not really sure what the right thing to do actually is, so we’ll just call this the “first draft” for now and then see how things play out!
Leave a reply
You must be logged in to post a comment.






