Home arrow PHP arrow Page 3 - Using the Memento Pattern with a File Reading Class

Defining the signature of a caretaker class - PHP

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.

TABLE OF CONTENTS:
  1. Using the Memento Pattern with a File Reading Class
  2. Building an originator class
  3. Defining the signature of a caretaker class
  4. The memento design pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
January 15, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you might have guessed, the functionality of a caretaker class has to be strongly related to the nature of the tasks performed by the originator. Keeping in mind this basic principle, below I listed the definition for the corresponding caretaker. In a fit of inspiration, I named it "FileDataSeeker." Take a look at it, please:

// define 'FileDataSeeker' class (in this case, this is the
Caretaker class)
class FileDataSeeker{
   private $filePointer;
   // the constructor takes up the originator object as the only
input parameter
   public function __construct(FileDataReader $fileDataReader){
     $this->setFilePointer($fileDataReader);
   }
   public function setFilePointer(FileDataReader $fileDataReader){
     $this->filePointer=$fileDataReader->getFilePointer();
   }
   public function getFilePointer(FileDataReader $fileDataReader){
     $fileDataReader->setFilePointer($this->filePointer);
   }
}

As illustrated above, the "FileDataSeeker" class has some useful methods for handling the "filePointer" property that belongs to the respective originator class. These methods will help this caretaker class to carry out its responsibility to store the value of the property. Thus, the value can be retrieved at a later time. Pretty logical, isn't it?

By providing the "FileDataSeeker" class with this ability, if the file reading class locates its file pointer at a non-existent place, it will be possible to move it back to a valid position via the respective caretaker. Definitely, that makes a lot of sense!

However, showing you the originator and the caretaker separately won't give you a clear idea of how they work. Thus, in response to this issue, in the next section I'll create a simple script, which hopefully will demonstrate the power of these two classes when they operate together.

As usual, to see how this will be achieved, go ahead and read the following section. I'll be there, waiting for you.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: