0

Given the following class:

public class CardGame extends Game {
    private CardDeck[] cardDecks;
    public CardGame(int numCardDecks) {
        super();
        cardDecks = new CardDeck[numCardDecks];
        for (int i=0; i < numCardDecks; i ++) {
            cardDecks[i] = new CardDeck();
        }
    }
}

Which UML relationship best describes this class? (And why?)
- Aggregation - Composition - Generalization - Factory

Note: I think this single-choice test problem itself is not clearly defined.

BJYC
  • 354
  • 2
  • 10
  • Can you explain why you want to know that? Aggregation and composition add very little semantics to a model and only in few circumstances. – qwerty_so Aug 03 '16 at 08:02
  • @YuChen, this looks like homework. What do you think is the answer and why? – jaco0646 Aug 03 '16 at 14:22
  • @jaco0646 Very close, actually a problem in a commercial mock test for a Java professional certificate. I was disputing the answer, Generalization, and found myself in line with other professions here. Nothing really complicated, feedback(s), for a couple of other problems with wrong answers, I believe, had been submitted. – BJYC Aug 03 '16 at 17:30
  • @jaco0646 not a homework, but from a coding/certificate mock exam. I would agree with the answers here so far, they are very well explained. – BJYC Dec 15 '17 at 00:11

2 Answers2

3

Aggregation, Composition and Generalization are UML (class diagram) notations which represent different types of relationships i.e. types of logical connections.

In your case 'Game' is a generalization of 'CardGame'; 'CardGame is a specialization of 'Game'. I want to say that in your case the 'CardDecks' have a Composition relationship to your 'Cardgame' since your cards are created in the 'CardGame' class and would be deleted if you delete the 'CardGame' i.e. "implies a relationship where the child cannot exist independent of the parent" (What is the difference between aggregation, composition and dependency?). But if you store the specific 'CardDecks' in a database, or if you are trying to model the real world in which you can use the cards in another game then it is Aggregation. Your CardDeck class is a 'factory method' since it is a class which creates objects.

I don't think this should be classified as a design pattern since for it to be a design pattern it has to be describing recurring solutions to common problems in software design.

"In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations." (https://sourcemaking.com/design_patterns)

Community
  • 1
  • 1
Kriss
  • 326
  • 1
  • 3
  • 12
3

Game generalises CardGame as CardGame is derived from it.

I'd argue that cardDecks is a composition of CardDeck as CardGame controls their lifetime and, within the context of this code, they can only belong to that CardGame.

David Osborne
  • 6,436
  • 1
  • 21
  • 35
  • Ehm... You wanted to say Game is generalization of CardGame, didn't you? Notice, the empty triangle arrow should go to Game block. – Gangnus Aug 03 '16 at 12:30
  • Sure. Maybe the correct language is '`Game` generalises `CardGame`'? – David Osborne Aug 03 '16 at 14:58
  • The arrow is called "Generalization". So, of course, your variant is more readable and understandable, but the questioner tries to understand the UML slang... So, both variants are OK up to me. Now your answer is correct, short and full - the ideal variant. – Gangnus Aug 03 '16 at 16:02