|
Posted by FluffyCat on 10/27/69 11:33
On 30 Nov 2005 08:42:45 -0800, "Oli Filth" <catch@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-Design-Patterns-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!
Navigation:
[Reply to this message]
|