I’m working my way through ‘head first design patterns’ and want to use this in practice immediately.
I’m writing a piece of code that connects an application with other applications. In fact, I need to generate an e-mail containing an XML file and send it via e-mail. But other stuff might be required in the future.
Thus, I identified ‘the things that change’:
– The data for the transmission
– The means of transmitting (could be e-mail, but could be FTP or webservice for another data-exchange)
So, I:
– Created an abstract class DataObject
– Created an interface ITransmissionMethod
– Created an dataExchange abstract class:
abstract class DataExchange<T,U>
{
private T DataObject;
private U SendMethod;
}
And SendViaMail is like
class SendViaMail : ISendMethod<System.Net.Mail.Attachment>
{
public override void Send(System.Net.Mail.Attachment dataItem)
{
throw new NotImplementedException();
}
}
Now – I can create classes like:
class MyExchange : DataExchange<MyDataObject,SendViaMail> { }
What do you think about this approach? Now what I would really like to do is Create an abstract method in DataExchange that should look something like
private abstract [the type of the T in ISendMethod<T>] PrepareObjectForSending(T dataObject) {
}
Visual Studio would force me to implement a method like:
private abstract System.Net.Mail.Attachment PrepareObjectForSendingMyDataObject dataObject) {
// Serialize XML file and make it into attachment object
}
Wouldn’t that be sweet? But what do you guys think about this approach? In the future, people can create new dataObjects and new sendmethods and the code would still work. What I’ve been trying to do is: program against interface and extract changing parts. How about it?
That would work, but you could separate concerns even more. Here is just another version – make DataExchange to be very simple and delegate real work to workers:
Converts would just convert data objects to objects suitable for transmission:
Senders would only send.
Putting all together: