What is the purpose of a delegation pattern?

JavaAndroidDesign PatternsDelegation

Java Problem Overview


I was looking through the source to http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/hardware/SensorManager.java;h=f079e42a9993ddd57303bb19fd6462db48ae5345;hb=HEAD">SensorManager</a> 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 http://en.wikipedia.org/wiki/Delegation_pattern">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.

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionahodderView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaRookieView Answer on Stackoverflow
Solution 3 - JavaJames BlackView Answer on Stackoverflow
Solution 4 - JavaAlireza Rahmani khaliliView Answer on Stackoverflow