IngredientAmountParser: Class and State Diagrams

The IngredientAmountParser class converts input strings - from end-users or files - into meaningful units like ingredients, measures, and quantities. You can read more about its implementation in the Parser javadoc documentation. We illustrate the workings of this class using a class diagram and a state diagram.

Back to the main UML page

Class diagram

The class diagram shows how the class uses the ElementParser interface to implement the State pattern. (You may want to read more about the State pattern on patterndigest.com.) Each of the six parser classes implements the interface, and the client class, IngredientAmountParser, has a field called parser that is a reference to an ElementParser. The parser classes reach back into IngredientAmountParser to change parser when it is time to move to the next state. (What lets them modify the private member parser? They are inner classes!) This mechanism is particularly nice because it hides the details of state transitions from the client class. It can be a little tricky to code, especially if you have multiple client classes - since we have only one, Java's inner classes make our job easier. (As is common in pattern class diagrams, the diagram shows only the key class members and methods.)

State diagram

The state diagram clarifies the dynamic behaviour of the parser. The parser has particularly simple state behaviour: it is almost a linear progression. You can use state diagrams to illustrate much more complex state behaviour, but LargoRecipes doesn't have anything more complex than this.

You may notice that the parsing seems out of order - how can you do the ingredient before the measure, for example? This is due to a preprocessing step that converts "1 tablespoon fresh basil", say, into "basil fresh tablespoon 1", which actually makes the parsing much simpler. See the javadoc for details.

The large box is the superstate Parsing, which the parser leaves when it runs out of tokens.


Last updated 25 October 2002
LargoRecipes source code, program, and web site copyright 2001-3 D. Squirrel

SourceForge.net Logo