Singleton
How to create an object once and use it everywhere?
It’s a singleton class. A singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere.
Java
// Create a Singleton class
public class SingletonService {
// Static instance
private static final SingletonService instance = new SingletonService();
public static SingletonService getInstance() {
return instance;
}
// Private constructor
private SingletonService() {
... do something
}
// Access a property everywhere
private ServiceConnection serviceConnection = null;
public void setServiceConnection(ServiceConnection serviceConnection) {
this.serviceConnection = serviceConnection;
}
public ServiceConnection getServiceConnection() {
return serviceConnection;
}
}
// Create a Singleton class
public class SingletonService {
// Static instance
private static final SingletonService instance = new SingletonService();
public static SingletonService getInstance() {
return instance;
}
// Private constructor
private SingletonService() {
... do something
}
// Access a property everywhere
private ServiceConnection serviceConnection = null;
public void setServiceConnection(ServiceConnection serviceConnection) {
this.serviceConnection = serviceConnection;
}
public ServiceConnection getServiceConnection() {
return serviceConnection;
}
}
// Create a Singleton class public class SingletonService { // Static instance private static final SingletonService instance = new SingletonService(); public static SingletonService getInstance() { return instance; } // Private constructor private SingletonService() { ... do something } // Access a property everywhere private ServiceConnection serviceConnection = null; public void setServiceConnection(ServiceConnection serviceConnection) { this.serviceConnection = serviceConnection; } public ServiceConnection getServiceConnection() { return serviceConnection; } }
// Usage
public class Somewhere {
...
// Retrieve one instance
SingletonService service = SingletonService.getInstance();
// Update property value
service.setServiceConnection(xxxxxx);
...
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
public class MainActivity extends ReactActivity {
...
@Override
protected void onDestroy() {
super.onDestroy();
// Retrieve one instance
SingletonService service = SingletonService.getInstance();
// Retrieve a property and use it
ServiceConnection serviceConnection = service.getServiceConnection();
if (serviceConnection != null) {
unbindService(serviceConnection);
}
}
...
// Usage
public class Somewhere {
...
// Retrieve one instance
SingletonService service = SingletonService.getInstance();
// Update property value
service.setServiceConnection(xxxxxx);
...
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
public class MainActivity extends ReactActivity {
...
@Override
protected void onDestroy() {
super.onDestroy();
// Retrieve one instance
SingletonService service = SingletonService.getInstance();
// Retrieve a property and use it
ServiceConnection serviceConnection = service.getServiceConnection();
if (serviceConnection != null) {
unbindService(serviceConnection);
}
}
...
// Usage public class Somewhere { ... // Retrieve one instance SingletonService service = SingletonService.getInstance(); // Update property value service.setServiceConnection(xxxxxx); ... } //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// public class MainActivity extends ReactActivity { ... @Override protected void onDestroy() { super.onDestroy(); // Retrieve one instance SingletonService service = SingletonService.getInstance(); // Retrieve a property and use it ServiceConnection serviceConnection = service.getServiceConnection(); if (serviceConnection != null) { unbindService(serviceConnection); } } ...
Kotlin
// Create a Singleton class
object SingletonService { // 1. Use object keyword for singleton class
init { // 2. Object doesn't have constructor, use init instead
... do something
}
// 3. Access a property everywhere
var serviceConnection: ServiceConnection? = null
get() = field
set(value) {
field = value
}
}
// Create a Singleton class
object SingletonService { // 1. Use object keyword for singleton class
init { // 2. Object doesn't have constructor, use init instead
... do something
}
// 3. Access a property everywhere
var serviceConnection: ServiceConnection? = null
get() = field
set(value) {
field = value
}
}
// Create a Singleton class object SingletonService { // 1. Use object keyword for singleton class init { // 2. Object doesn't have constructor, use init instead ... do something } // 3. Access a property everywhere var serviceConnection: ServiceConnection? = null get() = field set(value) { field = value } }
// Usage in Kotlin
class Somewhere {
...
// Retrieve a property and SET its value
SingletonService.serviceConnection = xxxxxx
...
// Retrieve a property and use it
SingletonService.serviceConnection?.let {
unbindService(
it
)
}
...
}
// Usage in Kotlin
class Somewhere {
...
// Retrieve a property and SET its value
SingletonService.serviceConnection = xxxxxx
...
// Retrieve a property and use it
SingletonService.serviceConnection?.let {
unbindService(
it
)
}
...
}
// Usage in Kotlin class Somewhere { ... // Retrieve a property and SET its value SingletonService.serviceConnection = xxxxxx ... // Retrieve a property and use it SingletonService.serviceConnection?.let { unbindService( it ) } ... }
// Use Kotlin singleton class in Java
public class MainActivity extends ReactActivity {
...
@Override
protected void onDestroy() {
super.onDestroy();
SingletonService service = SingletonService.INSTANCE;
ServiceConnection serviceConnection = service.getServiceConnection();
if (serviceConnection != null) {
unbindService(serviceConnection);
}
}
...
// Use Kotlin singleton class in Java
public class MainActivity extends ReactActivity {
...
@Override
protected void onDestroy() {
super.onDestroy();
SingletonService service = SingletonService.INSTANCE;
ServiceConnection serviceConnection = service.getServiceConnection();
if (serviceConnection != null) {
unbindService(serviceConnection);
}
}
...
// Use Kotlin singleton class in Java public class MainActivity extends ReactActivity { ... @Override protected void onDestroy() { super.onDestroy(); SingletonService service = SingletonService.INSTANCE; ServiceConnection serviceConnection = service.getServiceConnection(); if (serviceConnection != null) { unbindService(serviceConnection); } } ...
How to access companion object from Java?
Kotlin
class AudioPlayerService {
companion object {
var serviceConnection: ServiceConnection? = null
}
}
class AudioPlayerService {
companion object {
var serviceConnection: ServiceConnection? = null
}
}
class AudioPlayerService { companion object { var serviceConnection: ServiceConnection? = null } }
Usage in Kotlin
// Usage
// Retrieve property
AudioPlayerService.serviceConnection?.let {
// Check if serviceConnection not null, then do something with servicecConnection using "it"
...
}
// Update value to property
AudioPlayerService.serviceConnection = xxxxxxx
// Usage
// Retrieve property
AudioPlayerService.serviceConnection?.let {
// Check if serviceConnection not null, then do something with servicecConnection using "it"
...
}
// Update value to property
AudioPlayerService.serviceConnection = xxxxxxx
// Usage // Retrieve property AudioPlayerService.serviceConnection?.let { // Check if serviceConnection not null, then do something with servicecConnection using "it" ... } // Update value to property AudioPlayerService.serviceConnection = xxxxxxx
Usage in Java
- Add @kotlin.jvm.JvmField into the companion object
class AudioPlayerService {
companion object {
@kotlin.jvm.JvmField // Add this!!
var serviceConnection: ServiceConnection? = null
}
}
class AudioPlayerService {
companion object {
@kotlin.jvm.JvmField // Add this!!
var serviceConnection: ServiceConnection? = null
}
}
class AudioPlayerService { companion object { @kotlin.jvm.JvmField // Add this!! var serviceConnection: ServiceConnection? = null } }
- Then, you can access companion object from Java like this
ServiceConnection serviceConnection = AudioPlayerService.serviceConnection;
// Check if serviceConnection not null, then do something with servicecConnection
if (serviceConnection != null) {
unbindService(serviceConnection);
}
ServiceConnection serviceConnection = AudioPlayerService.serviceConnection;
// Check if serviceConnection not null, then do something with servicecConnection
if (serviceConnection != null) {
unbindService(serviceConnection);
}
ServiceConnection serviceConnection = AudioPlayerService.serviceConnection; // Check if serviceConnection not null, then do something with servicecConnection if (serviceConnection != null) { unbindService(serviceConnection); }
Leave a Reply