Swing and ‘prefer delegation over inheritance’

H

Hendrik Maryns

Reading books about software engineering and design patterns, I adopted
the axiom ‘prefer delegation over inheritance’. However, I see that the
Swing tutorials recommend to inherit from JFrame. The examples do it as
well, and in fact, a lot of apps I see do this.

Is there a reason for this, or is this just old-fashioned thinking? Am
I missing something? My app which *has* a JFrame works just fine.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFH+iYee+7xMGD3itQRAqfIAJ0UMl+8WXh5UwBWuni1V/BMrYVk2QCfa9ju
kkZ71dIDFYxB9g89TspVrkA=
=4fIe
-----END PGP SIGNATURE-----
 
R

RedGrittyBrick

Hendrik said:
Reading books about software engineering and design patterns, I adopted
the axiom ‘prefer delegation over inheritance’. However, I see that the
Swing tutorials recommend to inherit from JFrame.

Are you sure? where?

http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html
makes no such recommendation that I can see, the example has the class
contain a JFrame rather than extend it.

The examples do it as well,

A lot of Sun's examples which I recall from when learning Swing, used a
CreateAndShowGUI() method which instantiates a JFrame directly.

and in fact, a lot of apps I see do this.

Is there a reason for this, or is this just old-fashioned thinking? Am
I missing something?

I guess it is mostly old-fashioned thinking, some of the examples must
have been around a long time, with only minor changes. I expect some
examples, when illustrating some subject other than JFrame usage, extend
JFrame merely to reduce the linecount?

My understanding of the collected wisdom here is: just because two
JFrames contain different collections of widgets doesn't make them
different types of JFrame - at least not to the extent that you'd be
justified in subclassing JFrame.

My app which *has* a JFrame works just fine.

All mine do too nowadays.
 
A

Arne Vajhøj

Hendrik said:
Reading books about software engineering and design patterns, I adopted
the axiom ‘prefer delegation over inheritance’. However, I see that the
Swing tutorials recommend to inherit from JFrame. The examples do it as
well, and in fact, a lot of apps I see do this.

Is there a reason for this, or is this just old-fashioned thinking? Am
I missing something? My app which *has* a JFrame works just fine.

As with so many other patterns, then you should only apply it when
appropriate.

I don't think JFrame is an obvious fit for that pattern.

Both extending and composing JFrame are frequently used.

And frequently discussed.

See http://forums.java.net/jive/message.jspa?messageID=224388 as
an example.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top