Java enum technique - template method pattern

634 views
Skip to first unread message

RogerV

unread,
Dec 4, 2008, 2:41:17 AM12/4/08
to The Java Posse
While working on my jfig parser I was doing some google searching
regarding Java enum feature. Came across a tutorial on a technique
making clever use of Java enum, and which was dubbed:

template method pattern

Here's a code snippet based on something I implemented using this
technique per my jfig parser:

http://vossnet.org/template_method_pattern-a_code_snippet.rtf

(Open in something that handles rtf and will have color syntax source
code.)

The gist is that this is a very nice way to bind strings to methods
that are in themselves associated to enum ordinals. Input a string
from somewhere, bind it to its respective enum ordinal, and then
execute a method on the enum object in a OOP polymorphic manner. OOP
polymorphic method dispatch insures the correct method implementation
gets invoked.

Would be a great way to roll minimalist command pattern
implementations, which are frequently driven by simple command
strings. And is just the ticket I need for my parser where will by
doing such string binding/execution on a frequent basis.

ROGER MILLER

unread,
Dec 5, 2008, 4:49:00 AM12/5/08
to The Java Posse
Hi,
you mention a tutorial in your post, "template method pattern".
Can you please give a link to this tutorial?
I am going to implement a bunch of code to match a token into a
specific method sometime next week and i collect all technical doc i
can to find a better approach to do this efficiently.
Thanks.
Miller

Michael Lee

unread,
Dec 6, 2008, 1:51:48 AM12/6/08
to java...@googlegroups.com
You may want to look at Effective Java 2nd Edition.

Item 34: Emulate extensible enums with interfaces.

RogerV - I briefly read over the code - IMO - Enum is more of
'strategy pattern'..

RogerV

unread,
Dec 8, 2008, 5:45:58 PM12/8/08
to The Java Posse
Here's the tutorial link:

Making the Most of Java 5.0: Enum Tricks
http://www.ajaxonomy.com/2007/java/making-the-most-of-java-50-enum-tricks

Skim down until get to this section: "Enums and Template Methods"

RogerV

unread,
Dec 8, 2008, 6:22:16 PM12/8/08
to The Java Posse
On Dec 5, 10:51 pm, "Michael Lee" <stinkymi...@gmail.com> wrote:
> RogerV - I briefly read over the code - IMO - Enum is more of
> 'strategy pattern'..

The tutorial article (that I've now provided a link to) had dubbed
this approach - based on the Java enum feature - to be an example of
the "template method pattern".

When I went to the wikipedia entry that that article linked, what is
described there is what us OOP developers accomplish by use of
abstract classes and abstract methods. We code a high level algorithm
in one place and rely on subclass implementations of abstract methods
to provide the specific behavior for the particular item(s) being
operated on. (This OOP approach is not the only way to do this
pattern, as the wikipedia entry cites using C++ templates as well. C++
template libraries permit things like collection class templates to be
combined with iterator and memory management templates, etc.)

Well, the Java enum approach relies on the fact that enum ordinals are
effectively subclass instances of their enum class. Hence you can
place abstract method declarations in the enum class and then have
each enum ordinal provide an implementation of the abstract method(s).
It basically just makes for a very concise and tidy way to implement
this pattern - particularly where it's convenient to think of the enum
ordinals as representing command tokens.

In my particular case of the JFig parser, I needed to go an additional
step by associating strings from the raw input to their respective
ordinal "command token". Hence I added a static map object to the enum
class that is initialized with name/value pairs - raw input string as
a key and the value being the respective enum ordinal. Once again the
Java enum class provided a very convenient means to set up this string
binding association to "command token".
Reply all
Reply to author
Forward
0 new messages