Aggregate pattern: Difference between revisions

Content deleted Content added
Exsuscito (talk | contribs)
Modified grammar of description, still not certain how to clarify description yet.
simplify
 
(19 intermediate revisions by 15 users not shown)
Line 1:
{{Short description|Concepts in statistics and computer science}}
{{split}}
{{one source |date=March 2024}}
 
An '''Aggregate pattern''' can refer to concepts in either statistics or computer programming. Both uses dealsimplify withcomplexity considering a large case as composed ofinto smaller, simpler, piecesparts.
 
== Statistics ==
 
An aggregate pattern is an important statistical concept in many fields that rely on [[statistics]] to predict the behavior of large groups, based on the tendencies of subgroups to consistently behave in a certain way. It is particularly useful in [[sociology]], [[economics]], [[psychology]], and [[criminology]].
 
== Computer programming ==
 
In ''[[Design Patterns]]'', an aggregate is not a [[Software design pattern|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]].
{{Expand-section|a clear explanation of the code example or adding another code example|date=October 2007}}
<syntaxhighlight lang="python">
def fibonacci(n: int):
a, b = 0, 1
count = 0
while count < n:
count += 1
a, b = b, a + b
yield a
 
for x in fibonacci(10):
In [[computer programming]], an aggregate pattern is a [[design pattern (computer science)|design pattern]].
print(x)
 
def fibsum(n: int) -> int:
Members of a common [[Subclass (computer science)|subclass]] are each known to have certain [[Method (computer science)|method]]s. These methods return information about the state of that particular [[object (computer science)|object]]. Sometimes an application is concerned with an aggregation, or amalgamation, of data from several objects of the same type. This leads to redundant code in various places throughout the program:
total = 0
for x in fibonacci(n):
total += x
return 1;total
 
def fibsum_alt(n: int) -> int:
(Perl example)
"""
<source lang="perl">
Alternate implementation. demonstration that Python's built-in function sum()
my $subtotal;
works with arbitrary iterators.
foreach my $item (@cart) {
"""
$subtotal += $item->query_price();
return sum(fibonacci(n))
}
my $weight;
foreach my $item (@cart) {
$weight += $item->query_weight();
}
# and so on
</source>
 
myNumbers = [1, 7, 4, 3, 22]
Aggregation uses a [[wrapper pattern|wrapper]] to encapsulate as much duplicate functionality across methods as possible:
 
def average(g) -> float:
<source lang="perl">
return float(sum(g)) / len(g) # In Python 3 the cast to float is no longer be necessary
package Cart::Basket;
</syntaxhighlight>
Python hides essentially all of the details using the [https://docs.python.org/3/library/stdtypes.html#iterator-types 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.
@ISA = qw(Cart::Item);
sub query_price {
my $self = shift;
my $contents = $self->{contents};
foreach my $item (@$contents) {
}
}
# other query_ routines here...
sub add_item {
my $self = shift;
my $contents = $self->{contents};
my $item = shift; $item->isa('Cart::Item') or die;
push @$contents, $item;
return 1;
}
</source>
 
Aggregation is like iteration in that they both present information gleaned from a number of objects through a tidy interface in one object.
 
== See also ==
 
* [[Visitor pattern]]
* [[Template class]]
Line 64 ⟶ 47:
* [[Type safety]]
* [[Functional programming]]
 
== References ==
{{Reflist}}
 
[[Category:Software design patterns]]
[[Category:Articles with example PerlPython (programming language) code]]
 
 
{{statistics-stub}}