According to the concepts deployed in the introduction, in this first article of the series I'm going to demonstrate how to build an interpreter class by developing a simple user managing system. It will perform some useful tasks, such as adding new users to the system in question and displaying basic information associated with them, including their full names and their postal and email addresses. Next, I'm going to create an interpreter class, which will be responsible for parsing a set of predefined commands. These commands will come in handy for showing the aforementioned user-related information. However, as you'll see shortly, this process will be sent through the corresponding interpreter, in this way implementing the programmatic model imposed by the homonymous pattern. All right, having said that, please take a look at the signature of the following "User" class, which will be useful for handling different users as a bunch of objects. Its definition is listed below: // define 'User' class As indicated above, the previous "User" class is merely a computational model that represents a typical user, where his full name and postal/email addresses are stored as class properties. Besides, you should notice that this class presents some useful accessors for retrieving the properties that I mentioned before. So far, so good. You shouldn't have any problems understanding the logic followed by the prior "User" class, since as you can see, it's very easy to grasp. However, I stated previously that all these users should be stored somewhere so that they can be handled later on. So, based on this requirement, below I coded a simple class, aimed at saving objects of type "User" as an array structure. The signature of this brand new class is as following: // define 'UserSaver' class As you can see, the prior "UserSaver" class performs some useful tasks, like saving different user objects to an internal array, in addition to loading a particular user. Besides, there's an additional method, called "loadAll()," which obviously returns to calling code all the users stored in the aforementioned array. Now that you have learned the respective signatures for the previous two classes, it's time to leap forward and jump into the next section, where I'm going to show you how to create a simple interpreter class. In consonance with the schema dictated by this pattern, this class will define (and parse, by the way) a set of specific commands, which will display information about the user objects that you learned previously. To see how this interpreter class will be built, please click on the link below and keep reading.
blog comments powered by Disqus |