Interpreter Pattern

noun

say it: “in-TUR-pruh-ter PAT-urn” /ɪnˈtɜrprətər ˈpætərn/

A design pattern that turns each piece of a rule (a check, an and, an or, a not) into a small object that answers one question, so rules can be built from the pieces as data.

What is the Interpreter pattern?

The Interpreter pattern turns each piece of a rule (a check such as "total over $50", or a join such as "and") into a small object that answers one question, so rules can be built from the pieces as data. A store's coupon rules become trees that marketing builds on a screen, with no new code for a new coupon.

class TotalOver:
    def __init__(self, amount):
        self.amount = amount

    def qualifies(self, total):
        return total > self.amount


print(TotalOver(50).qualifies(132))  # True

In a sentence

  • “Marketing built next week's coupon on the admin screen themselves, because the Interpreter pattern made every rule a tree of small pieces.”

Level: Freelancer · More OOP terms at this level

Updated