In accordance with the concepts that I just deployed in the prior section, I'm going to build a simple PHP class. It will be merely a logical representation of a typical blog entry, where all its attributes, including its title, author and content will be saved as class properties. After defining this blog class, I'm going to create a couple of additional classes for processing the corresponding blog entries by using the interpreter pattern. But I'm getting ahead of myself, so for the moment, please examine the signature of the class below. It represents a conventional blog entry. Its definition is as follows: // define 'Blog' class As indicated above, the "Blog" class that I just defined is a basic representation of a simple blog entry, where all its properties can be easily retrieved via the corresponding accessing methods. As you can see, the logic implemented by this class is in fact very easy to grasp, so you shouldn't have major problems understanding how it works. Well, now that you have learned the definition corresponding to the previous "Blog" class, have a look at the respective signatures of the following classes, which are tasked with inserting, updating and deleting blog entries by implementing the interpreter pattern. Having said that, these brand new classes look like this: // define 'BlogHandler' class // define 'BlogInterpreter' class If you studied the signatures of the above two classes, then you'll have to agree with me that they demonstrate in a friendly fashion how to build a basic blog application that uses the schema imposed by the interpreter pattern. As you can see, the first "BlogHandler" class is responsible for interfacing directly with MySQL to perform the insertion, update and deletion of blog entries respectively, while the second class, called "BlogInterpreter," is tasked with parsing a set of predefined commands for performing all the aforementioned blog-related operations. Now do you see how the interpreter pattern is used in this case to build this simple blog application? I'm sure you do! All right, at this point you hopefully understand how all the previous classes do their business, therefore I think it's time to show how they can be used all together in one practical example. This will demonstrate the functionality of the interpreter pattern. Want to see how this concrete example will be developed? Go ahead and read the following section. I'll be there, waiting for you.
blog comments powered by Disqus |