aspose file tools*
The moose likes Java in General and the fly likes problem with object creator 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 "problem with object creator" Watch "problem with object creator" New topic
Author

problem with object creator

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
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()?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Jan 31, 2011
Posts: 239
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with object creator