473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The Command Design Pattern in PHP 5

New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern
Examples - the Command Pattern.

Since you all enjoyed the Visitor Pattern so much yesterday, today I
have the Command Pattern for you. This one is pretty straight
forward. In the Command Pattern an object encapsulates everything
needed to execute a method in another object.

http://www.fluffycat.com/SDCMSv2/PHP...terns-Command/
Nov 30 '05 #1
4 2847
FluffyCat wrote:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern
Examples - the Command Pattern.

Since you all enjoyed the Visitor Pattern so much yesterday, today I
have the Command Pattern for you. This one is pretty straight
forward. In the Command Pattern an object encapsulates everything
needed to execute a method in another object.

http://www.fluffycat.com/SDCMSv2/PHP...terns-Command/


Firstly, a typo - you have references to $plainVisitor, which is from
another example!!

Also, I'd recommend changing the name of BookCommandee to Book, because
that's all it is sematically.

Also, it would be more demonstrative if the logic to insert/remove the
stars was in the BookStarsOnCommand and BookStarsOffCommand classes,
i.e. remove BookCommandee::setStarsOn() and
BookCommandee::setStarsOff().

Otherwise, it begs the question "why not just call $book->setStarsOn()
from testCommand.php?".

--
Oli

Nov 30 '05 #2
On 30 Nov 2005 08:42:45 -0800, "Oli Filth" <ca***@olifilth.co.uk>
wrote:
FluffyCat wrote:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern
Examples - the Command Pattern.

Since you all enjoyed the Visitor Pattern so much yesterday, today I
have the Command Pattern for you. This one is pretty straight
forward. In the Command Pattern an object encapsulates everything
needed to execute a method in another object.

http://www.fluffycat.com/SDCMSv2/PHP...terns-Command/


Firstly, a typo - you have references to $plainVisitor, which is from
another example!!

Also, I'd recommend changing the name of BookCommandee to Book, because
that's all it is sematically.

Also, it would be more demonstrative if the logic to insert/remove the
stars was in the BookStarsOnCommand and BookStarsOffCommand classes,
i.e. remove BookCommandee::setStarsOn() and
BookCommandee::setStarsOff().

Otherwise, it begs the question "why not just call $book->setStarsOn()
from testCommand.php?".


Good catch on the typo, thanks! PHP saw that as resolving to NULL,
and so not there, and thus allowed a parameter where one should not
be. Interesting.

I have BookCommandee named as it is because other examples I have on
the site already use the plain vanilla Book class. So, I had to give
it a name other than Book.php in order to have similar classes in the
same directory. You are of course correct, Book would do the trick
naming wise if it didn't conflict with my other examples.

The design of the Command pattern, which of course I did not design,
is that the Command class executes an operation of another class. As
I understand it, the Command class shouldn't contain or enhance the
operation.

The key word for the Command pattern is encapsulation. You would use
this if you wanted to allow access to one specific function in one
specific object, but not necessarily give any additional access to the
object. This pattern is used, for example, in Java's EJBs.

Perhaps my example doesn't emphasize this because testCommand creates
BookCommandee and BookStarsOnCommand, and then goes on to use both.
Perhaps if after creating BookStarsOnCommand it passed it to another
class which could call only BookStarsOnCommand, but not access
BookCommandee, it would make the intent clearer.

Again, thanks for checking it out and catching that typo!
Nov 30 '05 #3
FluffyCat said the following on 30/11/2005 18:46:
On 30 Nov 2005 08:42:45 -0800, "Oli Filth" <ca***@olifilth.co.uk>
wrote:
FluffyCat wrote:
http://www.fluffycat.com/SDCMSv2/PHP...terns-Command/
Firstly, a typo - you have references to $plainVisitor, which is from
another example!! Good catch on the typo, thanks! PHP saw that as resolving to NULL,
and so not there, and thus allowed a parameter where one should not
be.
You should turn error-checking up to E_ALL, perhaps!

Also, it would be more demonstrative if the logic to insert/remove the
stars was in the BookStarsOnCommand and BookStarsOffCommand classes,
i.e. remove BookCommandee::setStarsOn() and
BookCommandee::setStarsOff().

Otherwise, it begs the question "why not just call $book->setStarsOn()
from testCommand.php?".

The design of the Command pattern, which of course I did not design,
is that the Command class executes an operation of another class. As
I understand it, the Command class shouldn't contain or enhance the
operation.

The key word for the Command pattern is encapsulation.


But in your example, it doesn't encapsulate anything...

You would use
this if you wanted to allow access to one specific function in one
specific object, but not necessarily give any additional access to the
object.

In that instance, having Book implement an interface would make more
sense than having to define and instantiate a separate Commander class
which does nothing more than pass method calls...

Perhaps my example doesn't emphasize this because testCommand creates
BookCommandee and BookStarsOnCommand, and then goes on to use both.
Perhaps if after creating BookStarsOnCommand it passed it to another
class which could call only BookStarsOnCommand, but not access
BookCommandee, it would make the intent clearer.


There's little point having an example of something if it doesn't
demonstrate its benefits and/or a sensible use!

To improve your example, you might demonstrate the benefits of treating
the Commanders polymorphically. e.g. along the lines of:
interface Command
{
function do();
}

class Eat implements Command
{ ... }

class Sleep implements Command
{ ... }

class GoToShops implements Command
{ ... }
function makeStuffHappen(Command $command)
{
$command->do();
}
$obj = new ObjectToBeControlled();

$commands = array(new Eat($obj), new Sleep($obj), new GoToShops($obj));

foreach ($commands as $cmd)
{
makeStuffHappen($cmd);
}

--
Oli
Nov 30 '05 #4

Good suggestions.

I added a function to testCommand which is only passed an instance of
Command. The function then calls $command_in->execute(). Hopefully
this will better illustrate that with the command pattern an instance
of Command encapsulates a call to another class.
Dec 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Abraham Khalil | last post by:
With all the Menu items, toolbar items and popup items that can total to about 50-75 choices in a gui design, what the 'easiest' way to map the action names with the class to call. Like to use...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
1
by: TEK | last post by:
Hello I'm wondering if anyone out there might give some input/suggestions/viewpoints around the Command pattern. In my case, the number one priority for using the pattern is undo support. Some...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
1
by: Peter Rilling | last post by:
I have an EXE that I would like to be able to run from either the command-line or as a windows service. Is there a way that I can tell which context the program is running in? Basically, if it...
3
by: Samuel R. Neff | last post by:
We're working on implementing a Command Pattern design with our application in order to help facilitate undo/redo. This is fine four user actions we control which basically means menu actions. ...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
1
by: Reg | last post by:
Hello, I have a situation where a text based protocol comes to my component to be handled. Protocol has differnt commands and they have different arguments. The question is that which is in C#...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.