The Interpreter Pattern in PHP 5

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Last week I continued my series of design patterns examples using PHP
5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern.  Here
now is my 20th PHP 5 design pattern example, the Interpreter Pattern.

In the Interpreter Design Pattern you define a language syntax, and
parse requests that use that syntax, and call the correct classes and
methods to handle the request.

An interesting pattern, although I don't think I've used it much in
systems I've worked on.  It is probably indespensible if you are
writing a compiler.

The State Pattern in PHP 5

And now, the State Pattern.

In the state pattern a class will change it's behavior based on
circumstances.  In my example a subclass of a specific interface may
actually switch the class being used to another subclass of the same

When I first drew up this example yesterday evening, based on one I did
a few years back in Java, I was pretty enthusiastic about it.  Now, I'm
not sure I like it 100%, and may still tinker with it.  I guess this
proves that these are perhaps "living examples", just my best try at an
example of a given pattern, are not set in stone, and may change.  I
also have a comments section with each pattern, so please feel free to
point out flaws or applaud any examples if you like. wrote:
Quoted text here. Click to load it

Site Timeline