Please verify whether below clas will work as Singleton

in continuation to the below Q i have one more clarification.

http://www.experts-exchange.com/Programming/Languages/Java/Q_27639746.html


public class LoadProperties {
      private static Properties properties = new Properties();

      
      public static Properties getProperties() {
            return properties;
      }

      
      public static void setProperties(
                  Properties properties) {
            LoadProperties.properties = properties;
      }

}


1)Initially am loading properties file in EJB class and same will be proagated to servlets as well;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.properties");
LoadProperties.getProperties().load(inputStream);

2)In the current application we are accessing like this in so many places.
LoadProperties.getProperties().getProperty("type");
LoadProperties.getProperties().getProperty("path");

3)As per suggestion I want to make LoadProperties as singleton and accordingly I need to change the accessing
Property values like below one instead of mentioned in step-2;so its cumbersome to changee  in so may places.so i dit like this;

LoadProperties.getInstance().getProperties().getProperty("type");


4)To Resolve this I changed the class like this and see mainly getProperties method.My aim is to get singkle reference of LoadProperties
through out tha application.

public class LoadProperties {
      private static Properties properties = new Properties();
      private static LoadProperties instance = new LoadProperties();

      
      private LoadProperties()
      {
            
      }
      
      public static LoadProperties getInstance() {
        return instance;    
       
}
      public static Properties getProperties() {
            instance= LoadProperties.getInstance();
            return instance.properties;
      }

}

Please suggest whether am i doing correct or not?
LVL 20
chaitu chaituAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
>if you load properties file in constructor then that may times the properties file will loaded  whenver you call like this

no it would not

It will load only for the first time wheh you call getInstance from getProperties method- all othert times - it will just check checj that there is an inatstance alredy created and wil  not even call constructir - just retyurn exostsing instance
0
 
ksivananthCommented:
by declaring the Properties as static, you are making it class instance specific of LoadProperties which supresses a feature of singleton.

I would suggest you to make it as instance variable and accessed by LoadProperties.getInstance().getProperties().getProperty("type");

this is the cleaner way, refactoring is there to help you changing in many places.
0
 
Gurvinder Pal SinghCommented:
1) If you want to make this class LoadProperties as stateless class, then this seems fine

2) rather than sharing the complete Properties object, expose methods for setting and getting individual properties

3) put load method in LoadProperties itself as a private method which will be invoked by getInstance method, or getProperty method
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
chaitu chaituAuthor Commented:
ksivanath,

"I would suggest you to make it as instance variable and accessed by LoadProperties.getInstance().getProperties().getProperty("type");"

I didnt get you.you want make like this;if i do like this wat is the advantage  will i get?

private  Properties properties = new Properties();

As you said I can do refactoring but we dont know where the application breaks and apart from that i need to do testing of entire application.At this stage i dont to take that risk.

Instead of making changes in all places i want to make simple change to make it effect of entire application.
0
 
chaitu chaituAuthor Commented:
gurvinder372,

2)rather than sharing the complete Properties object, expose methods for setting and getting individual properties

what is the advantage will i get if i make setter and getter methods.

3) put load method in LoadProperties itself as a private method which will be invoked by getInstance method, or getProperty method

Please show me .
0
 
for_yanCommented:
I think more normally singletons has such getInstance() method
which ensures that it is singleton:

    public static LoadProperties getInstance() {
       
if(instance == null)    {
 instance = new  LoadProperties();
}
return instance;
}

Open in new window





and you may initialize properties in constructor


It looks like getProperties() method should work and you'll not need to change the format
0
 
chaitu chaituAuthor Commented:
for_yan,

you mean to say this is correct.

    public static Properties getProperties() {
            instance= LoadProperties.getInstance();
            return instance.properties;
      }
0
 
Gurvinder Pal SinghCommented:
<<what is the advantage will i get if i make setter and getter methods.>>
Classes that need to access properites do not need to know how to access a single property value, giving you a flexibility to change the implementation of LoadProperties. For example, tomorrow you can hard code some properties into a  hashmap or load the properties directly into hashmap in case you need to extend the functionality from simply fetching a string property value

<<Please show me .>>

LoadProperties loadProp = null;
private void load ()
{
   //load properties from input stream
}

private LoadProperties()
{
   
}

public LoadProperties getInstance()
{
   if ( loadProp == null )
   {
       loadProp = new LoadProperties ();
       load ();
   }
  return loadProp;
}

public String getProperty( String key )
{
   if ( loadProp == null )
   {
       loadProp = new LoadProperties ();
       load ();
   }
   return loadProp .getProperty(key);
}
0
 
for_yanCommented:
I think you can do it thos way:
public class LoadProperties {
      private static Properties properties; 
      private static LoadProperties instance; 

      
      private LoadProperties()
      {
            properties = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.properties");
properties.load(inputStream);


      }
      
   public static LoadProperties getInstance() {
       
if(instance == null)    {
 instance = new  LoadProperties();
}
return instance;
}


      public static Properties getProperties() {
            instance= LoadProperties.getInstance();
            return instance.properties;
      }

}

Open in new window

0
 
for_yanCommented:
Yes, this seesm fine to me:
public static Properties getProperties() {
            instance= LoadProperties.getInstance();
            return instance.properties;
      }
0
 
chaitu chaituAuthor Commented:
gurvinder372,

your code looks fine to me but there is problem to implement your code.

this is my existing class .

As i said we are accessing property values in so many places like this(around 1000 places);

LoadProperties.getProperties().getProperty("type");
LoadProperties.getProperties().getProperty("path");


I don't want to change the code of accessing the values instead of that i want to change the existing class as Singleton and above code  should work without making lot of changes.

if i write your code then i need to make changes in all places like this;

LoadProperties getInstance().getProperty("type");

if i implement for_yan's suggestion then no need to change the code anywhere.



public class LoadProperties {
      private static Properties properties = new Properties();

      
      public static Properties getProperties() {
            return properties;
      }

      
      public static void setProperties(
                  Properties properties) {
            LoadProperties.properties = properties;
      }

}

Open in new window

0
 
Gurvinder Pal SinghCommented:
<<As i said we are accessing property values in so many places like this(around 1000 places);>>
In that case, there is very little you can do to your current code
0
 
chaitu chaituAuthor Commented:
for_yan,

   private LoadProperties()
      {
            properties = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.properties");
properties.load(inputStream);
      }


if you load properties file in constructor then that may times the properties file will loaded  whenver you call like this

 LoadProperties.getProperties().getProperty("type");
LoadProperties.getProperties().getProperty("path");

to restrict this am loading this property file in EJB through servlet listner class only once;

once if i get instance of LoadProperties in ear level class loader then for WAR level classes also get same instance so in that way no need to load properties file multiple times .am i right?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.