Home arrow PHP arrow Page 3 - Introducing the Memento Pattern

Building the second piece of the memento pattern - PHP

PHP developers frequently need to maintain the state of an object across several web pages. There are a number of ways to do this. In this article, the first of two parts, you will learn how to handle this task with the memento pattern.

TABLE OF CONTENTS:
  1. Introducing the Memento Pattern
  2. Creating the first piece of the memento pattern
  3. Building the second piece of the memento pattern
  4. Implementing the memento pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
January 08, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: