4

This question is mostly about the best way to approach this problem. Any input is appreciated.

The Problem

In my application i need to send some data to a number of different destinations (we will call them receivers from now on) using different protocols (http post, ftp, soap services). Each of the different receivers respond differently and some processing needs to be done on each response to work out what happened. This response processing can become quite complex.

My application also needs to do take different actions based on the receiver.

My Solution

My plan is to create a separate class for each receiver that implements an interface and use Factory design pattern to decide which class to load based on the receiver. Each receiver has a unique name (which is stored in the database for other reasons) which i plan to use to decide which receiver to load. I'll pass this to the factory constructor.

Currently i plan to have my interface implement these methods

  • prepareData()
  • sendData()
  • handleResult()

I'd also like a wrapper for these function such as process() which would just return a boolean based on wether the send sent. I'm not sure where i would fit this in though.

Thanks

1
  • I tried to see other patterns /ideas, but, the Factory Method pattern you mention, sounds like the best.
    – umlcat
    Mar 9, 2012 at 16:08

2 Answers 2

5

If you're thinking that your interface needs to do three different things (prepare, send, and handle), think some more about cohesion.

That is, you need something that knows how to prepare data differently for different receivers. This is a task in and of itself--so separate it from the other tasks into a series of items implementing an DataFactory interface (a la Factory Method pattern).

Again, the method for sending the data varies, encapsulate that into DataTransport classes (Strategy pattern?) Along the same lines, ResultHandler classes to handle each type of the result.

This way each class does exactly one thing, and can be tested and adjusted in its own right, without affecting anything else.

Think about it any way.

1

You can also check out the Template Method Pattern, this will help you encapsulate the process/algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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