Skip to content

Commit 80ae1a9

Browse files
committed
docs: update collecting parameter
1 parent a33b324 commit 80ae1a9

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

collecting-parameter/README.md

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ category: Behavioral
44
language: en
55
tag:
66
- Accumulation
7+
- Data processing
8+
- Data transfer
79
- Generic
810
---
911

@@ -18,56 +20,43 @@ Aims to simplify methods that collect information by passing a single collection
1820

1921
## Explanation
2022

21-
### Real-world example
23+
Real-world example
2224

23-
Within a large corporate building, there exists a global printer queue that is a collection of all the printing jobs that are currently pending. Various floors contain different models of printers, each having a different printing policy. We must construct a program that can continually add appropriate printing jobs to a collection, which is called the *collecting parameter*.
25+
> Imagine a scenario in a restaurant where a waiter needs to take an order from a customer. Instead of noting down each item separately (e.g., appetizer, main course, dessert, drink), the waiter uses an order form that collects all the items into a single document. This order form simplifies the communication between the waiter and the kitchen staff by aggregating all the details into one place. Similarly, in software, the Collecting Parameter pattern aggregates multiple parameters into a single object, streamlining method calls and improving code readability and maintainability.
2426
25-
### In plain words
27+
In plain words
2628

27-
Instead of having one giant method that contains numerous policies for collecting information into a variable, we can create numerous smaller functions that each take parameter, and append new information. We can pass the parameter to all of these smaller functions and by the end, we will have what we wanted originally. This time, the code is cleaner and easier to understand. Because the larger function has been broken down, the code is also easier to modify as changes are localised to the smaller functions.
29+
> The Collecting Parameter pattern simplifies method calls by encapsulating multiple parameters into a single object.
2830
29-
### Wikipedia says
31+
Wikipedia says
3032

31-
In the Collecting Parameter idiom a collection (list, map, etc.) is passed repeatedly as a parameter to a method which adds items to the collection.
33+
> In the Collecting Parameter idiom a collection (list, map, etc.) is passed repeatedly as a parameter to a method which adds items to the collection.
3234
33-
### Programmatic example
35+
**Programmatic Example**
36+
37+
Within a large corporate building, there exists a global printer queue that is a collection of all the printing jobs that are currently pending. Various floors contain different models of printers, each having a different printing policy. We must construct a program that can continually add appropriate printing jobs to a collection, which is called the collecting parameter.
3438

3539
Coding our example from above, we may use the collection `result` as a collecting parameter. The following restrictions are implemented:
3640

37-
- If an A4 paper is coloured, it must also be single-sided. All other non-coloured papers are accepted
38-
- A3 papers must be non-coloured and single-sided
39-
- A2 papers must be single-page, single-sided, and non-coloured
41+
* If an A4 paper is coloured, it must also be single-sided. All other non-coloured papers are accepted
42+
* A3 papers must be non-coloured and single-sided
43+
* A2 papers must be single-page, single-sided, and non-coloured
4044

