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

Introducing the Memento Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    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:
      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


    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


       · In this first part of this new series, the basics of the memento pattern are...
       · Great article - I really enjoyed this one!j
       · Hello Jon,Thank you for the kind comments on this PHP article. I really...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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