Private class?

C

codecraig

Hi,
First, I come from a Java background.

Ok, so I have this idea that I want to create an EventBus...basically
a central class where objects can register themselves as listeners for
different events. This central class also has methods so that objects
can fire events. Something like this...

Say we have object MrTree and object MotherNature. MrTree listens for
EVENT_GROW_LEAVES. So, when MrTree "hears" about EVENT_GROW_LEAVES,
MrTree will do just that, grow some leaves. So, while our python app
is running, MotherNature needs to tell MrTree to grow leaves. I am
suggesting something like this...

1. MrTree has a method, growLeaves()

2. MrTree registers for the event, EventBus.register(MrTree,
EVENT_GROW_LEAVES)

3. MotherNature fires grow leaves, EventBus.fire(EVENT_GROW_LEAVES)

4. MrTree's growLeaves method gets called by EventBus.

Now, in Java I would make EventBus a Singleton so that only one
instance of it would exist. And I would have MrTree implement
EventListener interface, where the interface defines a method like
growLeaves(). So when MotherNature tells the EventBus to fire the
event, EventBus loops through it's list of listeners and calls,
growLeaves()

How would I go about doing this in Python? Here is my quick idea

class EventListener:
def eventOccurred(self, eventType): pass

class MrTree(EventListener):
def __init__(self):
EventListener.__init__(self)
EventBus.register(self, EventBus.EVENT_GROW_LEAVES)

def eventOccurred(self, eventType):
if eventType == EventBus.EVENT_GROW_LEAVES:
print "Growing Leaves"

class EventBus:
EVENT_GROW_LEAVES = 0

__listeners = {}

def register(listener, eventType):
if __listeners.has_key(eventType):
curListeners = __listeners.get(eventType)
curListeners.append(listener)
else:
__listeners[eventType] = [listener]

def fire(eventType):
if __listeners.has_key(eventType):
x = __listeners.get(eventType)
for l in x:
l.eventOccurred(eventType)

class MotherNature:
def doSomeStuff():
print "doing whatever Mother Nature does..."
EventBus.fire(EventBus.EVENT_GROW_LEAVES)


....so that is what I am thinking. However, i guess my issue is how to
make EventBus a singleton or prevent it from being instaniated and
making it's methods statically accessible.

thanks.
 
P

PA

...so that is what I am thinking. However, i guess my issue is how to
make EventBus a singleton or prevent it from being instaniated and
making it's methods statically accessible.

Not directly related to your question, but... you may want to take a
look at NSNotification & Co. for, er, inspiration :)

"Introduction to Notifications"
http://developer.apple.com/documentation/Cocoa/Conceptual/
Notifications/index.html

Here is a -gasp- Java implementation as well:

http://dev.alt.textdrive.com/file/ZOE/Frameworks/SZFoundation/
SZNotification.java

Cheers
 
B

Brian van den Broek

codecraig said unto the world upon 2005-04-21 09:13:
Hi,
First, I come from a Java background.

...so that is what I am thinking. However, i guess my issue is how to
make EventBus a singleton or prevent it from being instaniated and
making it's methods statically accessible.

thanks.

I don't come from a Java, or any other language, background, so I
cannot warrant that any of these will seem helpful to you. But there
are a lot of Singleton and Singleton-like patterns floating about.

Here's some:

<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531>
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558>
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/102187>
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286206>
<http://fraca7.free.fr/blog/index.php?2005/03/23/6-design-patterns-part-iii---singleton>

Best,

Brian vdB
 
K

Kent Johnson

codecraig said:
class EventListener:
def eventOccurred(self, eventType): pass

The EventListener class is not needed in Python; you can include it for its documentation value if
you like but Python does not require interfaces the way Java does.
class EventBus:
EVENT_GROW_LEAVES = 0

__listeners = {}

def register(listener, eventType):
if __listeners.has_key(eventType):
curListeners = __listeners.get(eventType)
curListeners.append(listener)
else:
__listeners[eventType] = [listener]

def fire(eventType):
if __listeners.has_key(eventType):
x = __listeners.get(eventType)
for l in x:
l.eventOccurred(eventType)

...so that is what I am thinking. However, i guess my issue is how to
make EventBus a singleton or prevent it from being instaniated and
making it's methods statically accessible.

One way to do this is to make an EventBus module and make __listeners, register() and fire() be
module attributes. Your client code would look like
import EventBus
....
EventBus.register(...)
EventBus.fire(...)

Kent
 
J

Jack Diederich

Hi,
First, I come from a Java background.

Ok, so I have this idea that I want to create an EventBus...basically
a central class where objects can register themselves as listeners for
different events. This central class also has methods so that objects
can fire events. Something like this...

Say we have object MrTree and object MotherNature. MrTree listens for
EVENT_GROW_LEAVES. So, when MrTree "hears" about EVENT_GROW_LEAVES,
MrTree will do just that, grow some leaves. So, while our python app
is running, MotherNature needs to tell MrTree to grow leaves. I am
suggesting something like this...

1. MrTree has a method, growLeaves()

2. MrTree registers for the event, EventBus.register(MrTree,
EVENT_GROW_LEAVES)

3. MotherNature fires grow leaves, EventBus.fire(EVENT_GROW_LEAVES)

4. MrTree's growLeaves method gets called by EventBus.
<snip>

Singletons are overrated, if you need a namespace just use a class or
module as another responder suggested. Some of the indirection you
need to do this in Java goes away in python because you can do things
like pass around bound function objects. Here is some code that includes
those suggestions.

class EventCaller(object):
def __init__(self):
self.listeners = []
return
def append(self, listener):
self.listeners.append(listener)
return
def __call__(self):
for (listener) in self.listeners:
listener()
return

class EventBus(object):
growLeaves = EventCaller()

class MrTree(object):
def __init__(self):
EventBus.growLeaves.append(self.growLeaves)

def growLeaves(self): pass

class MotherNature(object):
def doStuff(self):
EventBus.growLeaves()


The EventCaller class is overloaded to behave both like a list and
like a function. The EventBus class never gets instantiated, it is
just used as a place to lookup the different event queues. MrTree
doesn't need to inherit from an interface class, his growLeaves method
is just a function that we can pass around.

-jackdied
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top