4145
```java
42-
package com.iluwatar.collectingparameter;
43-
44-
import java.util.LinkedList;
45-
import java.util.Queue;
46-
4746
public class App {
47+
4848
static PrinterQueue printerQueue = PrinterQueue.getInstance();
4949

50-
/**
51-
* Program entry point.
52-
*
53-
* @param args command line args
54-
*/
5550
public static void main(String[] args) {
56-
/*
57-
Initialising the printer queue with jobs
58-
*/
51+
// Initialising the printer queue with jobs
5952
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A4, 5, false, false));
6053
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A3, 2, false, false));
6154
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A2, 5, false, false));
6255

63-
/*
64-
This variable is the collecting parameter.
65-
*/
56+
// This variable is the collecting parameter.
6657
var result = new LinkedList<PrinterItem>();
6758

68-
/*
69-
* Using numerous sub-methods to collaboratively add information to the result collecting parameter
70-
*/
59+
// Using numerous sub-methods to collaboratively add information to the result collecting parameter
7160
addA4Papers(result);
7261
addA3Papers(result);
7362
addA2Papers(result);
@@ -79,6 +68,7 @@ We use the `addA4Paper`, `addA3Paper`, and `addA2Paper` methods to populate the
7968

8069
```java
8170
public class App {
71+
8272
static PrinterQueue printerQueue = PrinterQueue.getInstance();
8373

8474
/**
@@ -164,56 +154,46 @@ which is what we expected.
164154

165155
## Class diagram
166156

167-
![alt text](./etc/collecting-parameter.urm.png "Collecting Parameter")
157+
![Collecting Parameter](./etc/collecting-parameter.urm.png "Collecting Parameter")
168158

169159
## Applicability
170160

171-
Use the Collecting Parameter design pattern when
172-
173-
- When multiple methods produce a collection of results and you want to aggregate these results in a unified manner.
174-
- In scenarios where reducing the number of collections created by methods can improve memory efficiency and performance.
175-
- When you're refactoring large methods that perform multiple tasks, including the collection of results from various operations.
161+
* Use when a method needs to accept a large number of parameters, making the method signature unwieldy.
162+
* Use when the same group of parameters is passed to multiple methods, reducing redundancy and potential errors.
163+
* Use to improve the readability and maintainability of the code.
176164

177165
## Tutorials
178166

179-
Tutorials for this method are found in:
180-
181-
- [Refactoring To Patterns](http://www.tarrani.net/RefactoringToPatterns.pdf) by Joshua Kerivsky
182-
- [Smalltalk Best Practice Patterns](https://ptgmedia.pearsoncmg.com/images/9780134769042/samplepages/013476904X.pdf) by Kent Beck
167+
* [Refactoring To Patterns (Joshua Kerivsky)](http://www.tarrani.net/RefactoringToPatterns.pdf)
168+
* [Smalltalk Best Practice Patterns (Kent Beck)](https://ptgmedia.pearsoncmg.com/images/9780134769042/samplepages/013476904X.pdf)
183169

184170
## Known uses
185171

186-
Joshua Kerivsky gives a real-world example in his book 'Refactoring to Patterns'. He gives an example of using the Collecting Parameter Design Pattern to create a `toString()` method for an XML tree. Without using this design pattern, this would require a bulky function with conditionals and concatenation that would worsen code readability. Such a method can be broken down into smaller methods, each appending their own set of information to the collecting parameter. See this in [Refactoring To Patterns](http://www.tarrani.net/RefactoringToPatterns.pdf).
187-
188-
Other examples include:
189-
190-
- Aggregating error messages or validation failures across a complex validation process.
191-
- Collecting elements or information while traversing a complex data structure.
192-
- Refactoring complex reporting functionalities where various parts of a report are generated by different methods.
172+
* Use when a method needs to accept a large number of parameters, making the method signature unwieldy.
173+
* Use when the same group of parameters is passed to multiple methods, reducing redundancy and potential errors.
174+
* Use to improve the readability and maintainability of the code.
193175

194176
## Consequences
195177

196178
Benefits:
197179

198-
- Reduces code duplication by centralizing collection handling in a single place.
199-
- Enhances clarity and maintainability by making it explicit where and how results are collected.
200-
- Improves performance by minimizing the creation and management of multiple collection objects.
180+
* Improves code readability by reducing the number of parameters in method signatures.
181+
* Facilitates the reuse of parameter sets across different methods.
182+
* Enhances maintainability by centralizing the parameter structure.
201183

202184
Trade-offs:
203185

204-
- Increases coupling between the caller and the methods being called since they must agree on the collection to use.
205-
- May introduce side effects in methods if not carefully managed, as methods are no longer self-contained in their result handling.
186+
* Introduces an additional class, which may increase complexity if not managed properly.
187+
* Can lead to over-generalization if the parameter object becomes too large or unwieldy.
206188

207189
## Related patterns
208190

209-
- [Composite](https://java-design-patterns.com/patterns/composite/): Can be used in tandem with Collecting Parameter when dealing with hierarchical structures, allowing results to be collected across a composite structure.
210-
- [Visitor](https://java-design-patterns.com/patterns/visitor/): Often used together, where Visitor handles traversal and operations on a structure, and Collecting Parameter accumulates the results.
211-
- [Command](https://java-design-patterns.com/patterns/command/): Commands may utilize Collecting Parameter to aggregate results from multiple operations executed by the command objects.
191+
* [Command](https://java-design-patterns.com/patterns/command/): Commands may utilize Collecting Parameter to aggregate results from multiple operations executed by the command objects.
192+
* [Composite](https://java-design-patterns.com/patterns/composite/): Can be used in tandem with Collecting Parameter when dealing with hierarchical structures, allowing results to be collected across a composite structure.
193+
* [Visitor](https://java-design-patterns.com/patterns/visitor/): Often used together, where Visitor handles traversal and operations on a structure, and Collecting Parameter accumulates the results.
212194

213195
## Credits
214196

215-
- [Refactoring To Patterns](http://www.tarrani.net/RefactoringToPatterns.pdf) by Joshua Kerivsky
216-
- [Smalltalk Best Practice Patterns](https://ptgmedia.pearsoncmg.com/images/9780134769042/samplepages/013476904X.pdf) by Kent Beck
217-
- [Wiki](https://wiki.c2.com/?CollectingParameter)
218-
- [Refactoring: Improving the Design of Existing Code](https://amzn.to/3TVEgaB)
219-
- [Clean Code: A Handbook of Agile Software Craftsmanship](https://amzn.to/4aApLP0)
197+
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://amzn.to/4aApLP0)
198+
* [Refactoring: Improving the Design of Existing Code](https://amzn.to/3TVEgaB)
199+
* [Collecting Parameter (WikiWikiWeb)](https://wiki.c2.com/?CollectingParameter)

0 commit comments

Comments
 (0)