?
Solved

Please verify whether below clas will work as Singleton

Posted on 2012-03-20
13
Medium Priority
?
430 Views
Last Modified: 2012-03-26
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?
0
Comment
Question by:chaitu chaitu
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 37745933
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37745936
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37745992
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37745996
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37746016
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37746020
for_yan,

you mean to say this is correct.

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

Expert Comment

by:Gurvinder Pal Singh
ID: 37746031
<<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
 
LVL 47

Expert Comment

by:for_yan
ID: 37746033
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37746039
Yes, this seesm fine to me:
public static Properties getProperties() {
            instance= LoadProperties.getInstance();
            return instance.properties;
      }
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37746077
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37746149
<<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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37746560
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37748817
>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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question