Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods or Singleton, which one to choose? [closed]

Possible Duplicate:
Difference between static class and singleton pattern?

Which is better in Java,

implementing public static methods, like

Factory.createLoginRequest()

or implementing Singleton pattern, like

Factory.getInstance().createLoginRequest()

(Boths will return a Request object.)

Which one is better and why ?

like image 727
Mohammad Ersan Avatar asked Sep 07 '11 06:09

Mohammad Ersan


People also ask

Which one should I choose static or singleton pattern?

A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state. A Singleton can be initialized lazily or asynchronously and loaded automatically by the .

Why should you avoid singletons?

By using singletons in your project, you start to create technical debt. Singletons tend to spread like a virus because it's so easy to access them. It's difficult to keep track of where they're used and getting rid of a singleton can be a refactoring nightmare in large or complex projects.

Why use singleton when we can have static fields?

The Singleton pattern has several advantages over static classes. First, a singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members).

Should singleton be sealed?

Marking the class sealed prevents someone from trivially working around your carefully-constructed singleton class because it keeps someone from inheriting from the class.


2 Answers

from wikipedia:

Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.

like image 193
Mahdi Hijazi Avatar answered Oct 11 '22 14:10

Mahdi Hijazi


It depends.

Choose singletons, because:

  • In general, I'd say a singleton is slightly neater because it allows you to do some initialization of the singleton object in/from the (private) constructor.
  • When it is later decided this object should no longer be a singleton (due to new insights or new requirements), it is easier to refactor it: you just need to change all the places that get the instance, instead of all calls to the static methods.

Use static methods, because:

  • In the specific case of android, you might prefer static methods for performance - I suspect calling a static function might be a bit faster (easier to optimize for the compiler) compared to calling a method on a singleton object.
like image 21
Arnout Engelen Avatar answered Oct 11 '22 13:10

Arnout Engelen