I have five data types currently read from a database, and I need to add XML and CSV sources plus internal and external reading modes. That could mean 30 reader combinations. How would you organize this without creating a class for every combination?
How can I avoid a separate Spring Batch reader for every format and mode?
Answers
Start by separating what actually changes. The database, XML, and CSV readers can handle getting data from their respective sources. Mapping can turn that data into your domain types, and a separate component can handle the mode if it affects what happens after reading.
Compose those pieces when configuring the job instead of making a ClientDatabaseInternalReader and every other permutation. Interfaces or strategies can help where the implementations are genuinely interchangeable. You may still need some source-specific mapping for each type, so this won't necessarily eliminate all combination-specific code.
Reply to this answer
Before picking a pattern, pin down what internal and external mean. Do they change the input location, credentials, record shape, or what you do with the records? Those lead to pretty different designs.
Same question for the five types. If they share a file structure and only need different mapping, a standard mapper plus configuration may be enough. If their formats differ substantially, separate mappings or readers might be clearer than a generic reader full of conditionals.
Reply to this answer
Don't feel obliged to build a hierarchy for every axis. If source and mode are just configuration choices, pass them into a reader or its setup and delegate the differing work to helpers. A couple of explicit branches can be easier to maintain than a factory creating dozens of tiny classes.
Reply to this answer
Strategy is a reasonable fit for swappable parsing or mapping behavior. A factory can choose and assemble those strategies, but it won't reduce the complexity on its own. I'd first sketch the interfaces around the behaviors that vary, then add a factory only if constructing the combinations becomes awkward.
Reply to this answer
Add your answer
Answers are reviewed before publication.
3e~lockbox_517:
The mode might affect the read itself rather than processing afterward. If so, keep that behavior near the reader instead of forcing it into a later step.