PHP
  Home arrow PHP arrow Page 2 - 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 - Building an originator class
    ( Page 2 of 4 )

    As I stated in the beginning, implementing the memento pattern requires at least two independent classes. Therefore, the first step that I'm going to take will consist of defining one of the primary classes that comprises the structure of this pattern.

    I'm referring to the originator element, which in this case is aimed at building a comprehensive file reading class. Meanwhile, the caretaker class that I plan to create will be capable of holding the value of one particular property that belongs to this file reader, in this manner implementing a basic file navigation mechanism.

    Now that I've outlined the general structure of the originator class, you'll want to take a look at its corresponding signature:

    // define 'FileDataReader' class (in this case, this is the
    Originator class)
    class FileDataReader{
       private $filePointer;
       private $fileData;
       public function __construct(
              $dataFile='default_data_file.txt',$filePointer=0){
           if(!file_exists($dataFile)){
              throw new Exception('Invalid data file!');
           }
           $this->setFilePointer($filePointer);
           $this->setFileData($dataFile);
       
    }
       // set value for file pointer
       public function setFilePointer($filePointer){
         if(!is_int($filePointer)||$filePointer<0){
           throw new Exception('Invalid pointer for data file!');
         }
         $this->filePointer=$filePointer;
       }
       // fetch value of file pointer
       public function getFilePointer(){
         return $this->filePointer;
       }
       // read data from file
       public function setFileData($dataFile){
         if(!$this->fileData=file($dataFile)){
           throw new Exception('Error reading from data file!');
         }
       }
       // return file data as array
       public function getFileData(){
         return $this->fileData;
       }
       // fetch one line from data file
       public function fetchFileLine(){
         if(!$fileLine=$this->fileData[$this->filePointer]){
           throw new Exception('Error fetching line from file
    data!');
         }
         return $fileLine;
       }
    }

    As you can see, the above "FileDataReader" class is capable of performing some useful tasks, with reference to reading data from a specified text file. As to the functionality of this class, you'll realize that it's capable of fetching file data as an array structure, as well as retrieving one line of that file at once. Not too bad, right?

    However, while I have to admit that the previous file reading class isn't going to change your life as a PHP developer, it does show in a nutshell how to define the structure of an originator. Naturally, any class can potentially fall under that category, but in this case the "FileDataReader" class is more than enough to demonstrate the functionality of the memento pattern.

    At this stage, I'm pretty certain that you grasped the logic that drives the originator class. Therefore, in the following section I'll show you how to create the corresponding caretaker class. As I said when I explained how the caretaker class works, it will be capable of maintaining the value of a specific property.

    To learn how this brand new class will be created, please click on the link shown below and keep reading.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek