Aggregation -An aggregate is a class where some of the attributes are themselves objects.
-If all attributes are primitive, the class is not an aggregate.
e.g. public class BankProducts
{
private BankAccount savings;
private BankAccount chequing;
private Loan homeMortgage;
private Loan lineOfCredit;
}
- a composition is an aggregation in which the objects inside the class "live and die" with the object. In effect, it is created when the object is created and destroyed when the object is destroyed.
Aggregate: only holds references to external objects. These objects exist apart from the aggregate.
Composition: locally creates its own objects. Objects are only relevant to the object holding them.
External forces can generally change the value of an object in an aggregate. This is good for an aggregate, bad for a composition.
We don't want external forces messing with the internal structure of a composition.
Aggregate Constructors -Aggregates keep references to external objects. They don't generally make local copies.
e.g. public class Investment
{
public class Investment(Stock s, int num)
{
this.s = s;
this.num = num;
}
}
Composition Constructor-Objects in a composition reflect the internal workings of an object.
-Local versions, not accessible from the outside, should be kept.
-Extends the concept of private access type.
public class CompositionWithStock
{
private Stock s;
Public ComparisonWithStock(Stock s, int num)
{ //create a local copy of s and store it
this.s = new Stock(s);
this.num = num
}