looking for design pattern name

Raymond Hettinger vze4rx4y at verizon.net
Wed Jul 30 11:37:16 CEST 2003

"Andrew Dalke" <adalke at mindspring.com> wrote in message
news:bg1lt6$81g$1 at slb2.atl.mindspring.net...
> I'm looking for the name used for a pattern like what Qt uses for
> it's container hierarchy.  I'm doing this design for molecules, so I'll
> use that as my example.
> There's a container, in this case, a Molecule.  A Molecule has Atoms
> and Bonds.  When an Atom, it is passed the parent container as
> part of the constructor, as in
> mol = Molecule()
> O1 = Atom(mol, 8)    # note the parent in the constructor
> O2 = Atom(mol, 8)
> Bond(O1, O2, 2)    # (the parent is implicit - comes from the atoms)

What happens in Bond() if O1 and O2 have different parents?

When O1 is constructed, does mol get informed?  IOW, after the
four assignments, does mol know that it has two atoms and double bond?

> I want to compare this to a design like
> mol = Molecule()
> O1 = mol.add_atom(8)
> O2 = mol.add_atom(8)
> mol.add_bond(O1, O2)
> which does not allow a user-derived Atom and Bond class
> and one like
> mol = Molecule()
> O1 = Atom(8)
> mol.add_atom(O1)
> O2 = Atom(8)
> mol.add_atom(O2)
> b = Bond(2)
> mol.add_bond(b)

How does this setup handle:

  mol1 = Molecule()
  mol2 = Molecule()
  b = Bond(2)

Is there any way to detect that the same Atom or Bond
being used in two different molecules?

> Anyone have good names for any of these three styles?

Assuming that the first approach operates by constructing
components and notifying parents that it is a new component,
then I would call it the Widget pattern because it operates like
Tk widgets:

    root = Tk()               # root = Molecule()
    b1 = Button(root)     # b1 = Atom(root, 8)
    b2 = Button(root)     # b2 = Atom(root, 8)

In Chapter 2 of The Art of Objects, the second and third
approaches are called Collection Manager and Container.
The key distinction is that a Container only contains the
objects and is not responsible for creating them.

Another idea is to name the patterns after the limitations
inherent in each design.  When Atoms are constructed
implicitly, the design does not favor applications
that break-up a molecule and re-attach some of the existing
atoms into new molecules, so call it NoMix or some such.

Still another ideas is to describe a pattern by its
characteristic variant or invariant properties:

* The first approach has components with fixed parents
    established at the time of component creation.  Call the pattern
    SignUp since every new atom or bond has to immediately
    signup with an existing parent.

* The second approach has captive components.
   This suggests the name MoleculeDriven to cover the idea
   that all component atoms or bonds are created from the
   molecule, thus assuring there can be no orphan atoms or bonds.

* The third approach allows free mixing of components.
   This is suggestive of the name TinkerToy where all the
   molecular model components can lay separately on a
   table (sticks for bonds and cogwheels for atoms).

Raymond Hettinger

More information about the Python-list mailing list