Question

I have two classes Person and Company, where the associations specify like:

(1) person work for a company

(2) person(boss) manages person(worker) by rating.

Details is provided in this image: enter image description here

In case of first association i did this:

class Person {
    Company co;
    String name, ssn, addr;
}

class Company {
    String name, addr;
}

class WorksFor {
    Person person;
    Company com;
    int sal;
    String title;
}

Is it correct implementation? also i am confused about second association. Please help me with your valuable advise.

Was it helpful?

Solution

It is not.

You should know that if class have associetion with something it is like it has attribute that type with given cardinality.

So for example Person would have given fields (omitting association classes):

String name[1]
String ssn[1]
String addr[1]
Company company[1]
Person boss[0..*]
Person worker[1..*]

Then how to change those to java:

String name;
String ssn;
String addr;
Company company;
List<Person> bosses;
List<Person> worker;

But remember if there is required number of given type you should pass those elements in constructor. One thing important to mention: if in UML value is omitted it means [1]. So we need to have constructor that takes worker as argument.

With association classes things getting more complicated: You should create classeslike

class Performance {
    Person boss;
    Person worker;
    int performanceRating;

    public Performance(Person boss, Person worker, int performanceRating){
        this.boss = boss;
        this.worker = worker;
        this.performanceRating = performanceRating;
    }
}

And in person change those list of Persons to list of Performance.

Still it's invalid UML diagram so I'm not perfectly sure if my answer can helps.

OTHER TIPS

Usually, class in model (classes associated with data) is related with real-world objects. And it is common practice to call classes with a noun, so I would change WorksFor into something else.

class Person {
    String name, ssn, addr;
    Job job;
    Person boss;
}

class Company {
    String name, addr;
}

class Job {
    Company co;
    int salary;
    String title;
}

The problem with code above is ratings and obtaining workers of the boss - it can be solved in multiple ways. Assuming, that every boss is also a worker, you can extend a Person class:

class Boss extends Person {
    List<Person> workers;
}

Another possibility is keeping list of workers with Person - list will be empty/null if a person is not a boss.


Some ideas of performance management is given in KonradOliwer answer, I'm not going to duplicate it.

Could do like this:

class Person {
   EmploymentRelationship employmentRelationship;
   List<ManagementRelationship> workerManagementRelationships;
   ManagementRelationship bossManagementRelationship;
   String name, ssn, addr;
}

class Company {
   String name, addr;
}

class EmploymentRelationship {
   Person person;
   Company com;
   int sal;
   String title;
}

class ManagementRelationship {
   int performanceRating;
   Person boss;
   Person worker;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top