0

Interface to be implemented:

public interface Message {
}

public interface Handles<T extends Message>  {
    void hanlde(T message);
}

Concrete implementation:

public class EnforcementHandler<T extends Handles<T extends Message>> {

}

Unexpected bound

Is this pattern possible in Java?

Yusbel Garcia
  • 177
  • 1
  • 11
  • `public class EnforcementHandler & Message> {` if you want `T` to implement `Message` and `Handles`. – Turing85 Aug 07 '20 at 21:40
  • Hi @Turing85. No offense. What you're proposing won't work. The OP's _`Handles`_ is declared with a type parameter that has only one bound: _`Message`_. You're declaring _`EnforcementHandler`_ with a type parameter _`T`_ that has two bounds: _`Handles`_ & _`Message`_. It would be impossible to actually instantiate an instance of _`EnforcementHandler`_ with a type argument of some type that both implements _`Handles`_ and also implements _`Message`_ — *as you've defined with* _`T extends Handles & Message`_. [*See for yourself with an experiment I did*](https://www.browxy.com#USER_306203). – deduper Aug 08 '20 at 18:38
  • @deduper in the comment I provided, the concrete implementation of `T` (i.e. `HandlesMessage` in your code) must implement both `Handle` **and** `Message`. Although this approach is highly questionable since those two implementations should be separated in two classes (since it basically says that `HandlesMessage` handles itself...), it is what I inferred OP wanted. I do not recommend using this approach, but [it would work in theory](https://ideone.com/lHtfwt). Since I do not recomment the approauch and @Louis's answer was already there (and cleaner), I just posted it as comment. – Turing85 Aug 08 '20 at 19:05
  • @deduper thank you for your answer. I'm building a basic composite pattern using functions that follow a custom interface. In this case the custom interface could be a Consumer but the answer Louis provided was what I was looking to do. – Yusbel Garcia Aug 08 '20 at 19:18
  • „*...it basically says that HandlesMessage handles itself..it is what I inferred OP wanted...*“ — Would you mind if I *ask you for the specific use case* you envisaged when you were inferring such an arrangement, @Turing85? That would be very educational. TIA. – deduper Aug 08 '20 at 19:29
  • @deduper As I said: I would not recommend this approach and would strongly suggest [Louis's solution](https://stackoverflow.com/a/63309455/4216641). – Turing85 Aug 08 '20 at 19:30
  • Thanks @YusbelGarcia for providing such a good problem for my daily coding kata. – deduper Aug 08 '20 at 19:31
  • public static > void enforcement(M message, Handles handler){ //Todo: Apply security handler.hanlde(message); } – Yusbel Garcia Aug 08 '20 at 19:36
  • you can use it to compose operations, like in OOP pipe and filters – Yusbel Garcia Aug 08 '20 at 19:38
  • „*...I would not recommend this approach...*“ — Hmmm. By posting the approach in your comment @Turing85, you *did* actually recommend that approach. So I'm hoping to learn something that *I* don't know, but that you *do* know. Namely: *The specific use case you envisaged when you commented?* – deduper Aug 08 '20 at 19:41
  • @deduper It is syntactically possible, bot semantically questionable. If you want to continue the discussion (although I am afraid I have no further information to provide) then invite me to a chat. Comments are not for extended discussions. – Turing85 Aug 08 '20 at 19:43

2 Answers2

4

You must write it as EnforcementHandler<M extends Message, T extends Handles<M>>.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

What the exact responsibility of your EnforcementHandler is, is ambiguous.

Going by the „Handler“ part of its name, suggests to me that its responsibility is to „handle“ things. So this seems an appropriate implementation to my understanding…

public class EnforcementHandler<M extends Message> implements Handler<M>{ 
    
    public void handle( M msg ) { out.printf( "%1$70s%n ", msg.read( ) ); }
}

This demo shows how it could be used

  ...
  Message msg = ( ) -> "Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.";
  
  EnforcementHandler< Message > handler = new EnforcementHandler< >( );
  
  handler.handle( msg );
  ...

…Printing this friendly message to stdout

Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.
deduper
  • 1,944
  • 9
  • 22