Note that there are some explanatory texts on larger screens.

plurals
  1. POObject Relationship Design
    text
    copied!<p>I just happen to ponder of a OO concept which may sound quite trivial but I don't know why I find it quite confusing.</p> <p>Anyway, I am thinking for example, if I have an Animal class and a Location class. And I only allow one animal to be at one location at any time. So it is kind of like a 1-to-1 relationship. At the same time, I wish that the Animal and Location classes do not need some kind of bidirectional reference so that they are kept loosely coupled. If say I have this:</p> <pre><code>class Animal { private Location loc; public Animal(int x, int y) { loc = new Location(x,y); } public static newAnimal(Location[][] map, int x, int y) { if(map[x][y] != null) { return new Animal(x, y); } else return null; } class Location extends Point { public Location(int x, int y) { super(x, y); } } public static void main(String[] args) { //populates a map Location[][] map = new Location[10][10]; for(int x=0; x&lt;10; x++) { for(int y=0; y&lt;10; y++) { map[x][y] = new Location(x, y); } } Animal dog = new Animal(2, 4); //dog is at location 2,4 Animal cat = new Animal(5, 6); //cat is at location 5,6 //But this does not restrict a constraint requirement that there should only be one animal at any one point Animal horse = new Animal(2, 4); //now, horse is at the same location as dog but we only wanted one location to have one animal Animal rabbit = Animal.newAnimal(map, 20, 50); //rabbit is null because it is out of the map size } </code></pre> <p>From this, I foresee 2 problems.</p> <p>First, because my location does not know if an animal is already on it, many animals can all be pointing to a same location on the map array. This would violate the 1-1 multiplicity constraint that I wanted. In my case, I let the Animal to own the Location. this could the be reason why this could happen. If say I let the Location to own the Animal, this could be solved. But in a case if I want to know where my Animal is at, I need to loop through the entire map just to find where one of my Animal's location is? Alternatively, I can keep a bidirectional reference but this would cause the classes to be highly-coupled.</p> <p>The second problem that I feel could be a problem is the design in the Animal class. I have a static newAnimal() method to instantiate new animals. I did it this way because I thought allowing the caller to create new Animal instance directly from the constructor may allow an out of range coordinate input. But I still find the design very awkward.</p> <p>I am using Java codes in my example. And I am thinking the design within the class objects itself and is not involving the database yet.</p> <p>Any suggestions to improve the two issues I raised could be great. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload