Solved

Please verify whether below clas will work as Singleton

Posted on 2012-03-20
13
382 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:gurvinder372
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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Expert Comment

by:gurvinder372
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:gurvinder372
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 500 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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now