180

I have a generic interface

public interface Consumer<E> {
    public void consume(E e);
}

I have a class that consumes two types of objects, so I would like to do something like:

public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple>
{
   public void consume(Tomato t) {  .....  }
   public void consume(Apple a) { ...... }
}

Apparently I can't do that.

I can of course implement the dispatch myself, e.g.

public class TwoTypesConsumer implements Consumer<Object> {
   public void consume(Object o) {
      if (o instanceof Tomato) { ..... }
      else if (o instanceof Apple) { ..... }
      else { throw new IllegalArgumentException(...) }
   }
}

But I am looking for the compile-time type-checking and dispatching solution that generics provide.

The best solution I can think of is to define separate interfaces, e.g.

public interface AppleConsumer {
   public void consume(Apple a);
}

Functionally, this solution is OK, I think. It's just verbose and ugly.

Any ideas?

3
  • Why do you need two generic interfaces of the same basetype?
    – akarnokd
    Aug 19, 2009 at 8:16
  • 6
    Due to type erasure you can't do that. Keep it two different classes that implements consumer. Makes more small classes but keeps your code generic (Don't use the accepted answer, it breaks the whole concept... you can't treat the TwoTypesConsumer as a consumer, which is BAD). Nov 8, 2013 at 16:40
  • Check this for functional style impl - stackoverflow.com/a/60466413/4121845
    – mano_ksp
    Mar 6, 2020 at 8:53

9 Answers 9

80

Consider encapsulation:

public class TwoTypesConsumer {
    private TomatoConsumer tomatoConsumer = new TomatoConsumer();
    private AppleConsumer appleConsumer = new AppleConsumer();

    public void consume(Tomato t) { 
        tomatoConsumer.consume(t);
    }

    public void consume(Apple a) { 
        appleConsumer.consume(a);
    }

    public static class TomatoConsumer implements Consumer<Tomato> {
        public void consume(Tomato t) {  .....  }
    }

    public static class AppleConsumer implements Consumer<Apple> {
        public void consume(Apple a) {  .....  }
    }
}

If creating these static inner classes bothers you, you can use anonymous classes:

public class TwoTypesConsumer {
    private Consumer<Tomato> tomatoConsumer = new Consumer<Tomato>() {
        public void consume(Tomato t) {
        }
    };

    private Consumer<Apple> appleConsumer = new Consumer<Apple>() {
        public void consume(Apple a) {
        }
    };

    public void consume(Tomato t) {
        tomatoConsumer.consume(t);
    }

    public void consume(Apple a) {
        appleConsumer.consume(a);
    }
}
6
  • 2
    somehow that looks tome like code duplication... I encountered the same problem and found no other solution that looks clean.
    – grackkle
    Jun 9, 2011 at 19:42
  • 125
    But TwoTypesConsumer fulfills no contracts, so what's the point? It can't be passed to a method that wants either type of Consumer. The whole idea of a two-type consumer would be that you can give it to a method that wants a tomato consumer as well as a method that wants an apple consumer. Here we have neither. Aug 11, 2012 at 3:31
  • @JeffAxelrod I would make the inner classes non-static so they have access to the enclosing TwoTypesConsumer instance if necessary, and then you can pass twoTypesConsumer.getAppleConsumer() to a method that wants an apple consumer. Another option would be to add methods similar to addConsumer(Producer<Apple> producer) to TwoTypesConsumer.
    – herman
    Sep 27, 2012 at 11:04
  • This doesn't work if you don't have control over the interface (e.g. cxf/rs ExceptionMapper) ... Nov 12, 2013 at 9:27
  • 29
    I'll say it: This is a flaw with Java. There's absolutely no reason we shouldn't be allowed to have multiple implementations of the same interface, provided that the implementations take different arguments.
    – birgersp
    May 8, 2017 at 11:24
44

Because of type erasure you can't implement the same interface twice (with different type parameters).

2
  • 6
    I can see how it's a problem... the question is then what's the best (most efficient, safe, elegant) way to bypass this problem.
    – daphshez
    Aug 19, 2009 at 8:59
  • 2
    Without going into the business logic, something here 'smells' like the Visitor pattern. Aug 19, 2009 at 9:27
12

Here's a possible solution based on Steve McLeod's one:

