Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

memoized instance pattern?

Thread view: 
metaperl - 22 Jul 2007 10:03 GMT
I'm reading the source code from the book "Building Parsers with Java"
by Steve Metsker - http://www.oozinoz.com/bpwj.htm

and in the source for ArithmeticParser.java you notice two times where
he manually checks for the existence of an object before creating it.

How might he have abstracted this? I'm only including the part of the
code which shows the repetition of the pattern of conditionally
creating an instance only if one does not already exist:

public class ArithmeticParser {
    protected Sequence expression;
    protected Alternation factor;

public Parser expression() {
    /*
    * This use of a static variable avoids the infinite
    * recursion inherent in the grammar.
    */
    if (expression == null) {

        // expression = term (plusTerm | minusTerm)*;
        expression = new Sequence("expression");
        expression.add(term());

        Alternation a = new Alternation();
        a.add(plusTerm());
        a.add(minusTerm());

        expression.add(new Repetition(a));
    }
    return expression;
}

protected Parser factor() {
    /*
    * This use of a static variable avoids the infinite
    * recursion inherent in the grammar; factor depends
    * on expFactor, and expFactor depends on factor.
    */
    if (factor == null) {
        factor = new Alternation("factor");

        Sequence s = new Sequence();
        s.add(phrase());
        s.add(expFactor());

        factor.add(s);
        factor.add(phrase());
    }
    return factor;
}

}
Oliver Wong - 24 Jul 2007 22:37 GMT
> I'm reading the source code from the book "Building Parsers with Java"
> by Steve Metsker - http://www.oozinoz.com/bpwj.htm
[quoted text clipped - 3 lines]
>
> How might he have abstracted this?

   The problem with trying to abstract this away is that you still have
to specify a list of instructions to use to generate an initial value in
the case where the variable is null, so you're not saving much:

(Off the top of my head, might not compile):
<pseudoCode>
interface DefaultValueGetter<T> {
 T getDefaultValue();
}

class NullCheckerUtility {
 public static <T> T initializeMaybe(T curValue, DefaultValueGetter<T>
initializer) {
   if (curValue != null) {
     return curValue;
   } else {
     return initializer.getDefaultValue();
   }
 }
}

public class ArithmeticParser {
protected Sequence expression;
protected Alternation factor;

 public Parser expression() {
   expression = NullCheckerUtility.initializeMaybe(expression, new
DefaultValueGetter<Sequence>() {
     @Override
     public Sequence getDefaultValue() {
       // expression = term (plusTerm | minusTerm)*;
       returnValue = new Sequence("expression");
       returnValue.add(term());

       Alternation a = new Alternation();
       a.add(plusTerm());
       a.add(minusTerm());

       returnValue.add(new Repetition(a));
       return returnValue;
     }
   });
 return expression;
 }
}
</pseudoCode>

   The "null check" is a common enough pattern that most experienced
developers will know what you're trying to do when they see that code,
whereas this NullCheckerUtility class I mentioned above will probably
force them to start reading the implementation of this "abstraction" to
understand exactly what's going on.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.