我正在实现一个对象缓存:
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); //no chance of keyspace collision
}
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;
}
这种在特定类型的所有请求中实现的共享对象方案interface ProcessingStrategy
只有在具体类中没有存储状态时才有效。
方法怎么写no_state
?
我正在考虑检查是否所有成员字段都final
使用反射就足够了:
for(Field f : request_strategy.getClass().getDeclaredFields())
if(!Modifier.isFinal(f.getModifiers()))
return false;
//even if all fields are final;
//how to check that they are not being initialized with differently in the ProcessingStrategy constructor?