Overview
The class that only performs delegation work to other classes is called a Middle Man. This is the opposite of the Message Chains. Encapsulation (hiding internal details) in the world of Object-Oriented Programming is a typical pattern [1]. However, the problem arises when it goes too far - Fowler specified that it could be said that it’s terrible when half of the methods are delegators [2]. Mäntylä wrote that this is a problem when every time a new method has to be created, it requires the delegators to be modified with them [1].
Causation
This can happen due to over-zealous refactorization of Message Chains.
Problems
Without proper, meaningful, unambiguous naming for the delegation method, the developer might need to check what is precisely being called to be sure.
Holding references instead of actual values might slightly increase project complexity in volume.
Example
1class Minion:
2 _location: Location
3
4 def action(self):
5 ...
6 if self.is_frontline():
7 ...
8
9 def is_frontline(self)
10 return self._location.is_frontline()
11
12
13class Location:
14 _field: Field
15
16 def is_frontline(self)
17 return self._field.is_frontline()
18
19
20class Field:
21 def is_frontline(self)
22 ...Exceptions
In the context of the system as a whole, some communication between modules must take place. All possibilities should be properly balanced so that none of the smells dominate (Global Data, Tramp Data, Message Chain, Middle Man) to make the entire codebase as straightforward as possible.
Refactoring
- Remove Middle Man
- Inline Method
- Replace Delegation with Inheritance
- Replace Superclass with Delegate
Sources
- ORIGIN1999 · ISBN 978-0201485677