PHP
  Home arrow PHP arrow Page 2 - 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 - Creating the first piece of the memento pattern


    (Page 2 of 4 )

    As I clearly explained in the introduction of this article, when it comes to applying the memento design pattern it's necessary to define two classes, called the originator and the caretaker respectively. In this tutorial, I'm going to demonstrate how to use the pattern by creating the originator first, and then the caretaker.

    In this case, the functionality housed within the originator will involve traversing an array structure, which will be directly inputted into the class via its constructor.

    Having explained briefly how the originator class will work, take a look at its corresponding definition. It is as follows:

    // define 'ArrayProcessor' class (in this case, this is the Originator class)
    class ArrayProcessor{
       private $inputArray;
       private $arrayIndex;
       // set input parameters
       public function __construct($inputArray,$arrayIndex=0){
         if(!is_array($inputArray)){
           throw new Exception('Invalid input array!');
         }
         if($arrayIndex<0||$arrayIndex>(count($inputArray)-1)){
           throw new Exception('Invalid index value for input array!');
         }
         $this->setInputArray($inputArray);
         $this->setArrayIndex($arrayIndex);
       }
       // set input array
       public function setInputArray($inputArray){
         $this->inputArray=$inputArray;
       }
       // fetch input array
       public function getInputArray(){
         return $this->inputArray;
       }
       // set value for 'arrayIndex' property
       public function setArrayIndex($arrayIndex){
         $this->arrayIndex=$arrayIndex;
       }
       // get value of 'arrayIndex' property
       public function getArrayIndex(){
         return $this->arrayIndex;
       }
       // fetch array element according to array index
       public function getArrayElement(){
         if(!$elem=$this->inputArray[$this->arrayIndex]){
           throw new Exception('Error fetching element of input array!');
         }
         return $elem;
       }
    }

    As you'll realize, the signature of the above "ArrayProcessor" class is pretty straightforward. In this case, the class has been defined as the originator. It presents some simple methods for traversing a given array via an index parameter, which is also inputted through the respective constructor.

    Aside from describing the core tasks performed by the aforementioned originator class, there's not much to be said about it (at least for the moment). However, don't feel disappointed, since this is only the first building block of the memento pattern.

    As you learned before, there's another class involved in this schema: the caretaker class. In the next section I'm going to create this new class and show you how it can hold a specific state of the previous originator. Please click on the link below 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 3 hosted by Hostway
    Stay green...Green IT