• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with object creator

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to do something like this:













I want extension1 to have Creator<Integer, interfaceB> and extension2 to have Creator<Double, interfaceC>. See the pattern? Creator<var1, var2> where var1 is the type of immediate parent and var 2 is the interface implemented by said class. Is there any way to do this? can anybody tell the code of ObjectCreator.createCreator()?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:I want extension1 to have Creator<Integer, interfaceB> and extension2 to have Creator<Double, interfaceC>. See the pattern? Creator<var1, var2> where var1 is the type of immediate parent and var 2 is the interface implemented by said class. Is there any way to do this? can anybody tell the code of ObjectCreator.createCreator()?


I feel like I've walked into the middle of a discussion here. What is ObjectCreator? And what is ObjectCreator.createCreator() supposed to do? It would appear that you're trying to implement some kind of Factory pattern, but your specifications are very unclear.

If you're trying to create some sort of dynamically-typed "I don't know what object I'm going to need, but I still want to use it" app, Java is almost certainly the wrong choice of language. It could possibly be done through reflection, but the code is likely to be complex, error-prone, arcane and SLOW.

It should also be added that the uses for such code are very rare.

However, if you still feel that you absolutely must do it; and furthermore do it in Java: TellTheDetails (←click).
Also, explain what you're trying to do, not how you want to do it. That, presumably, is what you want advice about.

Right now, I have no idea what you're on about.

Winston
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, perhaps it's called factory, don't really know because I'm not familiar with java pattern and just don't have the time to do it. Maybe that's why my naming convention seems unorthodox. I want to create Creator<J, J1> where J1 is consistent with the name of interface implemented by the calling object. like in the example, extension2 extend base<Double> and implement interface C, therefore Creator object for exetension2 will be Creator<Double, interfaceC>.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:okay, perhaps it's called factory, don't really know because I'm not familiar with java pattern and just don't have the time to do it.


Well my advice, if you're embarking on something as involved as this, would be to make time.

Maybe that's why my naming convention seems unorthodox. I want to create Creator<J, J1> where J1 is consistent with the name of interface implemented by the calling object. like in the example, extension2 extend base<Double> and implement interface C, therefore Creator object for exetension2 will be Creator<Double, interfaceC>.


OK, first off, Java wrappers are final; so whatever your 'base' class is doing (and it should be Base, not base), it will have to be a wrapper to one, because it won't be able to extend it.

But I'm still a bit confused about what you want to do. It would appear that you want some sort of construct like:and the answer is: No, you can't do that generically.

What you can do is create a class (probably abstract) that extends Base and implements the correct interface, eg:and then create classes that simple extend it. And once you've got those set up, you could probably write some sort of "factory" to return them to you generically (though why you'd want to escapes me).

However, as I said before, you need to back up and explain WHAT you're trying to do, rather than this long mess of code, which is the HOW.

So, I'll ask one more time: What is all this stuff intended to achieve? And try to answer without any references to your classes or interfaces.
Your answer should be something like: "I want to create a cookbook."

Winston
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really know how what I want to do is relevant to the solving of my question, but okay, since you're very adamant to know what I want to achieve :
I'm trying to somehow through extension, reduce the amount of code. The Creator object, I want to make it accessible to base's children. but not only that, I want to also preserve the types as you may have seen, I want to preserve the interface implemented and the type of base used for the children. and if you're wondering why the interface is included too? I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:I don't really know how what I want to do is relevant to the solving of my question, but okay, since you're very adamant to know what I want to achieve :
I'm trying to somehow through extension, reduce the amount of code. The Creator object, I want to make it accessible to base's children. but not only that, I want to also preserve the types as you may have seen, I want to preserve the interface implemented and the type of base used for the children. and if you're wondering why the interface is included too? I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.


So basically I was right in my first post: You want to simply create "an Object" on the fly, without knowing what its type is going to be beforehand.

My suggestion: You've picked the wrong tool for the job.

Java is statically typed, which means that the language requires you to specify a type before you (a) create, or (b) use it. It provides some flexibility of use via interfaces, but these are strictly hierarchical in nature and tend to be geared towards specific modes of use.

Now, that said, the language does allow you two possibilities:
1. Reflection - An extra-linguistic set of methods which allow you to create and use objects based on their definition meta-data. It is generally avoided by experts, except in very specific cases (eg, writing IDEs), where introspection really is the only way to go. It's also horribly slow and the code is error-prone and difficult.
2. Compile-on-the-fly + Custom class loading - The idea behind this would be to write the actual Java code required for your 'Creator' class on the fly, compile it in real time, and then load the resulting class with a custom class loader so that it becomes 'recognised'. I've never tried it, so I can't really advise, save to say that the possible problems are LOTS, and the resulting code is, again, going to be complex and arcane.

And you want to do all this just to save your carpals?

Winston
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:I don't really know how what I want to do is relevant to the solving of my question, but okay, since you're very adamant to know what I want to achieve :
I'm trying to somehow through extension, reduce the amount of code. The Creator object, I want to make it accessible to base's children. but not only that, I want to also preserve the types as you may have seen, I want to preserve the interface implemented and the type of base used for the children. and if you're wondering why the interface is included too? I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.




I agree with Winston, I feel like I have walked into the middle of a conversation. I have little to no idea what you are saying or asking. And after this post, I have to admit that I still don't know what you are asking.

I don't know exactly the cause for the confusion, maybe it is because you are assuming a language, terminology, or a train of thought that we don't have. Regardless, can you elaborate a bit?

