On Sat, Jan 8, 2011 at 7:30 PM, Niclas Hedhman <nic...@hedhman.org> wrote:

> On Sun, Jan 9, 2011 at 9:01 AM, Greg Brown <gk_br...@verizon.net> wrote:
>
> >> This means just dropping 'final' from a few classes in wtk, and
> providing
> >> sample Java codes in a separate tutorial(sample) for pure Java developer
> >> using the builder styles GUI construction so that users can easily copy
> and
> >> pastes from it.
> >
> > Agreed on dropping the final modifier. If you are willing to volunteer
> the time to create such examples, that would be great.
>
> Designing for inheritance is tricky for future compatibility, and
> plainly dropping final is IMHO not a good approach. Let usecase drive
> the need, and seek delegation/behavior patterns instead, which will be
> much more robust in the long run.
>

I don't understand most of part what you wrote. what is future compatibility
means? what is the delegation/behavior patterns?
Can you explain in more detail?

I agree this kind of anonymous instance creation is a bit tricky approach
since this is used to initialize the object, creating new anomynous instance
is sort of side effect, not actually wanted result(but some case, we
actually want to make specialized class(or anonymous class) under given
Pivot class. In JVM level, these two cases are not distinguishable.)

There are a few issue I aware of using anonymous class in the proposed
builder style.
1) for instance, if A is serializable class, and if we create new A{{...}},
then this instance is no longer serializable. So if you want to ship this
through Java serialization, it fails. But in the Pivot case, most of GUI
class is not serializable, so this restriction will not be an issue. Also
conceptually, it is not natural to send GUI object through network.

2) performance: It looks the performance will be slower if we use anonymous
class. So I created benchmark test as shown below. It is a bit surplizing
that the performance is almost the same. I executed this by commenting out
one of the case to avoid interference.

     /*
    loop[100]  anonymous instance creation, time: 77
    loop[1000]  anonymous instance creation, time: 0
    loop[10000]  anonymous instance creation, time: 4
    loop[100000]  anonymous instance creation, time: 7
    loop[1000000]  anonymous instance creation, time: 16
    loop[10000000]  anonymous instance creation, time: 107
    loop[100000000]  anonymous instance creation, time: 917
    loop[1000000000]  anonymous instance creation, time: 7661

    -------------
loop[100]  normal instance creation, time: 77
loop[1000]  normal instance creation, time: 0
loop[10000]  normal instance creation, time: 4
loop[100000]  normal instance creation, time: 7
loop[1000000]  normal instance creation, time: 17
loop[10000000]  normal instance creation, time: 112
loop[100000000]  normal instance creation, time: 926
loop[1000000000]  normal instance creation, time: 7688

     */
public static void main(String[] args) {
long loop = 10;
for (int j = 1; j <= 8; j++) {
loop = loop * 10;
//System.out.println("compare performance difference, loop: " + loop);
// execute only one of the case.
/*
{
long time0 = System.currentTimeMillis();
for (long i = 0; i < loop; i++) {
new OlympicStanding() {
{
setNation("China");
setGold(51);
setSilver(21);
setBronze(28);
}
};
}
long time1 = System.currentTimeMillis();
System.out.println("loop["+loop+"]  anonymous instance creation, time: "+
(time1 - time0));
}
*/
{
long time0 = System.currentTimeMillis();
for (long i = 0; i < loop; i++) {
OlympicStanding os = new OlympicStanding();
os.setNation("China");
os.setGold(51);
os.setSilver(21);
os.setBronze(28);
}
long time1 = System.currentTimeMillis();
System.out.println("loop["+loop+"]  normal instance creation, time: "+
(time1 - time0));
}
 }
}

3) conceptually, if we only want to modify instance initialization part, the
most natural approach is to pass closure(function) to the constructor. In
other language like Scala, there is a special syntax to allow passing
closure by just concatenating closure block(e.g, h {...} <=>  h({..}) )
But in Java, there is no such light weight syntax to apply closure(no
closure still in Java 7..).
So as far as I know, this special anonymous style is the only way to achieve
this 'illusion' to pass closure.(in fact, we can write any statement in the
{{..}} block, this generality is important.)

For "future compatibility", I don't understand what you means by this, but
most of Pivot class is already non final, there are only quite a few classes
which are defined using final. So just changing these classes will not
change the compatibility much.

This builder style is not enforced to users, if they want, they can use it.
but using final class creates some ugly spots for users who want to take
this style.
My impression for using final is just performance consideration. But for
GUI, there would be no noticeable performance difference between  final and
none final classes.



>
> My 0.2 yuan from the rice gallery
>
>
> Cheers
> --
> Niclas Hedhman, Software Developer
> http://www.qi4j.org - New Energy for Java
>
> I live here; http://tinyurl.com/3xugrbk
> I work here; http://tinyurl.com/24svnvk
> I relax here; http://tinyurl.com/2cgsug
>



-- 
Cheers,
calathus

Reply via email to