aspose file tools*
The moose likes Java in General and the fly likes Reflection, Inheritance and Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection, Inheritance and Generics" Watch "Reflection, Inheritance and Generics" New topic
Author

Reflection, Inheritance and Generics

Martin Torre Castro
Greenhorn

Joined: Aug 07, 2012
Posts: 6

Hello,

I'm making a hierarchy of classes for common dialogs at an application.

It comes for dialogs to be frequent having some kind of table so I want to make a class GenericConfirmDialog.

And then I want to make inside a method which returns the appropiate SGATableModel (there are many in our application), which is
filled with <E> objects, so my proposal is this one:



I can't modify SGATableModel because it's third party code, but all the children from SGATableModel are modifiable.

When coming to generics and wildcards I have many doubts about how to get the .class from the wildcard and how to manage the references to constructors, classes, etc
I would be grateful if someone could tell me a more suitable way of doing this, or passing me an URL where I can read about this (I haven't found any with enough detail).

Thank you very much.




Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Martin Torre Castro wrote:I'm making a hierarchy of classes for common dialogs at an application.

It comes for dialogs to be frequent having some kind of table so I want to make a class GenericConfirmDialog.

And then I want to make inside a method which returns the appropiate SGATableModel (there are many in our application), which is
filled with <E> objects, so my proposal is this one:

It seems excessively complicated to me.

First: Don't use reflection unless you really have no other choice. It's error-prone, difficult to write properly, and SLOW.

Second: The place to put reflection if you really think you need it (although I suspect you probably don't) would be in a SGATableModelFactory class. I'd suggest you look at the Factory pattern.

Perhaps even better would be to add Factory methods to your SGATableModel interface (and if you don't have one; make one). Then your dialog class could be as simple as:(I left the FP parameter out because I don't understand what it's doing).

Reflection is highly specialized, and is rarely the right choice for anything except classes that truly require introspection.

I suspect also that you might make life simpler for yourself if you used composition rather than class hierarchies; but without more information it's difficult to tell.

HIH

Winston

[Edit] PS: I broke up that very long line in your code. Please re-read the UseCodeTags page thoroughly. Thanks.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Martin Torre Castro
Greenhorn

Joined: Aug 07, 2012
Posts: 6

I forgot to comment. I can't edit the SGATableModel class, because it's a third party code. (I'm editing now the first post for adding this).


So I can't make any composition inside SGATableModel or any interface for the SGATableModel to accomplish.
I could make some kind of Factory, but it would be outside the package.


But you gave me an idea. I'm thinking of making a Factory Method in the GenericDialog superClass



But I still need some way of getting the .class of the <E> type when calling the Factory method. As you see, you always make an XSGATableModel for <X> objects.
The method factory needs to know the parameter type of the <E> in some way.

Note: The FP has no point to do with all this. Be calm about that.

Thank you in advance.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Martin Torre Castro wrote:But you gave me an idea. I'm thinking of making a Factory Method in the GenericDialog superClass...

Again, it just doesn't sound right to me. My suggestion would be to create a separate Factory class and then use it inside your GenericDialog.

But I still need some way of getting the .class of the <E> type when calling the Factory method. As you see, you always make an XSGATableModel for <X> objects.
The method factory needs to know the parameter type of the <E> in some way.

Well, that's entirely dependent on whether your SGATableModel classes have generic type parameters or not. If not, shoot your 3rd party; if they do, you could probably create a factory with your dispatch style code from above, viz:and your Dialog might then look like:and there may well be better ways.

However, there is yet another possible alternative:
Are those classes that you supply (Facility, BlockLength, ArticleCrossDock...) also 3rd party classes, or your own?
If the latter, you could add the factory method to them.

There are probably tons of ways of doing what you want, but I repeat: Avoid reflection if you possibly can.
I suspect you could also do it without dispatch code, but it might well mean defining some new wrapper classes for your 3rd party SGATableModels.

Winston
Martin Torre Castro
Greenhorn

Joined: Aug 07, 2012
Posts: 6

I should shoot my 3rd party then and kill them all. The libraries and packages containing SGATableModel can't be touch and the class it's not made with the generic parameter type. The classes (Facility, BlockLength, ArticleCrossDock) managed for filling the table models are not modifiable as well, because they are used by another applications out of our control.

I guess the best way would be making the factory class you suggested and creating inside the method which returns XXXSGATableModels depending of the .class received as parameter. That would not need reflective construction because it's only a mapping between the BaseObjects and the XXXSGATableModels, just as in the if-elseif I wrote upwards. Another advantage is no duplicating code, because this models are also used in the main windows, not only at Dialogs.


I will keep trying on my own looking for better design. Thank you for your help.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Martin Torre Castro wrote:I will keep trying on my own looking for better design. Thank you for your help.

You're welcome. Best of luck.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reflection, Inheritance and Generics