Question

I am trying to learn UML concepts and their application in Java programming. I am aware of association, shared aggregation and composition concepts. What I am confused about is how they are applied to a particular scenario. e.g. if we have a set of three classes: Book, Author and Publisher, how can we apply these concepts.

Book.java

class Book {

private String ISBN;
private String category;
private Publisher publisher;
private ArrayList<Author> authors;  
}

Author.java

class Author {

private String authorName;
private String phoneNo;
private String email;
private ArrayList<Book> books;
}

Publisher.java

class Publisher {

private String publisherName;
private String publisherAddress;
private ArrayList<Book> books;
}

Are these mere associations? Can we say that the relation between Publisher and Book is of shared aggregation? What about the many-many relation between Book-Author?

To me it appears that Publisher-Book is a shared aggregation, Book-Author is a simple association. And if there will be a Chapter class, related to a Book, that would be composition. Am I right in my understanding?

Was it helpful?

Solution

Association, Aggregation, and Composition are all "has a" relationships.

Association means simply that two classes are associated with each other, but instances of each can exist independently of one another. Your example shows no simple associations.

Aggregation is a special association where one class houses a collection of another class. Authors have many Books, Publishers have many Books, etc.

Composition is a special aggregation that implies a strong life-cycle tie. In other words, the composed class generally cannot exist outside of the parent. For example, A Map.Entry cannot exist without the Map. You have no examples of this above.

You should also be aware that "composition" as it pertains to UML is much stricter/narrower than in Java.

OTHER TIPS

According to the UML Superstructure Specification, v2.4.1 (read below), "share aggregation" is not specially defined in UML. So whether it is shared or not depends on the model and whether it is composite or not depends on how the existences are managed.

"AggregationKind is an enumeration of the following literal values:

none - Indicates that the property has no aggregation. shared - Indicates that the property has a shared aggregation. composite - Indicates that the property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (parts).

Semantic Variation Points - Precise semantics of shared aggregation varies by application area and modeler."

enter image description here

While there is a many-to-many association between Book and Author, there is a many-to-one association between Book and Publisher.

In your Java classes you have been using two pairs of mutually inverse reference properties Book::authors and Author::books as well as Book::publisher and Publisher::books. This is the pattern for implementing bi-directional associations.

For more about associations, see Section 1 of my tutorial about associations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top