Reputation: 27375
I'm trying to understand object oriented approach by the following simple example:
I have the class:
public class TotalRestiction{
private Restriction r;
public add(RestictionItem i){
//updating the Restriction field
//depends on the RestrictionItem
}
private static class Restriction{
//...
}
public Collection<Object> getObjects(){
//...
}
}
public interface RestrictionItem{
//...
}
It's not clear to me what is the exact relationship between those classes. Is it aggregation or association? I tend to say that it's aggregation, because the TotalRestriction
aggregates the RestrictionItem
s, encapsulated within the Restriction
static nested class. But I'm not pretty sure about that.
Upvotes: 2
Views: 77
Reputation: 766
If you are instantiating class TotalRestriction as:
TotalRestriction tr=new TotalRestriction(new Restriction());
and in TotalRestriction class constructor is as :
class TotalRestriction{
Restriction r;
public TotalRestriction(Restriction r){
this.r=r;
}
}
then it is Aggregation
if you are using like this:
class TotalRestriction{
Restriction r;
public TotalRestriction(){
r=new Restriction();
}
}
then it is Association because class TotalRestriction uses service of class Restriction .
Upvotes: 0
Reputation: 53462
Association would be when it would use another class, which does not have to be encapsulated. Composition would be when your class has a has-a relationship with those classes, e.g. TotalRestriction has-a RestrictionItems. With Aggregation the class also has a has-a relationship, but the other classes also exist independently of the parent class, which is not the case with Composition..
In your case, I'd say Restriction class has a aggregation relationship towards RestrictionItem, and TotalRestriction has a composition relationship towards Restriction.
Upvotes: 1