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 theIterator
interface. The class has ahasNext()
method which delegates toenumeration.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 havestart()
,openWindow()
andbrake()
, but each of these methods will actually delegate to the engine, el.windows and braking system (see also this)