This discussion is archived
7 Replies Latest reply: May 30, 2012 2:06 AM by mrsAngelo RSS

to make singleton and to supply him resources to work

mrsAngelo Newbie
Currently Being Moderated
Hi,

I wont supply some resources to a “singleton” class for allowing him to properly work in the environment of the elaboration.

The classic solution to create “singletons” is to instantiate them using the method get_Instance(), and this is also the way (use of the same method get_Instance()) for getting instances of the “singleton”.

In this way, if I wont supply resources to the singleton I have to put parameters in the method get_Instance(list of parameters) and I have to continue to supply him the same parameters all the times that I need to get an instance from him.

Supply resources when I wont get instance of “singleton” is not not necessary, not comfortable and also not always possible.
Should be necessary separate the way to instantiate and the way to use the class...

I thought some solutions for this problem:
1. To use a further public method “_CONSTRUCTOR_TWO(list of parameters)” to supply the required information only after the first user of method Instance().
2. To use two different methods get_Instance( ), one of them with parameters an another without,
3. To use a class only to create the singleton …

No one of this solution seem me nevertheless suitable to solve the problem

I think this is a very common problem, and so I looked on Internet.
I read on the forums many threads about “using singleton” so I already know that supply resource to a Singleton is a controversial tip .
I read for ex.: “I'll make my point very clear: a singleton with parameters is not a singleton.”

Beyond the issues of principle I would like to receive some suggestions * based on practical experience * about how to use class "singleton" that makes use of resources that can not 'create in his internal but can to be only supply to it.

Thank you
Regards
Angelo Moreschini
  • 1. Re: to make singleton and to supply him resources to work
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Probably depends on what a 'resource' is.

    But if I wanted to pass something like a connection string I would just create an Initialize() method.
  • 2. Re: to make singleton and to supply him resources to work
    796440 Guru
    Currently Being Moderated
    mrsAngelo wrote:
    The classic solution to create “singletons” is to instantiate them using the method get_Instance()
    Actually, getInstance() should only return the existing singleton. There's no reason to lazily instantiate one.
    In this way, if I wont supply resources to the singleton
    Will they be a permanent part of the state of the singleton, set once and never changed?

    Or will they be a part of the state of the singleton that can change occasionally, but may stay the same across several calls to getInstance()?

    Or will they be different for each call to getInstance()?

    If different calls to getInstance() need to reflect different states for that object, then it probably shouldn't be a singleton, and you should probably fix your design.
  • 3. Re: to make singleton and to supply him resources to work
    mrsAngelo Newbie
    Currently Being Moderated
    The environment about I am talking is a JavaBean.
    There they are several classes that cooperate and it is comfortable (when it is possible) work with singleton, mainly to avoid legacy to connect objects that cooperate in situation not easily predicable .

    They are several similar cases and I'll talk about two that are easier to describe:

    1) The singleton need a reference for a component SIMPLY to show messages to the users JOptionPane.showMessageDialog(*component* , “message to show”);

    2) The duty of the singleton is to refresh information on the screen, always on the same way, this work can be required many times from many objects, and the elaboration need to know the state of the JavaBeans at the moment to make the work. In this case the resource is areference to some objects that can supply the values.


    I thought about this problem and, I think really I don't need of a singleton pattern howit is described in the theory and in the philosophy of the patterns ...
    In every cases all of the classes that I realized with singletons make the same work in different situations being used by objects that not easily can have information about the instantiation (legacy) of the singleton object .

    I thought still about how solve this problem and, in consideration that the environment of the work is not so dynamic, perhaps the best solution is to individuate the first use of singleton and supply it only in this moment the resources by a special method that can be called singleton_CONSTRUCTOR_AUXILIARY(resource..);

    thank the both of you for your answer

    regards

    Angelo Moreschini
  • 4. Re: to make singleton and to supply him resources to work
    mrsAngelo Newbie
    Currently Being Moderated
    Hi,

    I, also, am thinking about the possibility to get the instances of the classes that I need (as resources) using the Reflection...
    some thing like this for example :
    Class.forName("DBbeanNavigator")..

    any help to work in this direction is appreciated

    regards
    thank you

    Angelo Moreschini
  • 5. Re: to make singleton and to supply him resources to work
    gimbal2 Guru
    Currently Being Moderated
    Seems like you want to reinvent the wheel. I would seriously look into the Spring framework if I were you, all this kind of wiring you want to do is what people love the framework for.
  • 6. Re: to make singleton and to supply him resources to work
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    mrsAngelo wrote:
    The environment about I am talking is a JavaBean.
    That doesn't mean much.
    There they are several classes that cooperate and it is comfortable (when it is possible) work with singleton, mainly to avoid legacy to connect objects that cooperate in situation not easily predicable .

    They are several similar cases and I'll talk about two that are easier to describe:

    1) The singleton need a reference for a component SIMPLY to show messages to the users JOptionPane.showMessageDialog(*component* , “message to show”);
    I don't do GUIs but I can't imagine any reason that a singleton would be needed for that.

    >
    2) The duty of the singleton is to refresh information on the screen, always on the same way, this work can be required many times from many objects, and the elaboration need to know the state of the JavaBeans at the moment to make the work. In this case the resource is areference to some objects that can supply the values.
    Refreshing has nothing to do with it. But presumably you are using it to gather information from disparate sources.
    In every cases all of the classes that I realized with singletons make the same work in different situations being used by objects that not easily can have information about the instantiation (legacy) of the singleton object .
    If you have many different singletons doing what you stated above then I would suspect that your design is broken.
  • 7. Re: to make singleton and to supply him resources to work
    mrsAngelo Newbie
    Currently Being Moderated
    Thank you Jshell for your answer,

    I already took the decision to use a special method: singleton_CONSTRUCTOR-TWO(list of resources) to manage the problem, and now I already finished my work.

    I made my decision, thinking to definition of pattern :-) " a solution to a problem *in a particular environment*”.
    In this way I thought that the decision to use a auxiliary constructor as well is not a very good logic model in general, is however the logical model that meet all the requirements to realize the work that I need to do (the best in the particular environment).

    It is good, when we find a new problem, talk with other programmers how to solve it.

    Thank you and good work
    Regards
    Angelo Moreschini

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points