Reputation: 3660
I have a scenario where there is a service aggregator, lets take for example skyscanner or kayak or Google flights. If you see, they all ask basic details like what is source, destination, date and number of passengers, once you select the flight from a particular service provider, let's say for example expedia, you are are redirected to expedia with prefilled details that was sent across by service aggregator
Now the name of parameters sent from the source system may vary and on the destination we have to convert it into same POJO.
One option I was thinking of was Factory pattern, based on channel, I would return an object which would transform the source params to a common POJO.
Other design pattern that I feel similar is Strategy Design Pattern, to create a ExtractionStrategy based on Context
Is there any other apt design patter to solve this problem?
Upvotes: 0
Views: 1590
Reputation: 91
IMHO keeping the implementation as simple as possible, I would suggest you to do go with a simple factory pattern. Define a factory service just to perform 2 operations:
Define a parent class which has common fields of your POJO(say BasePOJO) and then extend all other types of source aggregator POJOs(assuming all would be having different field names etc). Since each of your source agg. classes would be having their own logic to create POJO specific to that type of source aggregator.
To link each source class impl. maintain an enum of: (sourceType, sourceAggClass)
ex:
enum sourceAgg{ (EXPEDIA, ExpediaModelImpl), (VIA, ViaModelImpl); }
All you have to do is get the implementation based on the source aggregator key(say EXPEDIA) from the enum, since you have the bean name in the context you can easily invoke the transformModel function for each type. The object returned by transformModel from each of the source agg. classes would be of type BasePOJO, it becomes easy to handle as formal parameters.
In future, if you want to add new source aggregator, all you have to do is just write the transform model of that class and add in the enum mappings, that's it!
PS: This is my 2nd answer on SO, Suggestions most welcome.
Upvotes: 2