What is the purpose of a delegation pattern?
Asked Answered
K

4

57

I was looking through the source to SensorManager in Android and found that when you register a SensorEventListener the SensorManager passes control of the listener to a ListenerDelegate.

I only bring this up as an example. I read the Wikipedia article on delegate programming but I am still not sure of its purpose. Why would one use a 'delegate'? How does it help the control flow of a program? What are the disadvantages of using (or not) one? Is it most practical for use with listeners?

Edit: ListenerDelegate is on line 487 and the methods in question are around line 1054.

Kaffir answered 23/8, 2011 at 23:24 Comment(1)
To reduce responsibilities of a class.Unbearable
C
80

Delegation is not exactly a 'design pattern' in the sense used in the GoF book. It is useful in a number of scenarios, and is a base for other patterns

  • when you want to perform some additional actions before/after you delegate (that's the Decorator pattern, but it's based on delegation). For example, Collections.synchronizedList(..) creates a new collection that delegates to the original one, but has its methods synchronized.
  • when you have incompatible interfaces and you want to adapt one to the other (the adapter pattern). You get the original object and delegate to it from methods that conform to the desired interface. For example, there's the EnumerationIterator class, that adapts enumerations to the Iterator interface. The class has a hasNext() method which delegates to enumeration.hasMoreElements()
  • when you want to hide some complexity from the user of your class, you can have methods that delegate to different actual workers. For example, a Car can have start(), openWindow() and brake(), but each of these methods will actually delegate to the engine, el.windows and braking system (see also this)
Cleliaclellan answered 23/8, 2011 at 23:29 Comment(3)
What is name for pattern defined in your third point (Car example).Cas
@EmptyData, it's the Facade pattern.Libnah
Talking about delegate, @Delegate lombok annotation on any field, make lombok generate delegate methods that forward the call to the fieldYovonnda
R
29

As per effective Java (by Joshua Bloch), composition is favorable over inheritance. Composition has several advantages over inheritance. One of the intuitions for this is as follows: Consider a sub-class which inherits from a base class. So any change in the base class will make the sub-class fragile as the sub-class depends on the base class. By using inheritance, we are making a binding on the sub-class to depend on the base class, which makes our code fragile. However by using composition, we can remove this limitation. Composition is done by establishing a 'has-a relationship' between classes instead of 'is-a' relationship as in inheritance. 'Delegate pattern' and 'Decorator pattern' both are examples of how composition can be achieved. You might want to read the chapter on 'composition vs inheritance' in the effective java book as it is quite informative.

For shorter explanation, you can refer to this article: http://javarevisited.blogspot.com/2013/06/why-favor-composition-over-inheritance-java-oops-design.html

Ricardo answered 24/5, 2015 at 14:37 Comment(0)
L
7

The delegate pattern is used to have someone else actually do the work, so, for example, in your example the SensorManager isn't going to know how to do what every listener wants, but you only want one program listening to the sensor.

So, there are listeners created by calling registerListener on the SensorManager, and these listeners are passed the information and then can decide what to do with the data from the sensors.

Lanceolate answered 23/8, 2011 at 23:30 Comment(0)
O
2

in a very simple sentence I can say: the main purpose of Delegation is to shields your objects from implementation changes to other objects in your software. read more here

Orderly answered 26/3, 2020 at 13:4 Comment(4)
the link is almost uselessMarva
i refers you to the bookOrderly
The point of stackoverflow is to have the answer here but not in a book which one has to pay for. Besides, I guess the book has many pages but you didn't mention the exact one to read or the exact paragraph supporting your answer - well, at least the one you claim it helps (but maybe it doesn't, that's your claim only).Marva
answer is there and for further reading you can download book for freeOrderly

© 2022 - 2024 — McMap. All rights reserved.