Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up bidirectional association between two objects in OO Java

I have a basic assignment to do but am very new to OOP and struggling with it. Other online resources are starting to add to my confusion.

I am required to:

  1. Write code for a class Person. A Person object is to have attributes name, age and address.

  2. Write code for a class Dog. A Dog object is to have attributes name and age.

  3. Give any additional code in the Person and Dog classes that is required to setup a bidirectional association between a Person object and a Dog object. A Person object acts as an owner for a Dog object and the Dog object acts as a pet for the Person object.

  4. Modify your Person class so that a Person object can act as owner for up to 20 Dog objects.

Obviously this is a very simple example.

My code so far:

Person Class :

    public class Person
{
    // instance variables - replace the example below with your own
    private String name;
    private int age;
    private String address;




    /**
     * Constructor for objects of class Person
     */
    public Person()
    {
       this.name = name;
       this.age = age;
       this.address = address;
    }



    //Set Methods: 
    public void setName () {
            this.name = name;
    }


    public void setAge () {
            this.age = age;
    }


    public void setAddress () {
            this.address = address;
    }


    //Get Methods: 
    public String getName () {
            return name;
    }

    public int getAge () {
            return age;
    }

    public String getAddress () {
            return address;
    }
}

Dog Class:

    public class Dog
{
    // instance variables - replace the example below with your own
    private String name;
    private int age;



    public Dog()
    {
       this.name = name;
       this.age = age;
    }


    //Set Methods:
    public void setName () {
        this.name = name;
    }

    public void setAge () {
        this.age = age;
    }

    //Get Methods:
    public String getName () {
        return name;
    }

    public int getAge () {
        return age;
    }


}

Main:

public class Main
{
   //Blank 
}

I know this code is currently useless and doesn't do anything but I am unsure of how to 'associate' the objects & where to do it. The assignment spec specifies a person acts as an 'owner' for the dog.

This is where my problem lies. Setting up the relationship between the objects.

like image 825
Anon Omus Avatar asked Nov 20 '13 13:11

Anon Omus


People also ask

What is bidirectional relationship in Java?

In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class's code can access its related object. If an entity has a related field, the entity is said to “know” about its related object.

How do you make a one-to-many relationship a bidirectional relationship?

Bidirectional @OneToMany Relationship – Employer/EmployeeWhen you traverse from the “Many” side to the “One” side, you only need to make reference to one object, which is why the Employee class holds a single reference to an Employer class via the private Employer employer instance variable.

What is a bidirectional association?

Bi-directional associations (or symmetric associations ) are the simplest way that two classes can relate to each other. A bi-directional association is shown as a line between two classes, and can contain control points. The classes can be any mix of classes, simple classes, or composite classes.

Which is an example of a bidirectional relationship?

A bidirectional relationship is valid in both directions. Intersects is an example of a bidirectional relationship.


1 Answers

The main problem here is consistency: if a Dog d1 is a pet for a Person p1, then p1 must be owner of d1, and vice versa. If, as many suggested, we have 2 methods (Person.addDog() and Dog.setOwner()), then a user can easily make a mistake and fail to call both methods (or call with wrong arguments). Since a Dog can have only one owner, a simple and safe interface would be using single method Dog.setOwner(Person p), where p may be null if we want the dog to have no owner. This method, besides setting the field Dog.owner, must remove this dog from the pet list of previous owner and (if p != null) add itself to the pet list of the new owner. The methods of class Person to add and remove pets should be visible for the class Dog but not visible to the user (they should be package private), while the method Dog.setOwner should be public.

UPDT We can consider value of Dog.owner as a primary datum, and value of Person.dogs as secondary data, similar to database indexes.

like image 145
Alexei Kaigorodov Avatar answered Oct 16 '22 22:10

Alexei Kaigorodov