This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Mar 29, 2012 4:06 PM by jschellSomeoneStoleMyAlias RSS

How to create more kinds of singleton objects?

916217 Newbie
Currently Being Moderated
Hello,

in my application I need to create more objects. Every object does during its creation same actions (they differ only in one parameter) and add some own methods.

These all objects should behave as a singleton pattern.

Could you help me (different design pattern) how to achieve this?

Thank you.
  • 1. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    Hello,

    in my application I need to create more objects.
    So what's stopping you?
    These all objects should behave as a singleton pattern.
    So, these objects are all of different classes? If so, what's the problem?

    Or are you saying that you want a fixed number of instances of the same class, that you control their creation, and that no more can be created? If so, that's essentially a [url http://en.wikipedia.org/wiki/Multiton]Multiton(⇐click) which is implemented in Java by [url http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html]enums.(⇐click)
  • 2. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    So, these objects are all of different classes?
    No. As I said, these objects are totaly same (differ in one parameter) and I don't want to duplicate my code to create different classes.
    Or are you saying that you want a fixed number of instances of the same class, that you control their creation, and that no more can be created?
    Almost exactly as you write, but these objects (as I said) add some more methods. I would like to have these methods separated.
  • 3. Re: How to create more kinds of singleton objects?
    gimbal2 Guru
    Currently Being Moderated
    Its hard to tell, but this piece of text:
    Every object does during its creation same actions (they differ only in one parameter) and add some own methods.
    makes me think that the OP right now has one singleton class that doesn't fit the requirements and now wants to create more singleton classes that are not completely new, but add only the new functionality that is unique to them on top of the existing singleton class.


    EDIT: ninja'd by the OP. You may want to look into using an abstract base class to give all singletons the same logic without having to duplicate code.

    http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
  • 4. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    Ok I'll try it another way - I'm in a situation when I need multiple objects that behave as in singletons but:

    a) all have the same code and differ only in one parameter
    b) these objects add their own methods (but again, almost the same)
    c) I do not want to duplicate application code

    How do you deal with this situation?
  • 5. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    Or are you saying that you want a fixed number of instances of the same class, that you control their creation, and that no more can be created?
    Almost exactly as you write, but these objects (as I said) add some more methods. I would like to have these methods separated.
    So, as I suggested, use an enum.
  • 6. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    Ok I'll try it another way - I'm in a situation when I need multiple objects that behave as in singletons but:

    a) all have the same code and differ only in one parameter
    b) these objects add their own methods (but again, almost the same)
    If they have the same methods but different implementations, then an enum is fine. If they have different methods (not just different implementations of the same methods), then they can't be the same class, and your question is very unclear.
    c) I do not want to duplicate application code

    How do you deal with this situation?
    Did you bother to click the links I provided in my first response?

    Once again: [url http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html]enums.(⇐click)

    Edited by: jverd on Mar 29, 2012 10:09 AM
  • 7. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    So, these objects are all of different classes?
    No. As I said, these objects are totaly same
    You didn't say they were the same class. The wording you used was not clear, and could have applied to different classes.
  • 8. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    If so, that's essentially a Multiton(⇐click) which is implemented in Java by enums.(⇐click)
    Multiton patter looks fine. if I this pattern understood correctly, the different kinds of "FooMultiton singleton" objects (differed in key parameter) will have all own methods in FooMultiton class?
  • 9. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    - there is one general setting object, this object reads settings from JAVA properties files (this file is previously mentioned parameter)
    - when this object is invoked it checks if properties file exists, is readable and some next controls (this must do all kinds of setting objects)

    - I want to use this universal object to create another kinds of setting - main application setting, e-mail setting, database setting setting etc. These settings objects do in practise same things:

    Database setting:

    - own method that returns string with database name
    - own method that returns string with database server ip address
    - etc...

    Application setting:

    - own method that returns string with server ip address
    - own method that returns int with server port
    - etc...

    Mail setting

    - own method that returns string email subject
    - own method that returns string email body
    - etc...

    So it is evident that all these types of settings objects returns own setting (String, Integer, Boolean) but these settings are read in exactly the same way from properties file (and here I don't want to duplicate my code).

    The reason for the singleton is I don't want to reload setting everytime setting object is invoked.

    Edited by: 913214 on 29.3.2012 7:33
  • 10. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    - there is one general setting object,
    I don't see the relevance of any of that, nor do I understand what point you're trying to make.

    For the third (fourth?) time: Use an enum. If you don't think that suits your needs (AFTER studying up on it, not just making assumptions), please explain why. (Seriously, if you can't be bothered to read the responses given, to put some effort into researching them, and to engage in a dialogue based on those responses, why even bother posting?)
  • 11. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    Ok, try to explain how to do what I need with Enums. If you don't understand my needs I'm afraid I'm not able to explain it more clearly for you...
  • 12. Re: How to create more kinds of singleton objects?
    916217 Newbie
    Currently Being Moderated
    I have such slightly modified Multiton pattern class.

    Now I want create a method that returns database server IP address from database setting type - try to tell me (exactly) how to write this method.
    public class Setting {
    
         public enum Type {
    
              database, application, mail
         }
         private static final Map<Object, Setting> instances = new HashMap<Object, Setting>();
         protected PropertiesFile file = null;
    
         private Setting(Setting.Type key) throws IOException {
              file = new PropertiesFile(key.toString() + ".properties");
         }
    
         public static Setting getDatabase() throws IOException {
              return Setting.getInstance(Setting.Type.database);
         }
    
         public static Setting getApplication() throws IOException {
              return Setting.getInstance(Setting.Type.application);
         }
    
         public static Setting getMail() throws IOException {
              return Setting.getInstance(Setting.Type.mail);
         }
    
         private static Setting getInstance(Setting.Type key) throws IOException {
              Setting instance = instances.get(key);
              if (instance == null) {
                   instance = new Setting(key);
                   instances.put(key, instance);
              }
              return instance;
         }
    }
  • 13. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    Ok, try to explain how to do what I need with Enums.
    No, that's not the way it works. You need to follow the link I provided, study up on enums, maybe do some additional google searches, and then, if you have specific questions, post them here, or, if you don't think enums are appropriate, explain why. It's not my job to spoon-feed you stuff that you can and should be researching yourself.
  • 14. Re: How to create more kinds of singleton objects?
    796440 Guru
    Currently Being Moderated
    913214 wrote:
    I have such slightly modified Multiton pattern class.

    Now I want create a method that returns database server IP address from database setting type - try to tell me (exactly) how to write this method.
    No. This is not a code service. You need to try it and ask a specific question about the specific parts that you're getting stuck on. There are plenty of examples available out there that show what you're trying to do.

    Also, the whole Setting class looks pretty pointless, and, if you're going to have it, make it static and get rid of all those static methods to get the enum values.
1 2 Previous Next

Legend

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