Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Gumbie: A GUI Generator for Jython


Apr02: A Factory with a Twist

A Factory with a Twist

Gumbie uses the factory pattern as described by the Gang of Four (Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma et al., Addison-Wesley 1994; ISBN 0-201-63361-2) — but with one crucial twist. Ordinarily, an object receives its factory as a parameter of its constructor. Gumbie, on the other hand, keeps its factory in a module called "Config." This module is initially empty, and the module that chooses the factory imports Config and assigns the factory to Config as a module variable, Config.factory. The other modules of Gumbie import Config and work with Config.factory.

I've done this for two reasons: First, once the factory has been placed in Config.factory, it is easily accessible for all modules that may need it, which is simpler than passing the factory as a parameter all the time. More importantly, the class MenuMaker is meant to be mixed with other classes (frames such as java.awt.Frame in particular), and the factory provides the classes to be mixed with MenuMaker. In other words, the factory determines superclasses of some Gumbie classes (see, for example, PowerFrame2.py), which would be impossible if the factory were passed to the constructor; by the time the constructor is called, the superclasses are already in place.

This design puts a Python-specific spin on the factory pattern and may be useful in other situations as well. There is, however, one drawback caused by a limitation of the current version (2.1) of Jython. When compiled with jythonc, the resulting Java bytecode won't work without the Jython compiler classes. This isn't a problem in most situations, as jythonc creates jar files containing all the necessary classes. However, web browsers such as Netscape Communicator refuse to open such files as applets because the compiler classes cause security violations.

Finn Bock, one of the developers of Jython, has indicated that future versions may remove this limitation. For the time being, I have decided to introduce two additional modules, PowerFrame.py and LayeredFrame.py, that are the same as the generic modules PowerFrame2.py and LayeredFrame2.py, except superclasses are hard coded. Applets should use the specialized modules instead of the generic ones. Although somewhat unpleasant, this solution is harmless because all code based on the specialized modules also works with the generic ones, so the two branches merge effortlessly once this limitation of Jython is gone.

— P.B.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.