Skip to content

Latest commit

 

History

History
65 lines (53 loc) · 1.07 KB

singleton.md

File metadata and controls

65 lines (53 loc) · 1.07 KB

Singleton

The objects can be marked as singleton to ensure that only one instance is created.

Note: The singleton is factory wide - so each factory instance has its own set of singletons.

The singleton can be created multiple ways:

  • Using the @Singleton annotation on
    • Either interface
    • Or implementation
  • Forcing it on the binding definition

Note: The single instance will be ensured if at least one method (above) is used for that binding.

Annotating interface

@Singleton
interface User
{
    String getName();
}
class UserImpl implements User
{
    @Override
    String getName() {
        return "John Doe";
    }
}

Annotating implementation

interface User
{
    String getName();
}
@Singleton
class UserImpl implements User
{
    @Override
    String getName() {
        return "John Doe";
    }
}

On binding

class MyModule extends Module
{
    @Override
    protected void defineBindings() {
        whenRequestedInstanceOf(User.class).singleton().thenInstantiate(UserImpl.class);
    }
}