delegation | - Create a method in the «Delegator» class that does only one thing: it calls a method in a neighbouring «Delegate» class, allowing reuse of the method for which the «Delegate» has responsibility
- By 'neighbouring', we mean the «Delegate» is connected to the «Delegator» by an association.
- Normally, in order to use delegation an association should already exist between the «Delegator» and the «Delegate».
- This association needs only to be unidirectional from «Delegator» to «Delegate».
- However it may sometimes be appropriate to create a new association just so you can use delegation - provided this does not increase the overall complexity of the system.
| Adapter and Proxy patterns | | - You want to minimize development cost and complexity by reusing methods
- You want to reduce the linkages between classes
- You want to ensure that work is done in the most appropriate class
| How can you most effectively make use of a method that already exists in the other class? | A pattern in which one procedure does nothing more than call another in a neighbouring class; this reduces total coupling in the system | - Overusing generalization and inheriting the method that is to be reused
- Instead of creating a single method in the «Delegator» that does nothing other than call a method in the «Delegate», you might consider having many different methods in the «Delegator» call the delegate's method? It would be better to ensure that only one method in the «Delegator» calls the method in the «Delegate». If many methods access the method in the «Delegate», then when it changes, you may have to change every method that accesses it
- a method must only accesses neighbouring classes
| 6.7 - The Delegation Pattern | | design pattern |