Winston Gutkowski wrote:

Hendra Kurniawan wrote:I don't really know how what I want to do is relevant to the solving of my question, but okay, since you're very adamant to know what I want to achieve :
I'm trying to somehow through extension, reduce the amount of code. The Creator object, I want to make it accessible to base's children. but not only that, I want to also preserve the types as you may have seen, I want to preserve the interface implemented and the type of base used for the children. and if you're wondering why the interface is included too? I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.


So basically I was right in my first post: You want to simply create "an Object" on the fly, without knowing what its type is going to be beforehand.



Interesting. I didn't even get that from the topic -- even after rereading (okay, maybe I did a bit but that was because I was influenced by your post).

Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:I don't really know how what I want to do is relevant to the solving of my question, but okay, since you're very adamant to know what I want to achieve...


And in answer to this: The reason is that we can't advise you properly unless we know what you want.

Of course, if you simply want a yes/no answer based on your original post:
Question: (paraphrased) Can I do something THIS WAY?
Answer: No.

Winston
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hendra Kurniawan wrote:I don't really know how what I want to do is relevant to the solving of my question


Then why are you doing it? Or why are you asking an irrelevant question then?

I'm trying to somehow through extension, reduce the amount of code... I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.


And what if some day, long after you expended all this effort now, you suddenly realize that you never really needed all this flexibility after all? In the real world, do you buy a bus because you might someday need to take fifty of your friends to the beach? What about a hundred friends? Oh no, now you need two buses! Wait, what about when you get married? You'll need to transport the whole town! Better get going on that fleet of buses. It's just silly, right? Well, it seems to me like you're looking to actually build a bus right now with all this stuff that you're trying to do.

Sometimes, to reduce the amount of code, you just need to code for the here and now. KISS - keep it simple, silly. Think about those other bridges when you get to them, if you ever even get to them.

Or as "Uncle Bob" likes to say: too much flexibility is TECHNICAL DEBT.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

I'm trying to somehow through extension, reduce the amount of code... I just want to cover all the bases. Who might know one day I will need to preserve the interfaces too. So bottom line, I want to write less code.


And what if some day, long after you expended all this effort now, you suddenly realize that you never really needed all this flexibility after all? In the real world, do you buy a bus because you might someday need to take fifty of your friends to the beach? What about a hundred friends? Oh no, now you need two buses! Wait, what about when you get married? You'll need to transport the whole town! Better get going on that fleet of buses. It's just silly, right? Well, it seems to me like you're looking to actually build a bus right now with all this stuff that you're trying to do.

Sometimes, to reduce the amount of code, you just need to code for the here and now. KISS - keep it simple, silly. Think about those other bridges when you get to them, if you ever even get to them.

Or as "Uncle Bob" likes to say: too much flexibility is TECHNICAL DEBT.




Many many years ago, I was part of a code review, where a programmer had a somewhat awkward coding (on hundreds of classes). I think it was something similar to saving on coding (like this topic). It actually got really contentious, because there were lots of other engineers that thought there were many easier ways to do it.

Regardless, the straw that broke the camel's back was the question "how many times have this been needed"? The answer was none. So, the programmer created tons of code (build a framework actually) on hundreds of classes, for something that was never used. At best, it was arguably not an efficient way for doing it (and unproven that it will work). At worst, it was a non-issue, and the effort was a complete waste of time.

Henry
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must admit that I didn't understand the requirements fully. So all of the following might be wrong. You've been warned.

Proper generic typing might be - perhaps - achieved by

Class<U> would then be a generic type of the class which can be created by the Creator interface. Of course, any interface/class naming convention has to be honored by programmers, the compiler cannot enforce that.

There is a compiler bug, though, that can sometimes cause the compiler erroneously recognize perfectly compatible generic types as incompatible. I've lost some hair to it myself. Also, that is in JDK6, some sources indicate it has been fixed in JDK7 (I don't use it and can't therefore verify).

As far as the reflective object creation goes: I do use Class.forName() (where class names go typically form property files or configuration) and someClass.newInstance() from time to time.

If it is actually more complicated that this, I'm with the rest of the band: it's an overkill.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Regardless, the straw that broke the camel's back was the question "how many times have this been needed"? The answer was none. So, the programmer created tons of code (build a framework actually) on hundreds of classes, for something that was never used. At best, it was arguably not an efficient way for doing it (and unproven that it will work). At worst, it was a non-issue, and the effort was a complete waste of time.


And, at the end of the day it sounds like this is an attempt to turn Java into a "dynamic language" simply to save typing.

And, quite apart from what you've said, Java is the wrong tool for the job. Want a language where you can simply write classes on the fly? Use Smalltalk, because there, code is a first-class object. And where is it used professionally? Almost nowhere.

Don't get me wrong; as an R&D tool, I suspect it's very useful; but as a real computer language? Used by people whose job depends on reliability? Don't think so - and that has nothing to do with the reliability of Smalltalk itself either; simply the reliability of the people that want to use its capabilities "to the max".

Winston
 
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my problem: when I see requirements described in terms of interfaces named "InterfaceA" and classes named "ClassB" I find it hard to understand. (And it makes me suspect you don't understand your requirements clearly either.) I always have to see a real-life example of where the requirements would apply. Those silly beginner examples with Animals and Dogs are marginally okay, but what I would really like is a requirement which describes something useful which you are about to start work on.
reply
    Bookmark Topic Watch Topic
  • New Topic