• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection: Instantiate Interface Implementation  RSS feed

 
Henry Lowell
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have the following:





Ok, so what I need to do is the following: Using reflection, I need to instantiate ThatInterface with an instance of ThisAndThat class. I can instantiate ThisAndThat with no problem.
 
Clifton Craig
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Lowell:
Say I have the following:





Ok, so what I need to do is the following: Using reflection, I need to instantiate ThatInterface with an instance of ThisAndThat class. I can instantiate ThisAndThat with no problem.


Henry,

You solved your own problem in the text of your post. just take the code that instantiates ThisAndThat and move it into a method that returns ThatInterface. You're probably confused in thinking you need to instantiate ThatInterface which is not possible. The whole idea is to have the caller use the ThatInterface as the type and then the code you have to instantiate ThisAndThat can be moved into a factory method that returns ThisInterface.

In my above example all you would need to do is inject TheOtherThing with ThatFactory using an assembler like Spring or just plain ol' wonderful Java.

(Man I love the way that codes out! Good example Henry!)
 
Henry Lowell
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I find of figured that out just before I got your response. Everything works great. Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!