473,701 Members | 2,959 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 2875
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
1733
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, Reflection etc? Somehow:
3
3144
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 design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
1
2556
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 implementation I'm htting a couple of quite large design considerations that I'm not sure I have the answare...
11
4303
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 becoming more available I am going to try the same thing I did in Java with PHP.
1
5553
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 want it to run as a normal application.
3
6126
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 grid, and the dataset stores the update. We haven't interjected any code which we can funnel through...
12
3034
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 operations added without being changed itself.
22
4720
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 Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
1
1961
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 and Delegates are proper how should I deal with differents of commands into Delegates and Events?...
0
8737
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8649
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9232
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9084
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8978
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8936
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7827
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4411
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.