Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36829
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2017-07-07 13:28 -0700 |
| References | <6c91c2b4-7d8f-42ba-8a51-eaef43635f49@googlegroups.com> <ojooib$v3s$1@dont-email.me> |
| Message-ID | <1d4174af-ff17-433c-bb32-23e17388e9ff@googlegroups.com> (permalink) |
| Subject | Re: no easy way to do a reverse pointer? |
| From | Eric Douglas <e.d.programmer@gmail.com> |
On Friday, July 7, 2017 at 3:52:32 PM UTC-4, Martin Gregorie wrote:
> Thats actually backwards: the A instance needs to hold a list of all the
> B instances it owns while each B instance holds a reference to its owning
> A instance. It has to be that way round or you can't easily find all the
> Bs that belong to a selected A. The fun and games comes when you want to
> find all the As that reference a given B. At first glance you just keep a
> list of As in each B but that would be hell on wheels to maintain.
>
> Its OK to use the array of references once, I've done exactly that for a
> set of rosters I maintain: each Roster header has a list of references to
> the roster Members and they in turn have a list of potential Duty slots.
> This allows a Member to be on more than one Roster.
>
> Working in reverse is different. Theres another structure with an entry
> per rostered date and each of these has a reference to all the Duty slots
> on that day.
>
> Another way to tackle this is the use the RDBMS model of insisting that
> each class instance has a unique key. Each list of classes is linked to a
> TreeMap instance which allows all access to by managed by using the keys
> in the TreeMaps. For youe example the ERD looks like this:
>
> ( A* )
> |
> |
> /|\
> ( B )
>
> and the list of TreeMaps is:
>
> TreeMap Akey - holds A's unique prime key and a reference to each A
> instance
> TreeMap Bkey - holds B's unique prime key and a reference to each B
> instance
> TreeMap Balt1 - holds alternate key 1 for B and a reference to the B
> instance. The Alternate key is Bkey.Akey
> TreeMap Balt2 - holds alternate key 2 for B and a reference to the B
> instance. The Alternate key is Bkey.Akey
>
> Using this arrangement you can:
>
> - find all B instances belonging to an A instance by using Balt1:
> they are all linked to the keys whose first part is the A instance
> prime key.
>
> - find all the A instances that are linked to a B instance by using Balt2:
> they are all the keys whose first part is the B prime key
>
> In practise I used both approaches in the rostering application:
> references are used to build the list of members in each roster and the
> list of possible duty slots for each member, but almost all the cross
> references etc are managed by key access via appropriately ordered
> TreeMaps.
>
Exactly, I think, but where to put the maps?
Instead of A and B maybe it's easier to see a practical model.
public class KitchenDuty {
}
public class NightWatchDuty {
}
public class Soldier {
}
Now, the easiest way to set and get a soldier's duties?
Any one soldier may be assigned multiple duties.
Any duty may have multiple soldiers.
We need to be able to list which soldiers are on any given duty.
We need to be able to list which duties are assigned to any given soldier.
Either each of these classes has a HashSet field and maintains a list of duties/soldiers as they're added/removed, or our independent class holds HashSets and maintains as it adds and removes.
public class MyApplication {
static public void main(String[] args) {
KitchenDuty kd = new KitchenDuty();
Soldier s1 = new Soldier();
Soldier s1 = new Soldier();
HashSet kds = new HashSet();
s1.assignDuty(kd);
kds.add(s1);
s2.assignDuty(kd);
kds.add(s2);
}
}
This ^^ independent method sounds ugly. I just want to be able to say kd.listSolders() and s1.listDuties(), so adding a pointer to one should automatically add back a pointer to the other?
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
no easy way to do a reverse pointer? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 11:56 -0700
Re: no easy way to do a reverse pointer? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-07 19:48 +0000
Re: no easy way to do a reverse pointer? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 13:28 -0700
Re: no easy way to do a reverse pointer? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-07 21:47 +0000
Re: no easy way to do a reverse pointer? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 18:06 -0400
Re: no easy way to do a reverse pointer? lewbloch@gmail.com - 2017-07-14 14:32 -0700
csiph-web