[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Please verify whether below clas will work as Singleton

Posted on 2012-03-20
13
Medium Priority
?
420 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

656 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