7.11 使用回掉的松偶合系统
Coupling Systems Loosely with Callbacks
Here's a mixin module that gives each instance of a class its own hash of "listener" callback blocks. An outside object can listen for a particular event by calling subscribe with the name of the event and a code block. The dispatcher itself is responsible for calling notify with an appropriate event name at the appropriate time, and the outside object is responsible for passing in the name of the event it wants to "listen" for.
module EventDispatcher
def setup_
listeners
@event_dispatcher_listeners = {}
end
def subscribe(event, &callback)
(@event_dispatcher_listeners[event] ||= []) << callback
end
protected
def notify(event, *args)
if @event_dispatcher_listeners[event]
@event_dispatcher_listeners[event].each do |m|
m.call(*args) if m.respond_to? :call
end
end
return nil
end
end
Here's a Factory class that keeps a set of listeners. An outside object can choose to be notified every time a Factory object is created, or every time a Factory object produces a widget:
class Factory
include EventDispatcher
def initialize
setup_listeners
end
def produce_widget(color)
#Widget creation code goes here…
notify(:new_widget, color)
end
end
Here's a listener class that's interested in what happens with Factory objects:
class WidgetCounter
def initialize(factory)
@counts = Hash.new(0)
factory.subscribe(:new_widget) do |color|
@counts[color] += 1
puts "#{@counts[color]} #{color} widget(s) created since I started watching."
end
end
end
Finally, here's the listener in action:
f1 = Factory.new
WidgetCounter.new(f1)
f1.produce_widget("red")
# 1 red widget(s) created since I started watching.
f1.produce_widget("green")
# 1 green widget(s) created since I started watching.
f1.produce_widget("red")
# 2 red widget(s) created since I started watching.
# This won't produce any output, since our listener is listening to
# another Factory.
Factory.new.produce_widget("blue")
分享到:
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
标题“coverage-cookbook-complete-verification-academy”表明这是一本关于覆盖度(coverage)的食谱手册,隶属于Cadence Academy的官方文件。这种手册通常包含一系列经过精心设计的指导方案,旨在帮助读者理解和...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
NGINX Cookbook 是一本专门介绍 NGINX 高性能负载平衡的书籍,书中涵盖了 NGINX 的高级配方和使用技巧,可以帮助读者快速搭建高性能的负载平衡系统。 GeoIP 模块和数据库 在 NGINX 中,可以使用 GeoIP 模块来根据...
Unity Game Development Cookbook - Paris Buttfield-AddisonUnity Game Development Cookbook - Paris Buttfield-Addison
Ruby Cookbook就是关于这一当今最热门编程语言的最全面的问题求解指南。本书使用清晰的阐述和数千行可以在你的项目中使用的源代码,来为你在实际应用中可能碰到的数百个问题提供解决方法。从数据结构到集成前沿技术...
docker run -tid -p <port>:80 apachecn0/pandas-cookbook-code-notes # 访问 http://localhost:{port} 查看文档 PYPI pip install pandas-cookbook-code-notes pandas-cookbook-code-notes # 访问 ...
Lott -- Modern Python Cookbook -- 2016 -- code.7z
Aggarwal -- Flask Framework Cookbook -- 2014 -- code.7z
Subramanian -- Python Data Science Cookbook -- 2015 -- code.7z
Fine -- Python 2.6 Graphics Cookbook -- 2010 -- code.7z