How do I make an aggregation relationship in Java

Discussion in 'Java' started by Anonymous, Apr 21, 2004.

  1. Anonymous

    Anonymous Guest

    I have two classes. Player.java and Team.java
    I want to make Player a member of Team.

    how do you do this?
    I tried putting Player p1 = new Player(); in Team but Visual Paradigm
    (when asked to generate a class diagram from java code) did not show
    an aggregation relationship in the class diagram (no line with a
    diamond shaped end)
    anyone know how to do it properly?
    if visual paradigm is wrong any suggestions on a new program to use?
    Anonymous, Apr 21, 2004
    #1
    1. Advertising

  2. Anonymous

    Chris Smith Guest

    Anonymous wrote:
    > I have two classes. Player.java and Team.java
    > I want to make Player a member of Team.
    >
    > how do you do this?
    > I tried putting Player p1 = new Player(); in Team but Visual Paradigm
    > (when asked to generate a class diagram from java code) did not show
    > an aggregation relationship in the class diagram (no line with a
    > diamond shaped end)
    > anyone know how to do it properly?


    There is no distinction in Java between aggregation and composition.
    Which you have depends on how you use the member. Reverse engineering
    to UML is not going to catch this, regardless of your tool. What you
    need to do is leave the code alone, and modify the UML diagram to
    express your intention.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Apr 21, 2004
    #2
    1. Advertising

  3. In message <>, Anonymous
    <> writes
    >I have two classes. Player.java and Team.java
    >I want to make Player a member of Team.
    >
    >how do you do this?
    >I tried putting Player p1 = new Player(); in Team but Visual Paradigm
    >(when asked to generate a class diagram from java code) did not show
    >an aggregation relationship in the class diagram (no line with a
    >diamond shaped end)
    >anyone know how to do it properly?
    >if visual paradigm is wrong any suggestions on a new program to use?


    Try this: http://www.dcs.napier.ac.uk/~kab/jeRome/jeRome.html



    --
    Jeffrey Spoon
    Jeffrey Spoon, Apr 22, 2004
    #3
  4. Anonymous

    Jim Guest

    Anonymous wrote:

    > I have two classes. Player.java and Team.java
    > I want to make Player a member of Team.
    >
    > how do you do this?
    > I tried putting Player p1 = new Player(); in Team but Visual Paradigm
    > (when asked to generate a class diagram from java code) did not show
    > an aggregation relationship in the class diagram (no line with a
    > diamond shaped end)
    > anyone know how to do it properly?
    > if visual paradigm is wrong any suggestions on a new program to use?
    >


    Is Player an attribute of Team in your code?

    You need to write something like

    public class Team {
    private Player p1; // p1 is an attribute of Team

    // constructor
    public Team() { p1 = new Player(); }
    }

    or you won't get an association. If you have written

    public class Team {

    // constructor
    public Team() {}
    public void someMethod() {
    Player p1 = new Player(); // Just a local variable!
    }
    }

    you do not have an association, only a dependency.

    You still may not get the diamond. Aggregations are usually a
    collection of some object. So you can code something like this

    public class Team {
    private ArrayList thePlayers = new ArrayList();

    // constructor
    public Team() {}
    public void someMethod() {
    thePlayers.add(new Player());
    }
    }

    thePlayers is an aggregate of Players.

    Jim
    Jim, Apr 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Osborn

    Aggregation vs. composition

    Rick Osborn, Jan 23, 2004, in forum: Java
    Replies:
    11
    Views:
    57,903
    amitg_amity
    Aug 31, 2009
  2. Markos Charatzas

    Interfaces and aggregation

    Markos Charatzas, Jan 30, 2004, in forum: Java
    Replies:
    7
    Views:
    1,931
    BarryNL
    Feb 2, 2004
  3. MegaC
    Replies:
    1
    Views:
    320
    Steve Pugh
    Mar 9, 2006
  4. Sony Antony
    Replies:
    3
    Views:
    452
    C. M. Sperberg-McQueen
    Dec 19, 2003
  5. Replies:
    2
    Views:
    377
Loading...

Share This Page