Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for different kind of input messages

I am writing a service which can receive input messages in different formats like json, xml, csv etc. Though messages are in different format but data is similar in all of them. Which design pattern should i use to achieve following:

  • Should be able to add new message format without changing existing code.
  • Convert different message formats into a uniform format to be consumed by core service for processing.

Thanks, Suraj

like image 754
suraj bahl Avatar asked Feb 02 '26 12:02

suraj bahl


1 Answers

At first sight, I would say the Strategy pattern.

Make an abstract class (e.g. InputMessage), and derive strategies for json, xml etc. For a new type of input messages, only a new strategy can be derived from InputMessage.

You can use operations for converting to/from the generic format (these will differ for each strategy).

like image 179
Michel Keijzers Avatar answered Feb 04 '26 00:02

Michel Keijzers