0

I have the following problem. Assume we have two instances of the class Assignment. Each instance has a start and an end date. The second assignment starts when the first ends. Now I have some information in the first assignment which is also needed in the second assignment when the first one is expired.

I would need something like a static variable for this both instances, but not static for the whole class.

What is the best way to do this?

Thanks

Matthias

===============EDIT===============

I think my question was not specific enough. I know how to copy data from one into another, but this is not appropriate in this situation.

My assignments are the linking between employees and projects to resolve a n:n relation. But one employee could have several assignments to the same project and I need information which is important for all assignments.

If I update the information in assignment1 this should be updated in all other assignments which connect the employee to this specific project. For performance reasons, I would prefer to change this in one central place and not to copy the information by hand.

===============EDIT===============

I thought about the problem, and I think my problem could be solved if every assignment holds a reference to the information. With this, a change in one of the assignments should automatically be distributed to the others.

===============EDIT===============

Abaddon666 answer and annotation were correct, the remaining question is how to share that reference to all instances of assignment in a proper way. We solved it in such a way, that we use a table in the DB where we store the information with two foreign keys and load the information for an employee and for a specific project when it is needed. I will accept Abaddon666 answer, but maybe there is a better solution as the current one.

  • I would create a class which has a static method. This method takes two Assignmet object and does the processes. You don't need to return these values, because Java pass arguments by reference. – grnc Feb 20 '19 at 16:11
  • Hi, thanks for the answers I updated my question. I guess I wrote it not specific enough –  Feb 20 '19 at 16:49
  • "If I update the information in assignment1 this should be updated in all other assignments which connect the employee to this specific project" - no clear what that means. Do you mean to shift all other assignment dates when one time span changes? – yegodm Feb 20 '19 at 17:18
  • I want a construct that simulates a static variable (the information) for a bundle of assignments. Sorry, I don't know how to explain it in a better way. –  Feb 20 '19 at 17:37
  • You can share some code snippets, for better explanation. – grnc Feb 20 '19 at 18:37
  • At the moment I think about the solution to the problem before I start coding. –  Feb 20 '19 at 18:40

1 Answers1

1

What you are trying to achieve is basic Composition. You want 1 to x instances of a certain type to share the same value. This can be achieved by creating a class that models your shared Information and add it as a field to your assignment:

public class Assignment{
    private Date startDate;
    private Date endDate;
    private Information information;

    public Assignment(Information information){
         this.information = information
    }

    public Information getInformation(){
         return this.information
    }
...
}

...

Information sharedInformation = new Information();
Assignment assignment1 = new Assignment(sharedInformation);
Assignment assignment2 = new Assignment(sharedInformation);
Abaddon666
  • 1,533
  • 15
  • 31
  • Hi thank you for your answer, I updated my question. I guess I wrote it not specific enough. –  Feb 20 '19 at 16:48
  • @MatthiasLauber Regarding your last edit composition is exactly what you want to go for. I am assuming the remaining question is mainly how to share that reference to all instances of assignment in a proper way? – Abaddon666 Feb 22 '19 at 10:33