feat: add py
This commit is contained in:
parent
82fa3d20b2
commit
e38db6ac2e
35
pub.py
Normal file
35
pub.py
Normal file
@ -0,0 +1,35 @@
|
||||
class SimplePubSub:
|
||||
def subscribe(self, event, listener):
|
||||
pass
|
||||
def unsubscribe(self, event, listener):
|
||||
pass
|
||||
def publish(self, event, data):
|
||||
pass
|
||||
|
||||
# 创建 PubSub 实例
|
||||
pubSub = SimplePubSub()
|
||||
|
||||
def subscriber1(data):
|
||||
print(f'Subscriber 1 received: {data}')
|
||||
|
||||
def subscriber2(data):
|
||||
print(f'Subscriber 2 received: {data}')
|
||||
|
||||
# 订阅事件
|
||||
pubSub.subscribe('event1', subscriber1)
|
||||
pubSub.subscribe('event1', subscriber2)
|
||||
|
||||
# 发布事件
|
||||
pubSub.publish('event1', 'Hello, World!')
|
||||
|
||||
# 取消订阅
|
||||
pubSub.unsubscribe('event1', subscriber1)
|
||||
|
||||
# 发布事件
|
||||
pubSub.publish('event1', 'Hello again!')
|
||||
|
||||
# 输出应该是如下的样式
|
||||
|
||||
# Subscriber 1 received: Hello, World!
|
||||
# Subscriber 2 received: Hello, World!
|
||||
# Subscriber 2 received: Hello again!
|
Loading…
Reference in New Issue
Block a user