UML Ternary Association Implement java Code

Viewed 747

ternary association is structural relationship specifies that Object of one thing connected to object of other two's

I understand this relationship but I have no idea how to implement methods that shows association between these three classes.

lets consider following example

  • project has number of Developers who use particular programming language for development
  • developer use particular programming language for develop number of projects
  • in selected project one developer use only one programming language

ternary association between project ,developer and programming language

There exists a ternary association between these three classes.

I've read on different sources regarding this all across the internet and couldn't find a solution

How do I implement above scenario in code (in java) ?

P.S - Not only this Any other ternary association coding example would be appreciate

2 Answers

You could use one class to represent the "Project". This class has:

-Map <Developer, Langage> developers

One class to represent a "Developer". This class has:

-Set<Langage> langages

-Set<Projects> projects

Finally, one class to represent the "Language".

Class Project {
    Map<Developer, Language> developers = new HashMap<>();

    public void add(Developer developer) {
        developers.put(developer, developer.getLanguage());
        developer.registerOn(this);
    }

Class Developer {

    private Set<Language> languages;  
    private Set<Projects> projects;

    public boolean developIn(Language language) {
       return languages.contains(language);
    }

    public void registerOn(Project project) {
       projects.add(project);
    }
}

Enum Language {
    JAVA,
    PHP;
}

That UML diagram does not say what you believe it says. The association is an object too.

If you want to specify that "in selected project one developer use only one programming language", then the diagram should be:

enter image description here

Related