Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different @Singleton & static @Provides in dagger2

Tags:

May I know the different between @Singleton VS static Provides in dagger2?

@Provides static User currentUser(AuthManager authManager) {     return authManager.currentUser(); }  @Provides @Singleton User currentUser(AuthManager authManager) {     return authManager.currentUser(); } 
like image 527
Shawn Thye Avatar asked Aug 13 '16 14:08

Shawn Thye


People also ask

How many types of singleton are there?

There are two types of singleton implementations: eager and lazy initialization.

What are the examples of singleton?

Example of singleton classes is Runtime class, Action Servlet, Service Locator. Private constructors and factory methods are also an example of the singleton class.

Is it OK to use singletons?

The truth is that singletons aren't inherently bad if they're used correctly. The goal of the singleton pattern is to ensure only one instance of a class is alive at any one time. That, however, is not the goal many developers have in mind when using singletons.

What are singletons in Java?

In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.


2 Answers

These are very different attributes, and you can have one or the other independently. All of these are valid:

@Provides User currentUser(...) {} @Provides static User currentUser(...) {} @Provides @Singleton User currentUser(...) {} @Provides @Singleton static User currentUser(...) {} 

To set the stage, a @Provides User method says "for this Component or its dependencies, call this @Provides method every time you need a User". Typically the method will return a new instance every time, and Dagger won't save or cache the instance.

@Singleton is an example of a scope, which is a fancy way to say lifecycle policy or policy for how often to create a new instance. @Provides @Singleton User says "for this Component or dependencies, just call this @Provides method once, and save the result". @Singleton happens to be a built-in common case, but you could also imagine creating a @UserScope (always return the same instance for this User), or in Android a @FragmentScope or @ActivityScope.

For your specific case, you probably don't want @Singleton, because it would instruct your component to save or cache the value from AuthManager. If the User value may change across your application's lifetime, the Component wouldn't reflect that. (In that case you would also want to make sure to inject Provider<User>, which would update, rather than User which would not.)

Leaving scopes behind for a moment, static behaves exactly the way you would expect it to in Java: If a method doesn't require any instance state, you can make it static, and your virtual machine can call it without preparing any instance state. In your generated Component implementation, Dagger will automatically call static methods statically, and instance methods on the Module instance you pass into your Component; in Android this results in a sizable performance increase. Because you don't use any instance state in your currentUser method, it can easily be made static.

Further reading:

  • SO: Scopes in Dagger 2
  • Dagger docs: Component (see heading "Scope")
like image 180
Jeff Bowman Avatar answered Oct 30 '22 18:10

Jeff Bowman


With @Singleton annotation only one instance of User object will be created throughout the application lifecycle.

static on @Provides methods introduced recently to make the invocation of method faster by 15 to 20% as mentioned here. There will be multiple instances of User object if we call this method multiple times.

like image 32
shekar Avatar answered Oct 30 '22 19:10

shekar