Is there a pattern...

Magnus L. Hetland mlh at idt.ntnu.no
Thu Aug 26 11:11:49 EDT 1999


Hi!

While checking some codee (that I had not written myself - and it was
in Java, not Python...) I encountered the following situation...

The code included classes and methods for creating graphs (i.e.
networks), in quite a nice manner. Each vertex had a list of edges,
and the edges themselve took care of registering themselves (and
removing themselves) with the vertices. Thus, you could make a simple
graph like this:

x = Vertex("some value")
y = Vertex("some other value")
Edge(x,y)

Now - if you want to remove the edge, you would do something like:

x.getOutEdge(0).deleteMe()

That is - you would get a hold of the edge you wanted to remove, and
call its delete method. (In Python maybe you could use del, but that's
beside the point). Now - this method called corresponding methods in
each of its two vertices (in this case x an y) that actually removed
it from their lists of edges. This seems OK - *except* that these
methods should *only* be called from the edge and nowhere else...

So I started to wonder - is there an intelligent design pattern that
addresses this problem, i.e. methods that should only be called from a
specific class or a specific set of objects or something? (Somewhat
like friend classes in C++, I guess - if I don't remember it all
wrong...)

Of course, you *could* just appeal to the programmers conscience and
tell him not to call the methods anywhere else, but...

--

  Magnus              Making no sound / Yet smouldering with passion
  Lie          The firefly is still sadder / Than the moaning insect
  Hetland                                       : Minamoto Shigeyuki




More information about the Python-list mailing list