HomePHP Using the Memento Pattern with a File Reading Class
Using the Memento Pattern with a File Reading Class
The memento pattern can be used to maintain the state of a property that belongs to a specific class. If you want to learn more about how to do this, start reading this tutorial! Welcome to the final installment of the series "Maintaining the State of Classes." In two parts, this series teaches you how to use the memento design pattern with PHP 5, and it accompanies the corresponding theoretical concepts with diverse hands-on examples.
If you already read the first article of the series, then you'll surely recall what this pattern is about. Just in case the subject doesn't ring any bells to you, however, let me offer you a quick introduction. In short, when the memento pattern is applied, two classes are required to satisfy a well-defined model: the "originator" and the "caretaker."
Where this pattern is used, the caretaker is capable of holding the value of one or more properties that belong to the respective originator. Nevertheless, a classic implementation of these two classes means usually that the referenced caretaker can't directly access the properties of the originator. So how can its state be maintained? Usually the caretaker takes the originator as an input parameter, and uses all its methods to gain access to the respective properties.
All right, I think that the previous explanation should give you an accurate idea of how this pattern works. Let me now introduce the topics that I'll be covering in this last article of the series. I'll show you how to use the memento pattern to keep track of some properties that correspond to a file reading class. In this way we'll construct a flexible mechanism for moving back and forth between file lines.
With the preliminaries out of our way, let's continue exploring the capacities offered by the memento pattern. Let's get started!