Вот как это выглядит.
Код: Выделить всё
// Immutable, hence, thread-safe
class MyClass {
// Returns a new instance.
public static MyClass newInstance() {
// Loads resources, and constructs a new object
}
private MyClass() {
}
}
Код: Выделить всё
private static volatile SoftReference INSTANCE;
// multiple instances may be created,
// in a timing manner,
// which is ok
private static MyClass getInstance() {
var result = INSTANCE;
if (result == null || result.get() == null) {
result = INSTANCE = new SoftReference(newInstance());
}
return result.get();
}
public static R applyInstance(
final Function
Подробнее здесь: [url]https://stackoverflow.com/questions/79789816/how-can-i-cache-a-single-object[/url]
Мобильная версия