Note that there are some explanatory texts on larger screens.

plurals
  1. POHierarchy / Flyweight / Instancing Problem in Python
    text
    copied!<p>Here is the problem I am trying to solve, (I have simplified the actual problem, but this should give you all the relevant information). I have a hierarchy like so:</p> <pre><code>1.A 1.B 1.C 2.A 3.D 4.B 5.F </code></pre> <p>(This is hard to illustrate - each number is the parent, each letter is the child).</p> <ol> <li><p>Creating an instance of the 'letter' objects is expensive (IO, database costs, etc), so should only be done once.</p></li> <li><p>The hierarchy needs to be easy to navigate.</p></li> <li><p>Children in the hierarchy need to have just one parent.</p></li> <li><p>Modifying the contents of the letter objects should be possible directly from the objects in the hierarchy.</p></li> <li><p>There needs to be a central store containing all of the 'letter' objects (and only those in the hierarchy).</p></li> <li><p>'letter' and 'number' objects need to be possible to create from a constructor (such as Letter(**kwargs) ).</p></li> <li><p>It is perfectably acceptable to expect that when a letter changes from the hierarchy, all other letters will respect the same change.</p></li> </ol> <p>Hope this isn't too abstract to illustrate the problem.</p> <p>What would be the best way of solving this? (Then I'll post my solution)</p> <p>Here's an example script:</p> <pre><code>one = Number('one') a = Letter('a') one.addChild(a) two = Number('two') a = Letter('a') two.addChild(a) for child in one: child.method1() for child in two: print '%s' % child.method2() </code></pre>
 

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