[Edu-sig] Re: Design Pattern question

Kirby Urner urnerk at qwest.net
Mon Oct 20 23:25:47 EDT 2003


> 
> I think I might find it confusing because of the way names get bound
> to certain things at class level, but overridden at the instance level.

I think you're right.  It's not that great a design pattern, for the reason
you mention:  children can't easily update each other.  This pattern is
mainly for when the children don't have their own versions of the same
variables -- basically read-only access to the parent is what's going on
here.

If I want children to all update the same variables, one option is to stuff
a globals class into the parent and then update it explicitly, as you say.

 >>> class Globals:  # some shared variables
	  a = 1
	  b = 2
		
 >>> class Foo:

	globals = Globals()

	def __init__(self):
		x = 1

		
 >>> o1 = Foo()
 >>> o2 = Foo()
 >>> o1.globals.a
 1
 >>> o1.globals.a = 1999
 >>> o2.globals.a
 1999

I can also stick new attributes into Foo.globals at runtime, and they'll
join the pack of shared updatables.
 
> What I have done (and I am certainly not saying it is the best way,
> or even any better than what you have) is to create a separate
> module that just holds a bunch of names that many other modules
> need to access. Everyone always refers to it by it's full name and
> so everyone always has the same information.

Interesting approach.  The general idea is we're looking to create a shared
namespace that's not truly global, but still lets as many objects as we like
in on the same secrets.

> It's kind of a global namespace that is not global, if that makes
> any sense.

Sure.

> In pygsear, it is the conf module. Some things that I keep in there
> are the size of the window, sound system status, time count (ticks),
> and I am probably going to put a handle for the currently running
> Game instance there too.

That's very similar to my challenge:  I'm working on one of those
multi-banded reports, with title, summary, group and detail bars (headers,
footers).  All these objects need to share information about the page,
especially how much room is left before a page break is required.

Kirby





More information about the Edu-sig mailing list