Design patterns: What is the difference between Interpreter and Composite?

First of all Interpreter is Behavioral Pattern and Composite is Structural. Which it-self says enough of how you should consider using them. Basically Structural Design Patterns deal with relationships between entities, making it easier for these entities to work together. On the other hand Behavioral Design Patterns are used in communications between entities and make it easier and more flexible for these entities to communicate.

So the answer to your first question is simply said - No.

Update: I found information that can be useful to you here.

Structural patterns are used for defining the static properties of the system (see Class diagram).

Example: Factory pattern can be used to create entities which constitute your system. You can have object Button that have different graphical properties on Windows vs. OS X. Factory pattern will create the Button regardless of the OS and the created object will have the same interfaces on both OSs expose the same behavior despite having different internals.

Behavioral patterns are used for defining the dynamic behavior of the system (see Activity, Sequence etc. diagrams).

Example: Adapter pattern can be used at runtime to transparently allow interfacing of 2 entities that do not share interface between them. It effectively changes the behavior of an object at runtime.


The interpreter pattern has little to do with the composite pattern. You might implement parts of an interpreter using a composite - for example, here an example is given of an OrExpression:

public class OrExpression extends Expression{
    private Expression expression1 = null;
    private Expression expression2 = null;

    public OrExpression(Expression expression1, Expression expression2) { 
        this.expression1 = expression1;
        this.expression2 = expression2; 
    }

    public boolean interpret(String str) { 
        return expression1.interpret(str) || expression2.interpret(str);
    } 
}

This is an example of the Composite pattern, where the OrExpression is composed of two other expressions, and the result of each passed to a boolean or function.

Generally though, this is only a small part of a possible interpreter - it's more of a high-level concept than a simple composite, and only used in quite niche domains (needing to interpret arbitrary strings, possibly to then execute commands or represent a set of rules)