I've set up a pluggable system that can retrieve numeric values from the system environment, which have no equivalent setters.
For example:
This interface specifies the contract of the retriever
public interface ValueRetriever
{
double retrieveValue();
}
Here are two implementations:
public class TotalMemoryRetriever implements ValueRetriever
{
@Override
public double retrieveValue()
{
return Runtime.getRuntime().totalMemory();
}
}
public class FreeMemoryRetriever implements ValueRetriever
{
@Override
public double retrieveValue()
{
return Runtime.getRuntime().freeMemory();
}
}
Other possibilities are to return system load, free disk space, etc.
I have built another layer which computes moving averages over different spans of time for the retrieved values, which is why it's useful to have an interface for the retrieval. That lets me use the moving averages mechanism with many "retriever" implementations by "plugging in" each specific retriever.
I have chosen the term "retriever" for this, but my sense is there may be a better term or a better way to do this. Is there a best practice or other convention I can use here?
You could name the classes TotalMemorySensor
and FreeMemorySensor
and make them both implement a MemorySensor
interface.
I'm not sure if the same interface should be used for RAM, CPU, disk, and other statistics. What does it even mean to retrieve a double
? Is it KB, MB, GB? It feels like DiskSensor
should have long totalSpaceInBytes()
while the CpuSensor
should have double loadAverage(TimeUnit)
.
A ValueRetriever
can mean anything and we already have a DoubleSupplier
.