据我所知,这种方法记忆(缓存)作为参数传递的供应商的价值.据我所知它表现得像单身模式.谁能解释它是如何工作的?
public static <T> Supplier<T> memoize(final Supplier<? extends T> valueSupplier)
{
final List<T> memoryList= new ArrayList<>();
return () -> {
if (memoryList.isEmpty()) {
memoryList.add(valueSupplier.get());
}
return memoryList.get(0);
};
}
Run Code Online (Sandbox Code Playgroud)
用法如下:
Supplier<SomeClass> cachedValue = memoize(() -> someClassObject.getSomeValueToBeCached());
cachedValue.get().doMethod();
Run Code Online (Sandbox Code Playgroud)