Design Patterns for Dummies. The Interpreter Pattern
Following up on the series on design patterns, I will be writing about the Interpreter pattern today. Interpreter is a behavioral pattern. You can read about the other patterns from the following links.
- Strategy Pattern
- State Pattern
- Template Method Pattern
- Chain of Responsibility Pattern
- Command Pattern
- Iterator Pattern
- Mediator Pattern
- Observer Pattern
- Visitor Pattern
You can read about the Structural patterns here.
You can read about the Creational patterns here.
The Interpreter
The interpreter pattern deals with interpretation of instructions defined for some specific purpose. The instructions are given as notations which are precise and can be defined in terms of grammar. For the sake of example, the notation in the following example is taken to be “PCCCCPC”. We will have to parse this expression and interpret it.
The components that make up an interpreter pattern are Context which contains information that is used by the interpreter and Element ( Term ) which provides an interface for the structure and the default operation for the interpreter.
Parsing
The interpreter pattern itself does not specify how the objects are to be built. For this purpose parsing is used, in which the the input is matched against a grammar to create the object structure. Once this is created the objects can passed to an interpreter to do its work.
This pattern has performance issues and is rarely used. We can use the Interpreter pattern when we have a grammar to be interpreted and the grammar is fairly small and efficiency is not critical.
Comments