Singletons - How to use it better?

The web application has Actions <--> Services <--> Implementations. The user comes to the UI, changes some settings (pagination size, show/hide properties), and these settings are saved in an XML file. The XML file is read/written by a 'UIConfiguration' class and settings are stored as a bean. My singleton 'ConfigService' instantiates this UIConfiguration. The code is like this (how do I post code here?)
public class ConfigService {
private Configuration uiConfiguration;
private static ConfigService instance = null;
private Map<String, ConfigBean> uiConfig;
private ConfigService(){
init();
private void init(){
uiConfiguration = new UIConfiguration();
try {
uiConfig = uiConfiguration.getConfigurations();
} catch (Exception e) {
public String getServerUrl(){
return "http://"+ uiConfig.get("server.hostname").getValue()
+ ":" + uiConfig.get("server.port").getValue();
public String getUIProperty(String propertyName){
return uiConfig.get(propertyName).getValue();
public static ConfigService getInstance(){
if(instance == null)
instance = new ConfigService();
return instance;
public void updateUIConfig(Map<String, ConfigBean> configs){
uiConfig = configs;
public Map<String, ConfigBean> getAllUIProperties(){
return uiConfig;
**Things that I think that I am not doing right are:* *
1. I have ConfigService.getInstance() in the contextlistener.
2. Also, everywhere in the application I do ConfigService.getInstance() whereever I need a UI property.
3. I do ConfigService.getInstance().updateUIConfig(configurations) in my UIConfiguration class when I update the properties.
Is this a right way to do this? Is there any better/clean way of doing this. I also use Spring to inject services into my action classes. Can that help to better the behavior anyhow?
Please help me here.

user2893271 wrote:
2. Also, everywhere in the application I do ConfigService.getInstance() whereever I need a UI property.
3. I do ConfigService.getInstance().updateUIConfig(configurations) in my UIConfiguration class when I update the properties. There's nothing necessarily wrong with either of those.
On the other hand, if you have a lot of getInstance() calls in the same method or class, you might want to make a local or member variable and just assign it once, rather than repeatedly calling the method. This would be more for simplicity and clarity, but if you have a lot of activity there across many threads, it's possible that lock contention (if you go with lazy instantiation and a synchronized getInstance() method) could have a performance impact. (Do NOT assume that it will and then munge up your code just based on that. Only make performance-related changes if you have measured and found that to be a bottleneck.)
One other issue is whether you should even have a Singleton in the first place. There's been a lot of backlash against Singleton lately. It's true that it is overused and abused, but, IMHO, the backlash has been just as over the top and knee-jerky as the initial "let's use singleton everywhere" approach. There are cases where singletons are appropriate. Yours may be one, or it may not. I don't know enough of your design to comment on it one way or the other, and I'm not interested in pursuing that question. I just want to raise the issue to you as something to think about and possibly research further.

Similar Messages

  • How long use with BETA drivers

    2_08_0002Beta driver has been a ok driver!! But when will we get a full retail driver? A company like Creative should and could make real working drivers you think?
    I want new drivers for my Audigy 2 card, and yes i know there are 2_07_0004 drivers out, but they are originally for X-Fi! So please creative... make me a happy costumer!!

    The most reliable way would be to load each driver (and all code using it) using its own classloader.
    But that would likely be overly complicated.
    The most viable solution will likely be (especially as this is a short term project, you say it's going to be retired soon) to ensure a commit after every smaller subset of the total data volume and keep using the Oracle 8 drivers.
    Try forcing a commit after say every 100 records and see if you still have the timeout problems. Most likely this will take care of your problems at the cost of a slight performance decrease (but better somewhat less than optimal performance than risk database corruption or crashing applications, right?).

  • How to use a better codec.

    Hi, guys.
    I've installed a codec on my system called "Lagartith", which is said to be completely lossless. I don't know exactly where it lives, but it's in the box somewhere!
    I thought it would be handy if I could coax PE7 to use it when encoding my films.
    Anyone know how to do this?
    Thanks,
    Kurt 

    Haha! I'm not even going to go to that Red Camera link, Hunt, in case I get tempted to mortgage the house!!
    Thanks for that in-depth info on codecs. To answer your follow-up question, I wanted to be able to improve the final output using a lossless codec, instead of the PE7 one. Not that I have a clue what PE7 uses, but I figured it probably wasn't completely lossless.
    Anyway, I'd best just stick to the defaults for a while before I try to get too fancy.
    I should say at this point that I'm a rank amateur, and haven't a clue about much of the secret life of my computer's software. Although I'm not totally ignorant, and I'm probably more computer-literate than most of my friends and family, I'm nevertheless just feeling my way in this field of video editing with a PC. Before this, I used a Mac, with its entry-level suite, and I picked that up quickly.
    But having bought a new computer purpose-built and dedicated to all things graphical, I'm having to educate myself pretty quickly! For some odd reason I expected that an upmarket app like PE7 would be a piece of cake and that I'd be able to just set it to cruise control and sit back like Homer Simpson. But I'm discovering otherwise, and actually I'm loving it! It's a challenge to the gray matter.
    Cheers,
    Kurt.

  • Hi, I switched off photo stream in iphone in settings and as a result those photos even got deleted in iPad. How do I retrieve them? How to use this better to avoid such stuff?

    I switched off photo stream in my iphone and as a result those 1 months photos got deleted on iPad. How can I retrieve them? How to avoid this in future?

    This happened because iCloud only keeps photo stream photos in iCloud for 30 days, even though your last 1000 photo will remain on your device until deleted.  Turning off photo stream deleted them from your device; reconnecting to photo stream redownloads photos from the last 30 days (as older ones are no longer in iCloud). 
    To keep your photos permanently, import them to your computer while they are still in your camera roll, as explained here:http://support.apple.com/kb/HT4083.  To save photo stream photos, save them to your camera roll so they can be imported too.

  • How to use iMessage better. - Issues

    I want to send a group message and is there a easy way of selecting all the contacts in one go?
    Why does deleted iMessages on the iPhone reappear on the iPad? It starts downloading all the history all over again? I want it sync only the unread messages, not the deleted ones.

    just send that person a text.   about imessage http://support.apple.com/kb/HT3529

  • More documentation on how to work the beta xcelsius on the biod

    Hello experts,
    Is there more documentation specifically on how to use the beta xelcius included in the biod, i haven't been able to use it because i can't see the excel datasheet so i can bind the data!!!!!!!!!!
    thanks a lot

    Steve,
    thanks a lot for your answer, i follow the steps in the link you suggested me and when i'm almost finished, when i try to see the template this error appears:
    "To access external data, add a cross-domain policy file to the external data web server.
    For more information, on the Adobe website, see the article u201CCross-domain Policy File Specificationu201D.
    If the problem persists, contact the file creator or your system administrator.
    Error: Error #2032
    Connection Type: Web Service Connection
    File URL: https://bi.ondemand.com/xcelsius_template_subclass_extension/template_file/28030/Template_4926942437686944813.swf?1318528121.0
    External Data URL: https://bi.ondemand.com/xcelsius_templates/soap_sample_data/1dteSwG4nIA1gQi9MSo7ZSfq32/OnDemandWSConnection"
    do you have any idea of what i did wrong?
    thanks a lot
    regards

  • When a context node use non-singleton, how to invoke the supply funtion eve

    when a context node use non-singleton, how to invoke the supply funtion everytime the lead selection is changed ?

    Hi wei,
    <b>Non-singleton nodes:</b>
    Web Dynpro allows you to define non-singleton nodes. Each non-singleton node has one node instance for each node element of the parent collection at runtime. The advantage is that each instance can be accessed directly. When using non-singleton nodes, the nodes are only created when the node values are retrieved. This can save resources that otherwise would slow down the performance of the application.
    <b>Supply function:</b>
    The Web Dynpro tools also automatically enhance the corresponding controller class with a supply function including the user coding area contained in it
    Supply functions are implemented as methods of type
    <b>public void supplyChildNodeElements(SomeChildNode node,
                                        SomeParentNodeElement)</b>
    in a Web Dynpro controller (view controller or custom controller). Supply functions and context nodes have a 1..1 relationship, that is, supply functions are specific for individual context nodes. Under certain conditions, supply functions are called by pages in the Web Dynpro runtime environment.
    Hope this helps u,
    Regards,
    Nagarajan.

  • How to use Buffering to make image better

    Rigth now I have a DFS maze program, that basically is like a grid maze with three buttons. Randomize (which, duh, randomizes the maze), Solve, and Statistics. When I click on the statistics button, another "wing" opens up on the left showing a text area. But when the text area is being built, what is actually going on in the background processing, is that thousands of maze iterations are running, and then the statistics is printing out. Of course when these iterations are running the maze screen goes blank, and there is one long blink. I would like to make it so that the last maze image stays, until all the statistics in the background is done, and then just the statistics is shown, and not the final maze iteration. So basically the maze image on the right will just be the maze image right before the statistics started running.

    Hi aspirantme,
    >>how to use radioButton(s) and image controls on windows phone 8.1
    Which version of your app is? Runtime or Silverlight?
    For Runtime version, please see the following articles:
    #RadioButton class
    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.radiobutton(v=win.10).aspx
    #How to add radio buttons (XAML)
    https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868200.aspx
    #Image class
    https://msdn.microsoft.com/library/windows/apps/br242752.aspx
    For Silverlight version, please refer to the following documents and guidelines:
    #RadioButton Class
    https://msdn.microsoft.com/en-us/library/windows/apps/system.windows.controls.radiobutton(v=vs.105).aspx
    #RadioButton control design guidelines for Windows Phone
    https://msdn.microsoft.com/en-us/library/windows/apps/hh202881(v=vs.105).aspx
    #Image Class
    https://msdn.microsoft.com/en-us/library/windows/apps/system.windows.controls.image(v=vs.105).aspx
    #Quickstart: Images for Windows Phone
    https://msdn.microsoft.com/en-us/library/windows/apps/jj206957(v=vs.105).aspx
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Does anyone know if apple's one-to-one program would be a good way to learn how to use logic pro or am I better off going to school to learn audio engineering or something?

    Of course going to school would be a good option, but I want to know if one-to-one is also a good way to learn how to use logic pro. Has anyone been through the one-to-one program for logic pro and can say that they learned how to use logic pro well because of it?

    For sure, one to one training, if given by a tutor who is capable, will hand you the means to build up self-confidence  and will therefore let you operate the hard/software in an intelligent manner, instead of going for the trial and error method which has its pro's and con's too. Once you've passed this beginners phase you will make your own decisions intelligently and then you will also start to get experience and learn even from your mistakes. Something like that in theory and the rest is up to you!
    Have a nice day

  • How to use Lightroom 1.0 w/ CS Beta and Bridge Beta 2

    I am currently using CS3 beta and Bridge Beta 2 and Windows XP Pro, and I have ordered Lightroom 1.0.
    I have a few questions as I envision the workflow{s} I might be able to use:
    1. Can I export to CS3 Beta or will I need to export to CS2 until the final CS3 comes out.
    2. Can I edit in CS3 Beta directly from Lightroom 1.0?
    3. I am confused as to the role of Bridge 3.7 might play in the above scenarios.
    Thanks,
    Ina

    Well, ACR 3.7 will play a role in allowing Bridge and CS2 the ablility to interpret Lightroom adjustments. ACR 4 with CS3 is an even greater upgrade.
    From LR you will be able to select 'edit in Photoshop' and the image will open in Photoshop with your LR adjustments applied. LR will then update the new file (TIFF, PSD, whatever you choose) to show your PS edits.
    I hope that helps.

  • HT1695 How can use my factory unlocked iphone better ?

    i want to know more about how to use siri?
    Is data necessary for siri?
    guide me well!

    http://www.imore.com/siri-ultimate-guide
    http://www.apple.com/iphone/features/siri-faq.html

  • How to delete the beta version in icloud?

    I purchased iWork half an year ago and I use it in icloud. But since the beta version has appeared, it´s not possible to work with my own software. Who knows how to delete the beta version from icloud?

    You can't delete it.
    Why do you need to?  Why can't you just work with your documents like everyone else does, by opening iWork software on your Mac and using File > Open, etc to see your docs that are in iCloud?  Why would you need to use your browser?

  • Re: [SunONE-JATO] Re: How to use a tiled view without a model

    I'm not sure what is different for you now. You still parse the string
    and put it into a data structure. Before the data structure was a
    vector, in JATO its just a model with a "hidden" data structure (a hash
    map).
    MVC only really comes into play when you talk about where your write
    this code, and where the data structure is being stored. So really, JATO
    takes care of half of the MVC'ness of it all (where the data is store).
    You just decide where to be the code to populate the model.
    Make sense?
    Is there something different required of you in JATO in this scenario
    that I am not grasping?
    c
    Hoskins, John D. wrote:
    Thanks for the feedback.
    The problem I was solving involved a single string, which contained
    delimited subsets of information.
    The string looked like
    this:"time|analyst|description|time|analyst|description|..."
    In ND, I parsed it apart into it's components (time vector, analyst vector,
    description vector), populated the repeated.
    With JATO, how would I make a model for something that doesn't have a
    database component like this?
    I'm pretty new to this MVC thing, so bear with me.
    John D. Hoskins
    Telephone & Data Systems
    Application Development & Support
    Voice: 608.664.8263
    Fax: 608.664.8288
    Email: john.hoskins@t...
    -----Original Message-----
    From: Craig V. Conover [mailto:<a href="/group/SunONE-JATO/post?protectID=219212113009229091025149066024064239039098031198039130252055210">craig.conover@s...</a>]
    Sent: 6/26/2002 3.22 PM
    Subject: Re: [SunONE-JATO] Re: How to use a tiled view without a model
    I guess the only thing "weird" (for lack of a better term) about what
    you are doing is that your are populating the model on the "display
    cycle". Typically, the cycle goes like this:
    Request -> populate model -> update data store -> retrieve data to
    populate model -> display data
    some of the above steps are optional but hopefully you get the point I
    am making.
    So what you are doing is:
    Request -> populate model/display data
    If it works for you, then it's not necessarilly wrong. But I would
    probably have my model populated before I forwarded to the target
    (displaying view bean) or at a minimum, in the begin display event of
    the view bean or the tiled view, but not during the iteration of the
    tiled view.
    c
    jhoskins wrote:
    Craig,
    Thanks for the pointers. I ended up doing something else. I set the
    models setSize() method to set the max size, and as the tiles fields
    iterated, populated the value from some vectors I had the data in
    already. Is this solution fraught with peril and will ultimately fail,
    or should I try your way?
    John
    --- Craig V. Conover wrote:
    John,
    Check out the docs for DefaultModel. There is an appendRow() method.
    So get your tiledview's primary model (the tiledview's primary model
    should be set to use an instance of DefaultModel), model.appendRow(),
    then model.setValue("fieldname", value) for each value.
    Rinse, repeat as needed.
    c
    jhoskins wrote:
    I would like to use a tiled view, but populate the fields manually.
    Any pointers about where I can set the size of the tiled view? I tried
    setMaxDisplayTiles() in the beginDisplay, but it won't get down and
    generate the rows.
    John Hoskins
    To download the latest version of JATO, please visit:
    http://www.sun.com/software/download/developer/5102.html
    For more information about JATO, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp
    To download the latest version of JATO, please visit:
    http://www.sun.com/software/download/developer/5102.html
    For more information about JATO, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp
    To download the latest version of JATO, please visit:
    http://www.sun.com/software/download/developer/5102.html
    For more information about JATO, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp
    To download the latest version of JATO, please visit:
    http://www.sun.com/software/download/developer/5102.html
    For more information about JATO, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp

    Craig,
    Thanks for the pointers. I ended up doing something else. I set the
    models setSize() method to set the max size, and as the tiles fields
    iterated, populated the value from some vectors I had the data in
    already. Is this solution fraught with peril and will ultimately fail,
    or should I try your way?
    John
    --- "Craig V. Conover" wrote:
    John,
    Check out the docs for DefaultModel. There is an appendRow() method.
    So get your tiledview's primary model (the tiledview's primary model
    should be set to use an instance of DefaultModel), model.appendRow(),
    then model.setValue("fieldname", value) for each value.
    Rinse, repeat as needed.
    c
    jhoskins wrote:
    I would like to use a tiled view, but populate the fields manually.
    Any pointers about where I can set the size of the tiled view? I tried
    setMaxDisplayTiles() in the beginDisplay, but it won't get down and
    generate the rows.
    John Hoskins
    To download the latest version of JATO, please visit:
    http://www.sun.com/software/download/developer/5102.html
    For more information about JATO, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp

  • How to use JDBC Connection Pools in a standalone application?

    Hi, there,
    I have a question about how to use JDBC Connection Pools in an application. I know well about connection pool itself, but I am not quite sure how to keep the pool management object alive all the time to avoid being destroyed by garbage collection.
    for example, at the website: http://www.developer.com/java/other/article.php/626291, there is a simple connection pool implementation. there are three classes:JDBCConnection, the application's gateway to the database; JDBCConnectionImpl, the real class/object to provide connection; and JDBCPool, the management class to manage connection pool composed by JDBCConnectionImpl. JDBCPool is designed by Singleton pattern to make sure only one instance. supposing there is only one client to use connection for many times, I guess it's ok because this client first needs instantiate JDBCPool and JDBCConnectionImpl and then will hold the reference to JDBCPool all the time. but how about many clients want to use this JDBCPool? supposing client1 finishes using JDBCPool and quits, then JDBCPool will be destroyed by garbage collection since there is no reference to it, also all the connections of JDBCConnectionImpl in this pool will be destroyed too. that means the next client needs recreate pool and connections! so my question is that if there is a way to keep pool management instance alive all the time to provide connection to any client at any time. I guess maybe I can set the pool management class as daemon thread to solve this problem, but I am not quite sure. besides, there is some other problems about daemon thread, for example, how to make sure there is only one daemon instance? how to quit it gracefully? because once the whole application quits, the daemon thread also quits by force. in that case, all the connections in the pool won't get chance to close.
    I know there is another solution by JNDI if we develop servlet application. Tomcat provides an easy way to setup JNDI database pooling source that is available to JSP and Servlet. but how about a standalone application? I mean there is no JNDI service provider. it seems a good solution to combine Commons DBCP with JNID or Apache's Naming (http://jakarta.apache.org/commons/dbcp/index.html). but still, I don't know how to keep pool management instance alive all the time. once we create a JNDI enviroment or naming, if it will save in the memory automatically all the time? or we must implement it as a daemon thread?
    any hint will be great apprieciated!
    Sam

    To my knoledge the pool management instance stays alive as long as the pool is alive. What you have to figure out is how to keep a reference to it if you need to later access it.

  • How to use ssd as boot drive and hdd as storage on a macbook pro 13

    Does anyone know how to use SSD as boot drive and have HDD in optical bay as storage on a Macbook pro 13. I mean in that both drives work as one . by formatting using mac software . I would like to run all programs and OS but would love to have my downloads and useless data on run on my 1tb 5400rpm hdd without me have to manual send it there..

    Does anyone know how to use SSD as boot drive and have HDD in optical bay as storage on a Macbook pro 13. I mean in that both drives work as one
    Each drive mounts as a separate volume. There is no convenient way to make them look like one contiguous volume.*
    Franky, with an hdd and a ssd I would recommend you put your home dir on the hdd and leave the ssd for the OS and your key applications.  It's relatively easy to do and I think better than moving only portions of your stuff.
    How to Move the Home Folder in OS X – and Why
    * There are ways to to make musltiple volumes appear as one large drive but I think it is beyond the scope of this discussion and not appropriate to this configuration.

Maybe you are looking for