Aggregate pattern: Difference between revisions

Content deleted Content added
No edit summary
Line 8:
 
In [[Design Patterns]], an aggregate is not a design pattern but rather refers to an object such as a list, vector, or generator which provides an interface for creating [[iterator]]s. The following example code is in [[Python (programming language)|Python]].
<source lang="python">
<pre>
mylist = ['apples','oranges','milk','eggs','flour']
 
Line 32:
 
def fibsum_alt(n):
"""
"Alternate implementation. demonstration that Python's built-in function sum() works with arbitrary iterators"
works with arbitrary iterators
"""
return sum(fibonacci(n))
 
Line 39 ⟶ 42:
def average(g):
return float(sum(g))/len(g) #in Python 3.0 the cast to float will no longer be necessary
</source>
 
</pre>
 
Python hides essentially all of the details using the [http://www.python.org/doc/lib/typeiter.html iterator protocol]. Confusingly, [[Design Patterns]] uses "aggregate" to refer to the blank in the code <code>for x in ___:</code> which is unrelated to the term "aggregation" <ref>[[Design Patterns]], p. 22: "Aggregation implies that one object owns or is responsible for another object. ... Aggregation implies that an aggregate object and its owner have identical lifetimes."</ref>. Neither of these terms refer to the statistical aggregation of data such as the act of adding up the Fibonacci sequence or taking the average of a list of numbers.