Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36832

Re: no easy way to do a reverse pointer?

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: no easy way to do a reverse pointer?
Date 2017-07-07 21:47 +0000
Organization A noiseless patient Spider
Message-ID <ojovht$qok$1@dont-email.me> (permalink)
References <6c91c2b4-7d8f-42ba-8a51-eaef43635f49@googlegroups.com> <ojooib$v3s$1@dont-email.me> <1d4174af-ff17-433c-bb32-23e17388e9ff@googlegroups.com>

Show all headers | View raw


On Fri, 07 Jul 2017 13:28:05 -0700, Eric Douglas wrote:

> 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.
>
Yes, that's OK, but there's almost certainly more structure that needs to 
be there to understand the problem. For starteds you need some sort of 
duty diary. The days in this would be initially populated with the 
appropriate number of unassigned duties of each type.

> Any duty may have multiple soldiers.
>
Probably not, but that depends on the representation: the 'Duty Rota' of 
a specific type will have a bunch of soldiers (one per duty slot), but if 
you consider that a Duty is might be defined as "Main Gate duty on the 
morning of 13 August 2017, then it quickly becomes obvious that a Duty 
instance has one soldier assigned to do it.

> 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.
> 
Yes, indeed, and the lists are quite different because of how they'll be 
used and by whom. 

The list of who has what duty needs to show the soldier doing each duty 
on every day: it is probably a matrix with date and time down the side, 
duty type across the top and a soldier's name in each cell.

OTOH a soldier's list only needs to show, ordered by date and time, what 
type of duty each is, when he'll be on it and where it is (assuming the 
duty slot's name doesn't indicate where it will be performed.
 
> 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?
>
Agreed. It looks as though Duty may be a good candidate for subtyping, 
though it may work equally well as a single class with its type being 
either a class attribute or derived from the column its linked to in the 
duty rota. This is something that should only be decided when the problem 
is completely understood and (preferably) after data analysis has 
generated an ERD linking the various entities (classes) and listed the 
main attributes of each entity.
 

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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