I am trying to understand the difference between Composition and Aggregation a little better. I have read many questions on here like this one: http://ift.tt/2zRKbD2
Say a Customer is allocated offers when they register with a website.. Please see the code below:
public class Customer
{
public Guid id {get; set; }
public string name {get; set; }
public DateTime DateOfBirth {get; set; }
public List<Offer> Offers {get; set; }
public void GetOffers(List<Offer> AllOffers)
{
//Loop through AllOffers and assign the offers the person
//is entitled to based on Age etc
}
}
In the example above I use composition. Alternatively I could do this (Aggregation):
public class Customer
{
public Guid id {get; set; }
public string name {get; set; }
public DateTime DateOfBirth {get; set; }
public List<Offer> GetOffers(List<Offer> AllOffers, List<Offer> MyOffers)
{
//Loop through AllOffers and assign the offers the person
//is entitled to based on Age etc
}
}
I believe I should be using composition (first example) because if a customer is deleted then the offers they are entitled to are deleted. Have I understood the difference between Aggregation and composition correctly? The reason I ask is because the Offer itself can link to many customers.
Aucun commentaire:
Enregistrer un commentaire