PHP
  Home arrow PHP arrow Page 4 - Introducing the Memento Pattern
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? 
PHP

Introducing the Memento Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-01-08


    Table of Contents:
  • Introducing the Memento Pattern
  • Creating the first piece of the memento pattern
  • Building the second piece of the memento pattern
  • Implementing the memento pattern

  • 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


    Introducing the Memento Pattern - Implementing the memento pattern
    ( Page 4 of 4 )

    As I said at the end of the section that you just read, the best way to understand the logic that drives the memento pattern is by creating a hands-on example that shows in a step-by-step format how a caretaker object can hold the state of a property that belongs to an originator.

    Based upon this premise, below I set up the example in question. It  demonstrates progressively how the previously created "ArrayElementSelector" class has the capacity to retain the value of the "arrayIndex" property that belongs to the "ArrayProcessor." The corresponding code sample is as follows:

    try{
        // example using Originator and Caretaker classes
        // instantiate new 'ArrayProcessor' object
        $arrayProcessor=new ArrayProcessor(array('This is element 1','This is element 2','This is element 3','This is element 4','This is element 5'));
        // instantiate new 'ArrayElementSelector' object
       $arrayElemSelector=new ArrayElementSelector($arrayProcessor);
       // display first element of input array
       echo 'First element of input array is the following: '.$arrayProcessor->getArrayElement();           

       /* displays the following:
       First element of input array is the following: This is element 1
        */           

        $arrayProcessor->setArrayIndex(1);
        $arrayElemSelector->setArrayIndex($arrayProcessor);
        // caretaker object holds up the value of array index
        $arrayElemSelector->setArrayIndex($arrayProcessor);
        echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

        /*
        displays the following:
        Second element of input array is the following: This is element 2
        */

        // move array index to nonexistent element of input array
        $arrayProcessor->setArrayIndex(10);
        echo $arrayProcessor->getArrayElement();

        /* 
        displays the following:
        Error fetching element of input array!
        */

        // move array index back to second element of input array by using caretaker object
        $arrayElemSelector->getArrayIndex($arrayProcessor);
        $arrayProcessor->getArrayIndex();
        echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

       /*
       displays the following:
       Second element of input array is the following: This is element 2
       */

    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As illustrated above, after instantiating the two objects required by the memento pattern, that is the originator (in this case, represented by the ArrayProcessor class), and the caretaker (as you know, the "ArrayElementSelector class), the first element of the inputted array is properly displayed.

    However, the actual capacity of the pattern is demonstrated when the "ArrayElementSelector" object stores the value of the "arrayIndex" property, before moving this index forward and trying to display a non-existent array element:

    // caretaker object holds up the value of array index
    $arrayElemSelector->setArrayIndex($arrayProcessor);
    // move array index to nonexistent element of input array
    $arrayProcessor->setArrayIndex(10);
    echo $arrayProcessor->getArrayElement();

    Obviously, this condition fires up an exception and the script is halted. But, since the caretaker has previously maintained the state of the "arrayIndex" property, it's possible to go back and redisplay the second element of the inputted array. As you can imagine, this situation is demonstrated by the following lines of code:

    // move array index back to second element of input array by using caretaker object
    $arrayElemSelector->getArrayIndex($arrayProcessor);
    $arrayProcessor->getArrayIndex();
    echo 'Second element of input array is the following: '.$arrayProcessor->getArrayElement();

    Now, after understanding how the originator and caretaker classes work together, I'm sure that you'll agree with me that the memento pattern comes in handy for maintaining the state of a particular class. Isn't this great?

    Wrapping up

    Over this first installment of the series, I explained the basics of how to apply the memento pattern in PHP 5. Aside from being introduced to the concepts of the originator and caretaker classes respectively, hopefully you learned how to store the state of a given class by using another one, which is indeed useful.

    However, this journey is not finished. In the last article of the series, I'll show you how to use the memento pattern to hold the state of a file reading class. I hope to see you there!



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT