Tutorials Design Patterns in C#

Interpreter Pattern — Complete Guide

Interpreter Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.

On this page

Design Patterns in C# · Lesson 15 of 69

Interpreter Pattern

GoF Core ✓EnterpriseCloud & Craft

Enterprise · 2 — App patterns · ~6 min · Module 3: Behavioral Design Patterns

What is this?

Interpreter defines a grammar and an evaluator for a simple language — useful for rules engines, not general programming languages.

Why should you care?

ShopNest promo rules like “SKU starts with HD and price > 1000” need a tiny expression tree.

See it live — copy this example

Paste into a C# console or class library project and run dotnet run.

public interface IRule { bool Eval(Item item); }
public sealed record Item(string Sku, decimal Price);
public sealed class SkuPrefixRule : IRule
{
    private readonly string _prefix;
    public SkuPrefixRule(string p) => _prefix = p;
    public bool Eval(Item i) => i.Sku.StartsWith(_prefix);
}
public sealed class MinPriceRule : IRule
{
    private readonly decimal _min;
    public MinPriceRule(decimal m) => _min = m;
    public bool Eval(Item i) => i.Price >= _min;
}
public sealed class AndRule : IRule
{
    private readonly IRule _a, _b;
    public AndRule(IRule a, IRule b) { _a = a; _b = b; }
    public bool Eval(Item i) => _a.Eval(i) && _b.Eval(i);
}

IRule promo = new AndRule(new SkuPrefixRule("HD"), new MinPriceRule(1000));
Console.WriteLine(promo.Eval(new Item("HD-100", 4999)));

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • Leaf rules check one fact; AndRule composes them.
  • That is a tiny interpreter over a rule AST.
  • For complex grammars prefer a mature library.

Practice next

  1. Evaluate the sample item true/false.
  2. Add OrRule.
  3. Parse a simple string into rules later.
  4. Add NotRule.
  5. Fail HD-50 at price 500.

Remember

AST + Eval. Compose rules. Keep scope tiny.

ShopNest promo rule tree

Marketing composes prefix + min-price rules.

Outcome: Promos change without redeploying ifs everywhere.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Redo Functionality:?
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Mid PDF Detailed
Abstract Expression (IExpression):?
Short answer: The IExpression interface declares an Interpret method, which is the core of the interpreter pattern. This method is used to interpret and evaluate expressions. public interface IExpression Example code { i…
Mid PDF Detailed
Mediator (ChatMediator):?
Short answer: The mediator manages communication between the users. It maintains a list of all users and broadcasts messages to all other users when one user sends a message. This keeps the users from directly knowing ab…
Mid PDF Detailed
Component (ICoffee):?
Short answer: This is the base interface that defines the common methods for the Cost() and Description() that every coffee component will implement. public interface ICoffee Example code { double Cost(); string Descript…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details