Chapter 10.  Accumulation

Prev don't be afraid of buying books Next

Chapter 10. Accumulation

A good deal of code in software systems accumulates information. The refactorings in this section target the improvement of code that accumulates information within an object or across several objects.

A Collecting Parameter [Beck, SBPP] is an object that visits methods in order to accumulate information from them. The visited methods may reside within one object or many objects. Each visited method provides the Collecting Parameter with information. When all relevant methods have been visited, the accumulated information can be obtained from the Collecting Parameter.

Move Accumulation to Collecting Parameter (313) is often used in conjunction with the refactoring Compose Method (123). The combination of these refactorings applies best when you have a long method that has many lines of code for accumulating information. To break the method into smaller parts, each of which handles a piece of the accumulation, you extract methods that accept and write to a Collecting Parameter.

The Collecting Parameter pattern resembles a Visitor [DP] in its ability to accumulate information from several objects. It differs from a Visitor in how it does the accumulation. Whereas visited objects pass themselves to a Visitor instance, objects that are visited by a Collecting Parameter simply call methods on the Collecting Parameter to provide it with information. If you have a lot of diverse information to accumulate from heterogeneous objects (i.e., objects with different interfaces), a Visitor will likely provide a cleaner design than a Collecting Parameter. That said, I frequently encounter code that could benefit from Move Accumulation to Collecting Parameter (313), while I infrequently encounter code that needs Move Accumulation to Visitor (320).

Unlike a Collecting Parameter, a Visitor is only useful for accumulating information from many objects, not one. It is also more applicable when you're accumulating information from heterogeneous objects, not homogeneous ones (i.e., objects that share the same interface). Because a Visitor is harder to implement than a Collecting Parameter, it's better to consider a Collecting Parameter solution before considering a Visitor one.

While the Visitor pattern is useful for certain types of accumulation, it is by no means limited to accumulation in what it can do. For example, a Visitor can visit an object structure and add objects to that structure during its journey. I use Visitors infrequently even for information accumulation, so I did not write about other refactorings to Visitor; they too are rare.

Amazon


Refactoring to Patterns (The Addison-Wesley Signature Series)
Refactoring to Patterns
ISBN: 0321213351
EAN: 2147483647
Year: 2003
Pages: 103

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net