public class TwoTypesConsumer {
    public void consumeTomato(Tomato t) {...}
    public void consumeApple(Apple a) {...}

    public Consumer<Tomato> getTomatoConsumer() {
        return new Consumer<Tomato>() {
            public void consume(Tomato t) {
                consumeTomato(t);
            }
        }
    }

    public Consumer<Apple> getAppleConsumer() {
        return new Consumer<Apple>() {
            public void consume(Apple a) {
                consumeApple(t);
            }
        }
    }
}

The implicit requirement of the question was Consumer<Tomato> and Consumer<Apple> objects that share state. The need for Consumer<Tomato>, Consumer<Apple> objects comes from other methods that expect these as parameters. I need one class the implement them both in order to share state.

Steve's idea was to use two inner classes, each implementing a different generic type.

This version adds getters for the objects that implement the Consumer interface, which can then be passed to other methods expecting them.

1
  • 2
    If anyone uses this: it's worth storing the Consumer<*> instances in instance fields if get*Consumer is called often.
    – TWiStErRob
    Apr 24, 2015 at 10:44
7

At least, you can make a small improvement to your implementation of dispatch by doing something like the following:

public class TwoTypesConsumer implements Consumer<Fruit> {

Fruit being an ancestor of Tomato and Apple.

6
  • 16
    Thanks, but whatever the pros says, I don't regard Tomato as fruit. Unfortunately there's no common base class other than Object.
    – daphshez
    Aug 19, 2009 at 7:46
  • 2
    You can always create a base class called: AppleOrTomato ;) Aug 19, 2009 at 8:24
  • 1
    Better, add a Fruit that delegates to either Apple or Tomato. Aug 19, 2009 at 16:40
  • @Tom: Unless i'm misunderstanding what you're saying, your suggestion only pushes the problem forward, since, for Fruit to be able to delegate to either Apple or Tomato, Fruit must have a field of a superclass to both Apple and Tomato referring to the object it delegates to.
    – Buhb
    Aug 19, 2009 at 18:03
  • 1
    This would imply that TwoTypesConsumer can consume any type of Fruit, any currently implemented and any someone may implement in the future.
    – Tom Gillen
    Mar 5, 2012 at 11:07
3

just Stumbled upon this. It just happened, that I had the same Problem, but I solved it in a different way: I just created a new Interface like this

public interface TwoTypesConsumer<A,B> extends Consumer<A>{
    public void consume(B b);
}

unfortunately, this is considered as Consumer<A> and NOT as Consumer<B> against all Logic. So you have to create a small Adapter for the second consumer like this inside your class

public class ConsumeHandler implements TwoTypeConsumer<A,B>{

    private final Consumer<B> consumerAdapter = new Consumer<B>(){
        public void consume(B b){
            ConsumeHandler.this.consume(B b);
        }
    };

    public void consume(A a){ //...
    }
    public void conusme(B b){ //...
    }
}

if a Consumer<A> is needed, you can simply pass this, and if Consumer<B> is needed just pass consumerAdapter

1
  • 1
    Daphna's answer is the same, but cleaner and less convoluted.
    – TWiStErRob
    Apr 24, 2015 at 10:42
3

In Functional style it is quite easy do this without implementing the interface and also it does the compile time type checking.

Our functional interface to consume entity

@FunctionalInterface
public interface Consumer<E> { 
     void consume(E e); 
}

our manager to process and consume entity appropriately

public class Manager {
    public <E> void process(Consumer<E> consumer, E entity) {
        consumer.consume(entity);
    }

    public void consume(Tomato t) {
        // Consume Tomato
    }

    public void consume(Apple a) {
        // Consume Apple
    }

    public void test() {
        process(this::consume, new Tomato());
        process(this::consume, new Apple());
    }
}
2

You cannot do this directly in one class as the class definition below cannot be compiled due to erasure of generic types and duplicate interface declaration.

class TwoTypesConsumer implements Consumer<Apple>, Consumer<Tomato> { 
 // cannot compile
 ...
}

Any other solution for packing the same consume operations in one class requires to define your class as:

class TwoTypesConsumer { ... }

which is pointless as you need to repeat/duplicate the definition of both operations and they won't be referenced from interface. IMHO doing this is a bad small and code duplication which I'm trying to avoid.

