Problem: An API call gets huge, with TooManyParameters and/or the need to change the signature frequently during development. Most invocations use default arguments. Function overloading is unavailable or considered undesirable. Or, it is desired to expand an interface without breaking existing code. Or, you need to return multiple values and don't have reference parameters available.
Context: API development, especially for a complex class/system/function.
Forces: Need to not have to type in huge argument lists; need to aid refactoring, need to decrease coupling between a function/method and its callers.
Proposed Solution: Replace the LongParameterList with a ParameterObject; an object or structure with data members representing the arguments to be passed in.
Performance penalty; the ParameterObject will not be cached in resources. Creating it may require additional time and may further abuse the garbage collector.
If not careful, can leave parameters in an undefined state (depending on language).
In some older languages, creating the ParameterObject may require more typing (and more programmer frustration/error) than just specifying an argument list.
Design Rationale: Decoupling systems.
Related Patterns:AntiPattern you should beware of:MagicContainer. Similar to ParameterObject, but uses a generic associative container (such as a HashTable) to contain the parameters. Much slower, and you have to handle missing or superfluous parameters. (On the other hand, coupling is further reduced, and no new class need be written).
Pattern Category:CategoryStructuralPatternsAlso Known As:Examples in the Literature: Examples in Practice:
X11 Graphics Context objects are examples of this pattern, using a GC to represent the equivalent of a whole mass of parameters which are of value to various drawing primitives, including fonts, colors, line thicknesses, etc.
File handles in Unix maintain file state such as the "current read/write pointer." Passing a handle of this nature can eliminate the need to manually maintain this state.