0

With the code what relationships are used? I think its composition and dependency, but if a class uses composition does that also mean they use aggregation and association?

public class Sentence {
  private final ArrayList<Word> words=new ArrayList<>();

  public void add(Word word){
    words.add(word);
  }

  public void wordCount(){
    System.out.println("Number of words: "+words.size());
  }
}

Also what would this relationship be?

public class Worker {
  private int numJobs;

  public void Usemachine(Machine machine){
    System.out.println("Using machine: "+machine);
    numJobs++;
  }

  public void jobCount(){
    System.out.println("Number of jobs: "+numJobs);
  }
}
ckhgray
  • 81
  • 1
  • 2
  • 7
  • 2
    I would call it composition (and delegation) only. – Elliott Frisch Dec 24 '21 at 12:30
  • 2
    You can't determine the type of relationship by this code because you can't tell whether `Word` can exist outside of `Sentence`. E.g. be referenced by other entities. If you already worked with databases the question boils down - does `Word` deserve an `ID` of its own? – Stanislav Bashkyrtsev Dec 24 '21 at 12:31
  • 2
    The code showcases different kinds of relationships depending on which part to consider. Generally this is how **composition** look in java. To learn more please take a look [at this answer](https://stackoverflow.com/a/26828250/1689608) – dshelya Dec 24 '21 at 12:36
  • @dshelya could you answer the edited version please? – ckhgray Dec 24 '21 at 12:46
  • 1
    The edit seems to be association. – Alias Cartellano Jan 03 '22 at 17:16

0 Answers0