// 发布者基类包含订阅管理代码和通知方法。 classEventManager is private field listeners: hash map of event types and listeners
method subscribe(eventType, listener) is listeners.add(eventType, listener)
method unsubscribe(eventType, listener) is listeners.remove(eventType, listener)
method notify(eventType, data) is foreach(listener in listeners.of(eventType)) do listener.update(data)
// 具体发布者包含一些订阅者感兴趣的实际业务逻辑。我们可以从发布者基类中扩 // 展出该类,但在实际情况下并不总能做到,因为具体发布者可能已经是子类了。 // 在这种情况下,你可用组合来修补订阅逻辑,就像我们在这里做的一样。 classEditor is public field events: EventManager private field file: File
constructor Editor()is events=newEventManager()
// 业务逻辑的方法可将变化通知给订阅者。 method openFile(path) is this.file = newFile(path) events.notify("open", file.name)
method saveFile() is file.write() events.notify("save", file.name)
// ...
// 这里是订阅者接口。如果你的编程语言支持函数类型,则可用一组函数来代替整 // 个订阅者的层次结构。 interfaceEventListener is method update(filename)
// 具体订阅者会对其注册的发布者所发出的更新消息做出响应。 classLoggingListenerimplementsEventListener is private field log: File private field message
constructor LoggingListener(log_filename, message) is this.log = newFile(log_filename) this.message = message
method update(filename) is log.write(replace('%s',filename,message))
classEmailAlertsListenerimplementsEventListener is private field email: string
constructor EmailAlertsListener(email, message) is this.email = email this.message = message
method update(filename) is system.email(email, replace('%s',filename,message))
// 应用程序可在运行时配置发布者和订阅者。 classApplication is method config()is editor=newEditor()