Home > Software design >  Use of instance variables in java
Use of instance variables in java

Time:02-17

I have a method called getCustomerCount() which makes a database call to get the customer count based on the store like below.

public int getCustomerCount(String storeName) {
  return repository.getCustomerCount(storeName);
}

I need to call the above method more than once from other methods so can I use an instance variable to set the customer count the very first time this method gets called and then reuse the variable instead of calling this method and making a database call every single time? Can I do something like this:

private int customerCount;
public int getCustomerCount(String storeName) {
  int count = repository.getCustomerCount(storeName);
  customerCount = count;
  return count;
}

public void processCustomerData(String storeName) {
  getCustomerCount(storeName);
// Based on customer count, add business logic 
  sendEmail(customerList);
}

public void sendEmail(List<Customers> customers) {
// get customer count and set email count to the number of customers in the system. 
  int emailCount = 0;
  emailCount  = customerCount;
}

Would this be an issue when multiple requests execute at the same time through a rest api? Are there any pros and cons of using instance variables like this? If there are better ways than using instance variables, please suggest. I need to execute the query for each request so singleton may not work for this case. Thank you.

CodePudding user response:

You could try to use the singleton design pattern for your specific problem

See this link: https://www.gofpatterns.com/creational/patterns/singleton-pattern.php

CodePudding user response:

I assume code above you shared is customerService. SpringBoot is singleton, so it is not good practice to have global variables(if they are not constant) in SpringBoot objects.

@Service
private CustomerService customerService;

int customerCount = customerService.getCustomerCount();
customerService.processCustomer(customerCount); // you should pass customerCount

Also you should change sendEmail method as it should take customerCount as argument if customers size can be different from customerCount

public void sendEmail(List<Customers> customers, int customerCount)

CodePudding user response:

If you don't want the method to hit the database each time, you should use the Integer wrapping class and store the count in a map:

private Map<String, Integer> customerCountMap = new HashMap();
public int getCustomerCount(String storeName) {
  if(customerCountMap.get(storeName) == null) {
    // you will probably need to pass the store name to the repository...
    customerCountMap.put(storeName, repository.getCustomerCount(storeName));
  }
  return customerCountMap.get(storeName);
}

You still need to update the count on Insert and delete. So you might end up with Spring caching anyway (or you could use the old fashioned observer pattern).

The sendEmail() method still doesn't make sense.

  • Related