PHP
  Home arrow PHP arrow Page 2 - 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 - 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


       · 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 3 hosted by Hostway
    Stay green...Green IT