Is this a good use of Singleton?

I am a new programmer trying to find best practices. I am writing a simple class in one method. It takes a long time and returns an int (representing the amount of memory) depending on how long that duration is.

I need more than one of these memory models and it seems like each should be a separate Singleton. Is this a reasonable use of the Singleton pattern? I don't want just one instance that ever existed, but it looks like this class is conceptually a Singleton. Is this a good reason to make it a singleton? Or would it be advisable to add a private constructor and make the method static? Or am I overdoing it all? The code looks something like this:

public class MemoryModel implements SomeInterface {

   public int get(Long links) {

      final int result;

      if (links < 700_000) {
          result = 50_000;
      } else {
          result = 140_000;
      }

      return result;
   }
}

      

EDIT: If I understand that everyone has answered correctly, the answer is no. I don't need exactly one instance of this class - there is no reason to create one at all. I should just make the method static and never instantiate.

I also understand the meaning of the answers and links that the Singleton pattern is used and can be problematic since it creates a global entity. Singletons should only be used when there is a good reason to require only one copy.

+3


source to share


5 answers


You have to make it static ...

First, the class you have there is missing from the singlelet. Singleton is a class that ensures that you only create one instance of it.



Second, there is no reason (yet) why you need an instance. You need the instance to emit the state of the object. The object you are missing there has no state (no fields = no state), only a method - so it can be made static.

+2


source


Single classes are used when you can refer to the same class in multiple places without creating a new object of that class.

In the example you provided, it looks like a public public method since it has nothing to do with the class instance.



There is a difference between singlons and utility classes, please read this link.

+4


source


This is not the singleton pattern you have. You just have a method in the class.

You can probably read here first and then try the singleton implementation again:

Singleton Pattern

+1


source


Just because you only need one copy of something doesn't mean you have to make it a single. If you can avoid it, don't do it.

Your goal is to call new MemoryModel()

only once. Your problem is that you need a way to refer to this object from different places.

But the best option for this is to just pass a reference to that object. This solves the problem with single points: the global reference information static

they provide.

static final Object singleton = new Object();
static void doStuffBad() {
    // bad.. hardcoded reference & also global state
    System.out.println(singleton);
}

static void doStuffGood(Object reference) {
    // good, this does not create any bad dependencies etc.
    System.out.println(reference);
}

public static void main(String[] args) {
    doStuffBad();
    doStuffGood(new Object());
}

      


There are times when going around links becomes silly. Therefore some singletons like String.CASE_INSENSITIVE_ORDER

(a Comparator

) make sense. In fact, it might make sense in your case as well. It really depends on what you are trying to achieve and why you want to do something singleton. You don't have to if you want, because you need to access objects in different places and your object hierarchy is so convoluted that passing references is too painful.

+1


source


This is a static function.

Singleton is a creation pattern. Not only a unique instance, but also encapsulates the creation algorithm.

It doesn't make sense to use a singleton if you are using a singleton to duplicate memory, you have a static method and a static variable pointing to instances on the heap. But with a static method you only have a static method not a heap (and it is better to use heap in java).

+1


source







All Articles