PHP
  Home arrow PHP arrow Page 4 - Using the Memento Pattern with a File ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Using the Memento Pattern with a File Reading Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over this final article of the series, you'll learn how to use the memento pattern...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT