Design Pattern: Interpreter Pattern in Java

Interpreter pattern is used to represent any language operation in Object Oriented Programming context.

This article demonstrates Interpreter pattern implementations in Java. Check the following examples.

Examples

Check the following examples of Interpreter pattern implementation in Java.

Example #1: Logical Operation

Let’s take the example of boolean operations like AND, OR, XOR, etc.

Operation Interface

// Operation.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public interface Operation {
    boolean execute(String opContext);
}

Terminal Operation Class

// TerminalOperation.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public class TerminalOperation implements Operation {

    private String data;

    public TerminalOperation(String data) {
        this.data = data;
    }

    @Override
    public boolean execute(String opContext) {
        return opContext.contains(data);
    }
}

AND Operation Class

// AndOperation.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public class AndOperation implements Operation {

    private final Operation op1;
    private final Operation op2;

    public AndOperation(Operation op1, Operation op2) {
        this.op1 = op1;
        this.op2 = op2;
    }

    @Override
    public boolean execute(String opContext) {
        return op1.execute(opContext) && op2.execute(opContext);
    }
}

OR Operation Class

// OrOperation.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public class OrOperation implements Operation {

    private final Operation op1;
    private final Operation op2;

    public OrOperation(Operation op1, Operation op2) {
        this.op1 = op1;
        this.op2 = op2;
    }

    @Override
    public boolean execute(String opContext) {
        return op1.execute(opContext) || op2.execute(opContext);
    }
}

XOR Operation Class

// XorOperation.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public class XorOperation  implements Operation {

    private final Operation op1;
    private final Operation op2;

    public XorOperation(Operation op1, Operation op2) {
        this.op1 = op1;
        this.op2 = op2;
    }

    @Override
    public boolean execute(String opContext) {
        return op1.execute(opContext) ^ op2.execute(opContext);
    }
}

Demo

// Demo.java

package com.bigboxcode.designpattern.interpreter.booleanoperation;

public class Demo {
    public static void main(String[] args) {
        Operation op1 = new TerminalOperation("Big");
        Operation op2 = new TerminalOperation("Box");

        Operation andChecker = new AndOperation(op1, op2);
        Operation orChecker = new OrOperation(op1, op2);
        Operation xorChecker = new XorOperation(op1, op2);


        String checkStr1 = "Big Box Code";
        String checkStr2 = "Only Big Code";
        String checkStr3 = "Only Box Code";
        String checkStr4 = "No Code";


        boolean andResult1 = andChecker.execute(checkStr1);
        boolean andResult2 = andChecker.execute(checkStr2);
        boolean andResult3 = andChecker.execute(checkStr3);
        boolean andResult4 = andChecker.execute(checkStr4);

        System.out.println("Data: " + checkStr1 + "; AND Result: " + andResult1);
        System.out.println("Data: " + checkStr2 + "; AND Result: " + andResult2);
        System.out.println("Data: " + checkStr3 + "; AND Result: " + andResult3);
        System.out.println("Data: " + checkStr4 + "; AND Result: " + andResult4);

        System.out.println("n----------------------------------------------n");

        boolean orResult1 = orChecker.execute(checkStr1);
        boolean orResult2 = orChecker.execute(checkStr2);
        boolean orResult3 = orChecker.execute(checkStr3);
        boolean orResult4 = orChecker.execute(checkStr4);

        System.out.println("Data: " + checkStr1 + "; OR Result: " + orResult1);
        System.out.println("Data: " + checkStr2 + "; OR Result: " + orResult2);
        System.out.println("Data: " + checkStr3 + "; OR Result: " + orResult3);
        System.out.println("Data: " + checkStr4 + "; OR Result: " + orResult4);

        System.out.println("n----------------------------------------------n");

        boolean xorResult1 = xorChecker.execute(checkStr1);
        boolean xorResult2 = xorChecker.execute(checkStr2);
        boolean xorResult3 = xorChecker.execute(checkStr3);
        boolean xorResult4 = xorChecker.execute(checkStr4);

        System.out.println("Data: " + checkStr1 + "; XOR Result: " + xorResult1);
        System.out.println("Data: " + checkStr2 + "; XOR Result: " + xorResult2);
        System.out.println("Data: " + checkStr3 + "; XOR Result: " + xorResult3);
        System.out.println("Data: " + checkStr4 + "; XOR Result: " + xorResult4);
    }
}

Output

Data: Big Box Code; AND Result: true
Data: Only Big Code; AND Result: false
Data: Only Box Code; AND Result: false
Data: No Code; AND Result: false

----------------------------------------------

Data: Big Box Code; OR Result: true
Data: Only Big Code; OR Result: true
Data: Only Box Code; OR Result: true
Data: No Code; OR Result: false

----------------------------------------------

Data: Big Box Code; XOR Result: false
Data: Only Big Code; XOR Result: true
Data: Only Box Code; XOR Result: true
Data: No Code; XOR Result: false

Source Code

Use the following link to get the source code:

Other Code Implementations

Use the following links to check Interpreter pattern implementation in other programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.