Home arrow PHP arrow Page 5 - More Examples of Creating Command Objects with PHP 5

Including all the classes in one hands-on example - PHP

Are you one of those PHP developers who wants to expand your background in design patterns by learning an additional one? If your answer is affirmative, then this series might be appealing to you. It will teach you, in three educational tutorials, how to create and implement command objects with PHP 5.

TABLE OF CONTENTS:
  1. More Examples of Creating Command Objects with PHP 5
  2. Building an array command class
  3. Creating two more command classes
  4. Building an array commanded class
  5. Including all the classes in one hands-on example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To show you how all the previously defined classes can be integrated into the same code sample, below I set up a simple yet illustrative script. It shows the neat functionality of the command pattern by using both the commanders and commanded objects that you learned before. Here is the mentioned script, along with its respective outputs:

try{
// instantiate 'ArrayCommanded' object
$arrayCommanded=new ArrayCommanded(array('Element 1',
'Element 2','Element 3','Element 4','Element 5')); // display input array after instantiating 'ArrayCommanded'
object print_r($arrayCommanded->getInputArray()); /* displays the following    Array ( [0] => Element 1 [1] => Element 2 [2] => Element 3 [3]
=> Element 4 [4] => Element 5 )            */ // instantiate 'ArrayToUpperCommand' object $arrayToUp=new ArrayToUpperCommand($arrayCommanded); // execute 'setUppercasedArray()' method $arrayToUp->executeCommand(); // displays input array after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following:            Array ( [0] => ELEMENT 1 [1] => ELEMENT 2 [2] => ELEMENT 3 [3]
=> ELEMENT 4 [4] => ELEMENT 5 ) */ // instantiate 'ArrayToLowerCommand' object $arrayToLow=new ArrayToLowerCommand($arrayCommanded); // execute 'setLowercasedArray()' method $arrayToLow->executeCommand(); // displays input array after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following: Array ( [0] => element 1 [1] => element 2 [2] => element 3
[3] => element 4 [4] => element 5 ) */ // instantiate 'ArrayToReverseCommand' object $arrayToRev=new ArrayToReverseCommand($arrayCommanded); // execute 'setReversedArray()' method $arrayToRev->executeCommand(); // displays data string after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following:         Array ( [0] => element 5 [1] => element 4 [2] => element 3 [3]
=> element 2 [4] => element 1 ) */ } catch(Exception $e){             echo $e->getMessage();             exit(); }

As you can see, the above example illustrates clearly how the command pattern can be applied by utilizing all the previously created classes. In addition, the prior script also displays the corresponding array elements according to the "array command" applied to them via the respective "ArrayCommanded" class.

As usual, feel free to tweak the source code of all the classes shown in this article. You can experiment with them and introduce your own approaches for implementing this handy pattern.

Final thoughts

That's all for the moment. In this second part of the series, I extended the application of the command pattern by defining some array processing classes, which came in very handy for demonstrating the functionality of the pattern in question.

However, this journey isn't finished yet. In the last tutorial, I'll show you how to apply the command pattern in a more useful fashion. You'll learn how to use it to work with self-saving objects. I'll meet you there!



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: