PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Building the second piece of the memento pattern
    ( Page 3 of 4 )

    In consonance with the concepts that I expressed in the prior section, the schema imposed by the memento pattern must be completed by creating an additional class, called the caretaker. As its name implies, this class will take care of holding a specific state of the previously defined originator, completing the logic implemented by the pattern in question.

    However, before I teach you how to put the two pertinent classes to work together, first let me show you the signature that corresponds to the caretaker. In this you will understand later how this new class interacts with the respective originator.

    Here is the definition for a brand new class, called "ArrayElementSelector." It plays the role of the caretaker. Have a look at its signature, please:

    // define ArrayElementSelector' class (in this case, this is the Caretaker class)
    class ArrayElementSelector{
       // the constructor accepts the originator as the unique parameter
       public function __construct(ArrayProcessor $arrayProcessor){
         $this->setInputArray($arrayProcessor);
         $this->setArrayIndex($arrayProcessor);
       }
       // set input array by using originator object (ArrayProcessor)
      public function setInputArray(ArrayProcessor $arrayProcessor){
        $this->inputArray=$arrayProcessor->getInputArray();
      }
      // get input array by using originator object (ArrayProcessor)
      public function getInputArray(ArrayProcessor $arrayProcessor){
        $arrayProcessor->setInputArray($this->inputArray);
      }
      // set array index by using originator object (ArrayProcessor)
      public function setArrayIndex(ArrayProcessor $arrayProcessor){
        $this->arrayIndex=$arrayProcessor->getArrayIndex();
      }
      // get array index by suing originator object (ArrayProcessor)
      public function getArrayIndex(ArrayProcessor $arrayProcessor){  
        $arrayProcessor->setArrayIndex($this->arrayIndex);
      }
    }

    As you can see, the class shown above implements a classic structure for a caretaker class. As you'll recall from the concepts that I explained in the introduction of this article, the previous class is only capable of accessing the originator via its interface, and never directly. Pretty weird, right?

    Of course, this condition is clearly reflected by each of the methods exposed by the respective caretaker. As you can see, these methods access and set the index of the given input array (and its elements too) only by using the originator object.

    Besides, I'd like you to pay attention to the signature of the following method, which is extremely demonstrative:

    // set array index by using originator object (ArrayProcessor)
    public function setArrayIndex(ArrayProcessor $arrayProcessor){
     
    $this->arrayIndex=$arrayProcessor->getArrayIndex();
    }

    Do you see how the value of the "arrayIndex" property is accessed by using an ArrayProcessor's method? I bet you do! As you'll see soon, this condition is crucial for holding the state of a specific property that belongs to the originator, which  leads straight to the definition of the memento pattern.

    All right, now that you hopefully learned how the originator class does its business, as well as how the corresponding caretaker interacts with it, it's time to jump forward and develop a hands-on example. This example will demonstrate in a friendly fashion how the previous "ArrayElementSelector" class is capable of maintaining the value of the "arrayIndex" property that corresponds to "ArrayProcessor."

    Do you want to see how this example will be created? Please visit the following section 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