简体   繁体   中英

Java - objects with each other in parameters

So I have something like this in my driver class:

Person person1 = new Person(home1, job1);

Home home1 = new Home(person1);

Job job1 = new Job(person1);

where the parameters for the Person constructor are a home and job, and for the home constructor is the owner, and for the job constructor is the employee. How could I make this work?

Avoid circular dependencies where possible . However you can do this by wiring them up from the outside. So:

Person person1 = new Person();

Home home1 = new Home(person1);
person1.setHome(home1);

Job job1 = new Job(person1);
person1.setJob(job1);

As I say often unwise, but can be useful on occasion when used carefully. However it is always unwise to do this and make Person rely on .setHome(home1); and .setJob(job1); being called; in otherwords a homeless jobless Person should still operate without error.

You can't forward-reference the variable like your code is doing. When creating person1 , home1 and job1 haven't been created yet. Because of the interdependency, this approach will always have something being passed to a constructor that isn't itself initialized yet.

You will need to create these objects without the references, then use setter methods after instantiation to create the relationships. Something like this:

Person person1 = new Person();
Home home1 = new Home();
Job job1 = new Job();

person1.setHome(home1);
home1.setOwner(person1);
person1.setJob(job1);
job1.setOnwer(person1);

First of all it's ain't pretty to do something like that, so the best solution will be rethinking solution :)

Anyway, if you really have to do something like that, you can move initialization from constructor to other method (for example setter). One of possible solutions:

public class Home {
     // ...

     public void setPerson(Person person) {
          // ...
     }
}

public class Job {
     // ...

     public void setPerson(Person person) {
          // ...
     }
}

public class Person {

     // ...

     public void Person(Home home, Job job) {
          job.setPerson(this);
          home.setPerson(this);
          // ...
     }
}

// ...
Home home = new Home();
Job job = new Job();
Person person = new Person(home, job);

This is not possible with just constructors - as objects cannot be used before they are created and variables cannot be used before they are assigned (meaningful) values! As such, mutators (or field access) must be used:

// No circular deps yet
Home home1 = new Home();
Job job1 = new Job();
Person person1 = new Person(home1, job1);
// Connect back to person
home1.setPerson(person1);
job1.setPerson(person1);

However, this manual assignment is prone to being forgotten or otherwise incorrectly applied . One way this parent-child relationship establishment can be cleaned up - and is done so in various UI or other Object Graph libraries - is to make the "add" method also establish the opposite relation. Then it can be "simplified" to:

Home home1 = new Home();
Job job1 = new Job();
Person person1 = new Person(home1, job1);
// Note that only one side of the dependency has been manually assigned above!

// Which in turn takes care of the appropriate logic to
// establish the reverse dependency ..
public Person (Home home, Job job) {
   this.home = home;
   this.home.setPerson(this);
   // ..
}

It might also be prudent to add guards such that a Home/Job instance can't be "accidentally" re-assigned to a different person.

Assuming Person is your main object (a person has a job and owns a home), you could, in its constructor, create a new Home and a new Job and pass them references to the person.

public class Person {

    private Home home;
    private Job job;

    public Person() {
        this.home = new Home(this);
        this.job = new Job(this);
    }

    public Home getHome() {
        return this.home;
    }

    public Job getJob() {
        return this.job;
    }

}

Your Home and Job classes will obviously have to take a Person in their constructors.

So back in your Driver, all you need is:

Person p = new Person();

Job j = p.getJob();

Home h = p.getHome();

And now you can do whatever you want to with all three.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM