Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 wondering 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?

like image 368
Mann Avatar asked Dec 23 '10 05:12

Mann


People also ask

What is delegate pattern in Java?

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context.

What is the purpose of the delegate pattern?

The delegation pattern enables an object to use another “helper” object to provide data or perform a task rather than do the task itself. This pattern has three parts: An object needing a delegate, also known as the delegating object. It's the object that has a delegate.

How do command patterns work?

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

What is delegate pattern in Swift?

A design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. Swift Official Documentation.


1 Answers

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

like image 176
Bert F Avatar answered Oct 03 '22 08:10

Bert F