PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

■Chapter 11: Performing and Representing Tasks .............................................


4:


top marks


52:
dunce hat on


You may need to read through this section a few times before the process clicks. The old issue of
object versus class trees might confuse you here. Expression classes are arranged in an inheritance
hierarchy just as Expression objects are composed into a tree at runtime. As you read back through the
code, keep this distinction in mind.
Figure 11–2 shows the complete class diagram for the example.


Figure 11–2. The Interpreter pattern deployed


Interpreter Issues


Once you have set up the core classes for an Interpreter pattern implementation, it becomes easy to
extend. The price you pay is in the sheer number of classes you could end up creating. For this reason,
Interpreter is best applied to relatively small languages. If you have a need for a full programming
language, you would do better to look for a third-party tool to use.
Because Interpreter classes often perform very similar tasks, it is worth keeping an eye on the classes
you create with a view to factoring out duplication.
Many people approaching the Interpreter pattern for the first time are disappointed, after some
initial excitement, to discover that it does not address parsing. This means that you are not yet in a

Free download pdf