4

I have the following interface:

public enum AggregatorType{
    DATA_BASE,
    GLOBAL_CACHE,
    //etc
}

public interface DataAggregator<T>{

     public AggregatorType getType();

     public Collection<T> getData();

}

My question is about putting different types in the same enumeration. To me, it smells like not a good thing at least (because two separate type put in the same place). But I can't see what potential problems it can lead to. Couldn't you help me understand?

STaefi
  • 3,800
  • 1
  • 22
  • 39
St.Antario
  • 23,179
  • 26
  • 96
  • 243
  • What benefit would this have over `instanceof`? – Tobia Tesan Jul 27 '15 at 08:04
  • @TobiaTesan No, instanceof is not a solution because I don't know the actual type of the Aggregator. – St.Antario Jul 27 '15 at 08:05
  • 2
    I'm kind of confused. Would you please describe your use case for this, maybe with an example? I strongly suspect you don't *need* it. Also [this](http://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) might be of help :) – Tobia Tesan Jul 27 '15 at 08:10
  • Type enums for objects in OOP normally smell bad; mostly this will better be solve by inheritation or common interfaces. – Uwe Allner Jul 27 '15 at 08:14
  • @TobiaTesan This is only for splitting different aggregators into the different groups. The aggregators return data and then I'll find their intersection. – St.Antario Jul 27 '15 at 08:37

1 Answers1

3

Typically using enum types is not a good practice to explicitly define the type of a class because in some other part of your code you want must have a control structure (if-else/switch-case) on this type to separate the way of aggregation.

If I were you I would put an aggregate method in this interface and I would let the Polymorphism mechanism do the magic for me. DataBaseAggregator and CacheAggregator can be to implementation of this interface to define different behaviors in the aggregation of those data.

STaefi
  • 3,800
  • 1
  • 22
  • 39