?
Solved

Please verify whether below clas will work as Singleton

Posted on 2012-03-20
13
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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