patternjavaMinor
Batch Processing : Design check with SRP and OCP prinicple
Viewed 0 times
processingsrpocpwithdesignbatchprinicpleandcheck
Problem
I have to create a batch process which reads data from DB , process it and saves it in db . The batch data is fetched organization wise ( each organization has set of data to be processed) at once.We need to process only specific set of data i.e organization related and save it into db. Hence i have designed below classes . Does this design breaks SRP and OCP?
Should the process method in DepositProcess have the code to iterate the batch data ?
Batch Processor Interface
Deposit Processort class
Service interface and implementation class
DAO interface and implementation
```
package com.ibank.batch.dao;
import java.util.List;
public interface BatchProcessorDao {
public Object f
Should the process method in DepositProcess have the code to iterate the batch data ?
Batch Processor Interface
package com.ibank.batch;
public interface BatchProcessor {
public void process();
}Deposit Processort class
package com.ibank.batch;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.ibank.batch.svc.DepositBatchProcessorSvc;
public class DepositProcessor implements BatchProcessor {
@Override
public void process() {
DepositBatchProcessorSvc svc = new DepositBatchProcessorSvc();
HashMap> batchData = svc.fetchBatchData();
Set key = batchData.keySet();
Iterator keyItr = key.iterator();
while (keyItr.hasNext()) {
String organizationId = (String) keyItr.next();
List claimsList = batchData.get(organizationId);
svc.saveBatchData(claimsList);
}
}
}Service interface and implementation class
package com.ibank.batch.svc;
import java.util.List;
public interface BatchProcessorSvc {
public Object fetchBatchData();
public void saveBatchData(List data);
}
package com.ibank.batch.svc;
import java.util.HashMap;
import java.util.List;
public class DepositBatchProcessorSvc implements BatchProcessorSvc {
@Override
public HashMap> fetchBatchData() {
return null;
}
@Override
public void saveBatchData(List claimList) {
}
}DAO interface and implementation
```
package com.ibank.batch.dao;
import java.util.List;
public interface BatchProcessorDao {
public Object f
Solution
Quick question, have you tried testing your code? Trying doing so, and you will find the leaks in your code.
-
-
-
Always try to return interfaces and not implementations, ex:
could be refactored to
-
-
DepositProcessor has a high dependency on DepositBatchProcessorSvc, why not to inject it and make the code flexible (easier to test and change).public class DepositProcessor{
private final DepositBatchProcessorSvc service;
DepositProcessor(DepositBatchProcessorSvc service){
this.service = service;
}
}-
BatchProcessorSvc could be totally generified,/*K type of the object's promary key, and T is the object type*/
public interface BatchProcessorDao {
@Override
Map> fetchBatchData();
}-
Always try to return interfaces and not implementations, ex:
HashMap> fetchBatchData()could be refactored to
Map> fetchBatchData()-
Save should never be fire and forget, I know a lot of db clients do it, but it is really bad, I need to know if things were saved successfully, wishful programming is bad, you could return the objects after saving themList saveBatchData(List data);Code Snippets
public class DepositProcessor{
private final DepositBatchProcessorSvc service;
DepositProcessor(DepositBatchProcessorSvc service){
this.service = service;
}
}/*K type of the object's promary key, and T is the object type*/
public interface BatchProcessorDao<K,T> {
@Override
Map<K, List<T>> fetchBatchData();
}HashMap<String, List<String>> fetchBatchData()Map<String, List<String>> fetchBatchData()List<T> saveBatchData(List<T> data);Context
StackExchange Code Review Q#112131, answer score: 3
Revisions (0)
No revisions yet.