Java Omnibus
OOAD & Architecture / GoF patterns / Behavioral / Interpreter Pattern in Java
GoF Behavioral Java Order Management
GoF Pattern

Interpreter is useful only when you truly have a small language or expression grammar to evaluate.

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Intent

What the pattern is trying to do

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Problem

What force creates the need

A system needs to evaluate a small, domain-specific expression language, but scattering parser logic and conditionals would become brittle.

Solution

How the pattern responds

Model grammar elements as expression objects and evaluate them through a shared interpret operation.

Structure

How the moving parts fit together

Terminal and non-terminal expressions implement a common interface, and composed expression trees evaluate input within a context.

Java Example

Java example in the Order Management domain

Interpreter Pattern in Java example
package org.javaomnibus.ecommerce.gof.behavioral;

public interface PromotionRule {
    boolean matches(Order order);
}

public final class PremiumCustomerRule implements PromotionRule {
    @Override
    public boolean matches(Order order) {
        return order.isPremiumCustomer();
    }
}

public final class AndRule implements PromotionRule {
    private final PromotionRule left;
    private final PromotionRule right;

    public AndRule(PromotionRule left, PromotionRule right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public boolean matches(Order order) {
        return left.matches(order) && right.matches(order);
    }
}
Code Walkthrough

Step by step

  1. Each rule object represents one piece of a small promotion language.
  2. Composite rules build larger expressions from smaller ones.
  3. This works when the domain really has reusable grammar-like constructs.
  4. Interpreter is usually best for narrow DSLs, not for ordinary business logic.
When To Use

Where this pattern helps

  • When you have a small rules language or query DSL
  • When the grammar is stable and expression objects improve clarity
When Not To Use

When a simpler design is better

  • When the logic is ordinary application code without a true language
  • When a mature parser or rules engine is more appropriate
Trade-Offs

What this pattern costs

  • Can create many small classes quickly
  • Performance and maintainability degrade if the language grows too large
Modern Relevance

How this fits today

Interpreter is less common in everyday Java, but it remains relevant for rule DSLs, search filters, workflow expressions, and small query languages.

Common Misuse

Where teams get it wrong

Forcing normal business branching into interpreter-style classes often overcomplicates code that was never really a language in the first place.

Related Patterns

Compare with nearby choices


What To Read Next