This might be an indicator also that there is too much responsibility in one class to consume 2 different objects (if they aren't coupled).

However what I'm doing and what you can do is to add explicit factory object to create connected consumers in the following way:

interface ConsumerFactory {
     Consumer<Apple> createAppleConsumer();
     Consumer<Tomato> createTomatoConsumer();
}

If in reality those types are really coupled (related) then I would recommend to create an implementation in such way:

class TwoTypesConsumerFactory {

    // shared objects goes here

    private class TomatoConsumer implements Consumer<Tomato> {
        public void consume(Tomato tomato) {
            // you can access shared objects here
        }
    }

    private class AppleConsumer implements Consumer<Apple> {
        public void consume(Apple apple) {
            // you can access shared objects here
        }
    }


    // It is really important to return generic Consumer<Apple> here
    // instead of AppleConsumer. The classes should be rather private.
    public Consumer<Apple> createAppleConsumer() {
        return new AppleConsumer();
    }

    // ...and the same here
    public Consumer<Tomato> createTomatoConsumer() {
        return new TomatoConsumer();
    }
}

The advantage is that the factory class knows both implementations, there is a shared state (if needed) and you can return more coupled consumers if needed. There is no repeating consume method declaration which aren't derived from interface.

Please note that each consumer might be independent (still private) class if they aren't completely related.

The downside of that solution is a higher class complexity (even if this can be a one java file) and to access consume method you need one more call so instead of:

twoTypesConsumer.consume(apple)
twoTypesConsumer.consume(tomato)

you have:

twoTypesConsumerFactory.createAppleConsumer().consume(apple);
twoTypesConsumerFactory.createTomatoConsumer().consume(tomato);

To summarize you can define 2 generic consumers in one top-level class using 2 inner classes but in case of calling you need to get first a reference to appropriate implementing consumer as this cannot be simply one consumer object.

0

Another alternative to avoid the use of more classes. (example using java8+)

// Mappable.java
public interface Mappable<M> {
    M mapTo(M mappableEntity);
}

// TwoMappables.java
public interface TwoMappables {
    default Mappable<A> mapableA() {
         return new MappableA();
    }

    default Mappable<B> mapableB() {
         return new MappableB();
    }

    class MappableA implements Mappable<A> {}
    class MappableB implements Mappable<B> {}
}

// Something.java
public class Something implements TwoMappables {
    // ... business logic ...
    mapableA().mapTo(A);
    mapableB().mapTo(B);
}
0

Sorry for answer old questions, but I really love it! Try this option:

public class MegaConsumer implements Consumer<Object> {

  Map<Class, Consumer> consumersMap = new HashMap<>();
  Consumer<Object> baseConsumer = getConsumerFor(Object.class);

  public static void main(String[] args) {
    MegaConsumer megaConsumer = new MegaConsumer();
    
    //You can load your customed consumers
    megaConsumer.loadConsumerInMapFor(Tomato.class);
    megaConsumer.consumersMap.put(Apple.class, new Consumer<Apple>() {
        @Override
        public void consume(Apple e) {
            System.out.println("I eat an " + e.getClass().getSimpleName());
        }
    });
    
    //You can consume whatever
    megaConsumer.consume(new Tomato());
    megaConsumer.consume(new Apple());
    megaConsumer.consume("Other class");
  }

  @Override
  public void consume(Object e) {
    Consumer consumer = consumersMap.get(e.getClass());
    if(consumer == null) // No custom consumer found
      consumer = baseConsumer;// Consuming with the default Consumer<Object>
    consumer.consume(e);
  }

  private static <T> Consumer<T> getConsumerFor(Class<T> someClass){
    return t -> System.out.println(t.getClass().getSimpleName() + " consumed!");
  }

  private <T> Consumer<T> loadConsumerInMapFor(Class<T> someClass){
    return consumersMap.put(someClass, getConsumerFor(someClass));
  }
}

I think that is what you are looking for.

You get this output:

Tomato consumed!

I eat an Apple

String consumed!

2
  • 1
    In question: "But I am looking for the compile-time type-checking..."
    – æ-ra-code
    Jul 8, 2019 at 14:06
  • @aeracode No options to do what OP wants. Type erasure makes imposible to implements the same interface twice with different type variables. I only try to give you another way. Of course you can check types accepted previously to consume an onbject.
    – Awes0meM4n
    Jul 9, 2019 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.