473,574 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2863
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 BookStarsOnComm and and BookStarsOffCom mand 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 BookStarsOnComm and and BookStarsOffCom mand 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 BookStarsOnComm and, and then goes on to use both.
Perhaps if after creating BookStarsOnComm and it passed it to another
class which could call only BookStarsOnComm and, 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 BookStarsOnComm and and BookStarsOffCom mand 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 BookStarsOnComm and, and then goes on to use both.
Perhaps if after creating BookStarsOnComm and it passed it to another
class which could call only BookStarsOnComm and, 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 ObjectToBeContr olled();

$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
1730
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 the Command design pattern to do this generically: Somehow get the "action string" and map it to a "command class". Is this possible with Command,...
3
3131
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 from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This...
1
2543
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 now, a lot more in the future. I think I have a pretty good understanding about how the command pattern is tought to work, however during...
11
4291
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. They are still online at http://www.fluffycat.com/java/patterns.html Since September 2003 I've mainly been using PHP, and now that PHP 5 is...
1
5543
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 runs as a service, I would want it to call ServiceBase.Run (which will then call the execution logic). If it is run at the command-line, I simply...
3
6120
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. However, it's not quite so clear for bound controls. We don't directly interact with the change--the user makes a change in a control, such as a...
12
3027
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 itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new...
22
4703
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. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via...
1
1949
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# world's way to handle different command a J2EE style Cpmmand Pattern Design Pattern or C#'s Events and Delegates? Any opinions of that? If Events...
0
7826
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8085
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.