Thursday, 15 August 2013

java - Application properties as static variables or instance variables with singleton -


I need to read application properties in a class from a .properties file that forms a point for application properties Should work in What is the recommended method for such a class: define those properties as a fixed variable or in the example variable with singleton patterns?

I have a myapp.properties file in the format key = value . Let's say that there are two application properties defined in this file:

company = ABC batchism = 1000

At the application startup I will read this file whenever I use application properties in a class ApplicationProperties I will use this class if I need it

I have 2 options:

Option 1: Application properties Defined as static variables:

  public class ApplicationProperties {private static string company; Private static id BATCH_SIZE; Static {// read myapp.properties file and static variable computing & amp; BATCH_SIZE} Private ApplicationsApplications () {} Public Stabilized String Millcompany () {Return Company; } Public stable difference getBatchSize () {Return BATCH_SIZE; }}  

Option 2: Define the application properties as an example variable:

  public class ApplicationProperties {Personal Static ApplicationProperties INSTANCE = New ApplicationApplications (); Private string company; Private int batch size; Private ApplicationsApplications () {// read myapp.properties file and frequency variable computing & amp; Batch system) public static applicationProperties getInstance () {return INSTANCE; } Public string getCompany () {return.company; } Public int getBatchSize () {return.batchSize; }}  

For option 1 I will arrive this way:

  ApplicationProperties.getCompany (); ApplicationProperties.getBatchSize ();  

For option 2, I would like to reach out to:

  ApplicationProperties.getInstance (). GetCompany (); . ApplicationProperties.getInstance () getBatchSize ();  

Which is better? And why?

If the answer to this question has been answered first, please answer.

Option 2 is a bit more complex and verbose without offering any benefit So, option 1 is better design.

IMHO is not "opinion-based".


No comments:

Post a Comment