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

The memento design pattern in action - 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 I expressed in the section that you just finished reading, one of the most educational ways to understand how the memento pattern works is by creating a practical example where the two classes are put to work together.

In this case, you'll see how the "FileDataSeeker" class defined in a previous section is capable of maintaining the value of the "filePointer" property, which belongs to the "FileDataReader" class.

Let us assume that there is a sample text file containing the following lines of basic data:

This is the line 1 of data file.
This is the line 2 of data file.
This is the line 3 of data file.
This is the line 4 of data file.
This is the line 5 of data file.
This is the line 6 of data file.
This is the line 7 of data file.
This is the line 8 of data file.
This is the line 9 of data file.
This is the line 10 of data file.

Here is the short script that demonstrates the functionality provided by the memento pattern. Please examine the code sample below:

try{
   // example using Originator and Caretaker classes
   // instantiate new 'FileDataReader' object
   $fileDataReader=new FileDataReader();
   // instantiate new 'FileDataSeeker' object
   $fileDataSeeker=new FileDataSeeker($fileDataReader);
   // display first line of data file
   echo 'Value for first line of data file is as follows:
'.$fileDataReader->fetchFileLine();
           

  /*
  displays the following
  Value for first line of data file is as follows:
  This is the line 1 of data file.
  */            

  // move file pointer to last line of data file
  $fileDataReader->setFilePointer(9);
  // display last line of data file
  echo 'Value for last line of data file is as follows:
'.$fileDataReader->fetchFileLine();
           

  /*
  displays the following
  Value for last line of data file is as follows:
  This is the line 10 of data file.
  */           

  // store value of file pointer onto caretaker object
  $fileDataSeeker->setFilePointer($fileDataReader);
  // try to fetch an invalid line from data file
  //$fileDataReader->setFilePointer(-1);
  // trigger an error
  echo $fileDataReader->fetchFileLine();

  /*
  displays the following
  Invalid pointer for data file!
  */

  // move file pointer back to last element of data file
  // via caretaker object
  $fileDataSeeker->getFilePointer($fileDataReader);
  $fileDataReader->getFilePointer();
  echo 'Value for last line of data file is as follows:
'.$fileDataReader->fetchFileLine();

  /*
  displays the following:
  Value for last line of data file is as follows:
  This is the line 10 of data file.
  */
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

With reference to the example shown above, there are some interesting things to note. First, notice how after instantiating the respective originator and caretaker objects, the first line included in the sample data file is read and displayed. So far, there is nothing unexpected, right?

However, things become a little more exciting when the file reading object is pointed to a non-existent line of the mentioned data file. This throws an exception, and consequently the execution of the script is halted. However, since the value of its "filePointer" property was previously stored by the respective "FileDataSeeker" object, it's possible to move the pointer in question back to the last element of the data file.

This condition is clearly illustrated by the following code snippet:

// move file pointer back to last element of data file
// via caretaker object
$fileDataSeeker->getFilePointer($fileDataReader);
$fileDataReader->getFilePointer();
echo 'Value for last line of data file is as follows:
'.$fileDataReader->fetchFileLine();

As you can see, this is where the memento pattern comes into its own, since the value of the mentioned file pointer property is retrieved from the caretaker object. Now do you see how the state of class can be tracked with this handy pattern? I suppose you do!

As usual, feel free to incorporate your own modifications to all the classes that were shown in this tutorial. In this way you can expand your background on the memento pattern. Fun is already guaranteed!

Final thoughts

Sadly, we've come to the end of this series. In this pair of tutorials, I introduced the key points of how to apply the memento design pattern with PHP 5. As you saw, if you're looking for an accessible approach for keeping track of different properties that belong to a given class, indeed this pattern could be a good choice.

See you in the next PHP tutorial!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: