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

Creating two more command classes - 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

As I stated in the previous section, I'd like to extend the initial example by creating two additional command classes. These classes will control programmatically by different methods the way that elements of a specified array are displayed. Keeping in mind this condition, in the following code sample I defined the "ArrayToLowerCommand" and "ArrayToReverse" classes respectively, which not surprisingly will turn into upper cased strings all the elements of an input array, in addition to reversing these elements as well.

Please, take a look at the respective signatures for these new pair of classes:

// define concrete 'ArrayToLowerCommand' class (implements concretely 
the 'executeCommand()' method class ArrayToLowerCommand extends ArrayCommand{ public function executeCommand(){ $this->arrayCommanded->setLowercasedArray();           } } // define concrete 'ArrayToReverseCommand' class (implements concretely
the 'executeCommand()' method class ArrayToReverseCommand extends ArrayCommand{ public function executeCommand(){ $this->arrayCommanded->setReversedArray();    } }

In this case, the two new command classes listed above present signatures that are nearly identical to the previously defined "ArrayToUpperCommand" class. Of course, the only small difference rests on the definition of their respective "executeCommand()" methods, which perform distinct tasks, even when they're called the same.

Also, it's important to stress a relevant point with reference to all the command classes: notice how they use an instance of the commanded object to perform well-differentiated processes, but in all the cases, the corresponding methods encapsulate the complete logic for doing their business only into the mentioned commanders. Are you starting to see how this pattern works now? I bet you are.

Okay, at this stage you hopefully understand the principle that stands behind creating all these array command classes, but... what about defining the respective array commanded object?

Fortunately for you and me, the creation of commanded objects is indeed a no-brainer process that can be done with minor hassles, something that I'm going to show you in the next few lines. Therefore, if you're curious about how these types of objects will be defined, please click on the link below and keep reading.



 
 
>>> 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 3 - Follow our Sitemap

Dev Shed Tutorial Topics: