You should not have to explicitly test the dynamic type of a factory method's return value. The static type should tell you all you need to know. That means the return type should be as specific as it needs to be to tell you what you can do with the object. For example, a factory method that makes maps of varying implementation should return Map
:
public interface MapFactory {
public Map makeMap();
...
}
Whether the Map
is a HashMap
or TreeMap
or ConcurrentSkipListMap
, you can use the Map
methods to interact with it. If it turns out you need to call ceilingKey
, which isn't a Map
method, you have a design problem.
In your case, your factory methods make no sense, since there is no more specific type to return than Object
, and nothing you can do with the return values beyond the API of Object
. A more reasonable factory would return objects that can be interacted with the same way, regardless of how the objects are implemented.