Java Command Pattern vs iPhone Delegate Pattern
Hi I am a java developer and these days I have also started working on iphone development. I was wonderin开发者_运维技巧g that Java's command pattern is somewhat similar to delegate pattern in iphone or vice-versa because in both there is somebody else doing work for assignee. Can somebody enlighten me on this?
Strictly speaking, they are different, although complementary.
Command pattern
- this is the encapsulation of an operation/request/action as an object. Although a common reason to encapsulate an operation as an object would be to send it off to a delegate, there are a variety of other reasons you might want to use the command pattern, e.g. to create an undo mechanism or to write a audit log.
Delegate pattern
- this is having one object shadow behind another in order to support it. Its usually a 1-to-1 relationship. In iPhone, a Window might have a Window delegate. The window might ask the Window delegate how to respond to certain user actions. If the different user actions were self-contained objects, then they might be an example of the command pattern, but this is a separate idea from the Window delegate.
From what I understand (which is, admittedly, very little), the delegation in the Cocoa model does not use command objects between the delegator and delegatee (although a method invocation is sometimes called a "message" on OO speak as if it were an actual entity).
The two ideas are complementary and are often used together, but they are separate ideas. You can use the command pattern without delegates and delegates without command objects.
See:
http://en.wikipedia.org/wiki/Command_pattern
http://en.wikipedia.org/wiki/Delegation_pattern
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18
精彩评论