PHP
  Home arrow PHP arrow Page 4 - Using the Memento Pattern with a File Reading Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using the Memento Pattern with a File Reading Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2007-01-15


    Table of Contents:
  • Using the Memento Pattern with a File Reading Class
  • Building an originator class
  • Defining the signature of a caretaker class
  • The memento design pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using the Memento Pattern with a File Reading Class - The memento design pattern in action
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek