[Edu-sig] Re: Design Pattern question

Lee Harr missive at hotmail.com
Mon Oct 20 19:38:48 EDT 2003


>I wondered if anyone had ideas about a design pattern I've been playing
>with.  Is it perverse?
>
>I don't want to create global variables at the module level, but have a lot
>of information that many objects need to share and keep current about,
>possibly update.  I don't always know these variables at the time my 
>objects
>get initialized.  Some become available later.
>
>So the idea is to have my classes inherit from a base class the main 
>purpose
>of which is to get runtime "globals".  These may be defined well after the
>subclass objects have been initialized.
>
>Also, I don't explicitly initialize this shared parent class either (it
>doesn't have an __init__ method) -- I just stuff it with variables at the
>class level when I want to communicate them to the children.
>


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.
Like this:

>>>class Holder:
...   pass
...
>>>class Foo(Holder):
...   def __init__(self):
...     self.a = 1
...
>>>o=Foo()
>>>o.a
1
>>>Holder.b = 3
>>>o.b
3
>>>class Goo(Holder):
...   def __init__(self):
...     self.q = 2
...
>>>k=Goo()
>>>k.q
2
>>>k.b
3
>>>o.b=5
>>>k.b
3
>>>Holder.b=5
>>>k.b
5
>>>k.b=7
>>>Holder.b
5



So, one instance changing the value does not change it in other
instances, unless they all always explicitly access it as Holder.x

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.

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

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.

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail




More information about the Edu-sig mailing list