I implement an object cache:
void cache(String request_type, String request_id, ProcessingStrategy request_strategy)
{
if (no_state(request_strategy) == true)
map.put(request_type, request_strategy);
else
map.put(request_id, request_strategy);
}
ProcessingStrategy lookup(String request_type, String request_id)
{
ProcessingStrategy request_strategy = map.get(request_type);
if (request_strategy == null) return map.get(request_id)
else return request_strategy;
}
This scheme of sharing objects implementing interface ProcessingStrategyfor all requests of a certain type will work only if the state is not saved in a particular class.
How to write a method no_state?
I think that checking for the presence of all member fields finalusing Reflection will be:
for(Field f : request_strategy.getClass().getDeclaredFields())
if(!Modifier.isFinal(f.getModifiers()))
return false;
source
share