Building Interpreter Classes with PHP 5 - Seeing the interpreter pattern in action
(Page 4 of 4 )
As you'll certainly recall from the previous section, I said that the best way to understand how the interpreter pattern works is by developing a concrete example, where all the classes that were defined previously are put to work together.
Basically, what is expected here is that the interpreter class must be capable of parsing the appropriate commands passed in to its "interpret()" method, in this way displaying information about one or more user objects.
So, considering the expectations that you may have concerning the implementation of the previous interpreter class, below I included a short script, which demonstrates the functionality of this pattern.
The corresponding code sample is as follows:
try{
// create some fictional users
$user1=new User('John Doe','Binary Avenue
1234','john@domain.com');
$user2=new User('Mary Jackson','Port 80
Boulevard','mary@domain.com');
$user3=new User('Alfred Smith','Scripted Street
101010','alfred@domain.com');
// create 'UserSaver' object
$userSaver=new UserSaver();
// save fictional users via 'UserSaver' object
$userSaver->save($user1);
$userSaver->save($user2);
$userSaver->save($user3);
// create 'UserInterpreter' object
$userInt=new UserInterpreter($userSaver);
// display name of first user
echo $userInt->interpret('name',0);
/*
displays the following:
John Doe
*/
// displays postal address of first user
echo $userInt->interpret('address',0);
/*
displays the following:
Binary Avenue 1234
*/
// display email of first user
echo $userInt->interpret('email',0);
/*
displays the following:
john@domain.com
*/
// display name of second user
echo $userInt->interpret('name',1);
/*
displays the following:
Mary Jackson
*/
// display postal address of second user
echo $userInt->interpret('address',1);
/*
displays the following:
Port 80 Boulevard
*/
// display email of second user
echo $userInt->interpret('email',1);
/*
displays the following:
mary@domain.com
*/
// display name of last user
echo $userInt->interpret('name',2);
/*
displays the following:
Alfred Smith
*/
// displays postal address of last user
echo $userInt->interpret('address',2);
/*
displays the following:
Scripted Street 101010
*/
// display email of last user
echo $userInt->interpret('email',2);
/*
displays the following:
alfred@domain.com
*/
// send a erroneous command to the interpreter
//echo $userInt->interpret('age',2);
/* throws an exception with the following message:
A valid user command must be supplied to parse user data.
*/
// display data about all users at once
echo $userInt->interpret('all',0);
/*
displays the following:
Name : John Doe Postal Address : Binary Avenue 1234 Email :
john@domain.com
Name : Mary Jackson Postal Address : Port 80 Boulevard
Email : mary@domain.com
Name : Alfred Smith Postal Address : Scripted Street 101010
Email : alfred@domain.com
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
If you study the above example, then you'll realize how powerful the interpreter class can be when it comes to displaying information about one or more users. As you can see, the script begins by creating some fictional users, which are saved onto an instance of the "UserSaver" class, and then uses the interpreter object to show data about each one of the stored users.
Of course, this entire process is performed by sending out to the interpreter the correct user commands. This means that this simple implementation of the pattern in question can be really helpful for building a basic abstraction layer for handling user data. Quite good, isn't it?
As homework, try adding more users to the previous example, and see what happens when you send erroneous commands to the interpreter. Trust me, the experience can be truly fun!
Final thoughts
In this first tutorial of the series, I walked you through the basics of how to implement the interpreter pattern in PHP 5. However, this is only the beginning, because in the next part I'm going to show you how to use this handy pattern in conjunction with some string processing classes. Now that you're warned, are you going to miss it? I hope not!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |