views:

84

answers:

1

I've got multiple structures to maintain in my application. All link to the same records, and one of them could be considered the "master" in that it reflects actual relationships held in files on disk. The other structures are used to "call out" elements of the main design for purchase and work orders. I'm struggling to come up with a pattern that deals appropriately with changes to the master data.

As an example, the following trees might refer to the same data:

A
|_
  B
  |_ 
    C
    |_
      D
|_
   E
   |_
     B
     |_
       C
       |_
         D


A
|_
   B
   E
C
|_
  D

A
|_
  B
  C
  D
  E

These secondary structures follow internal rules, but their overall structure is usually user-determined. In all cases (including the master), any element can be used in multiple locations and in multiple trees. When I add a child to any element in the tree, I want to either automatically build the secondary structure for each instance of the "master" element or at least advertise the situation to the user and allow them to manually generate the data required for the secondary trees.

Is there any pattern which might apply to this situation? I've been treating it as a view problem, but it turns out to be more complicated than that when you look at the initial generation of the data.

A: 

A tree implementation should be a good starting point. The master copy will be the complete tree.

The nodes in the copies can be composite objects that contain the data and a reference to the respective node in the master tree.

When a child is added or any other modification happens on a node in one of the copies, the node can send a message to the master tree with the reference of the corresponding node in the master tree and details of the change.

The master would then modify itself and update the other copies.

The event handling may get tricky because you will have to make sure to not make the whole process cyclic.

Prachi
Hi Prachi, I'm looking for suggestions on how to handle the syncing of structures, not on the base structure itself - I am constrained to use tree structures for all of the data involved. Thanks for your answer, though.
Mike Burton
An event bus might be useful where each copy of the master could register as a publisher. When a copy is updated, it lets the bus controller know about the update. The bus controller can send out events to the master and the other copies about the update.
Prachi