HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Batch Processing : Design check with SRP and OCP prinicple

Submitted by: @import:stackexchange-codereview··
0
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

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.

-
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 them

List 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.