More Examples of Creating Command Objects with PHP 5 - Including all the classes in one hands-on example
(Page 5 of 5 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|