Pattern 31Target-Specific Generator Classes

Purpose

This pattern describes a class library whose sole purpose is to represent and generate output constructs in a particular language.

Discussion

Rather than use print statements to generate programs or data, generator classes let us insulate application code from the exact syntax of an output language. In that sense, they are similar to templates. The difference is, we’re defining a special class for each output element, whereas, with templates, we use a single StringTemplate class. In both cases, the objects hold the data necessary to generate the associated output constructs. The only (minor) advantage to using specialized generator classes is that we can name the data fields.

Each generator ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.