1. Observer Pattern

The observer pattern allows a type instance to "publish" events to other type instances ("observers") who wish to be updated when a particular event occurs.

1.1. Implementation

In long-running applications—such as webservers—instances can keep a collection of observers that will receive notification of triggered events.

Implementations vary, but interfaces can be used to make standard observers and notifiers:

type (
    // Event defines an indication of a point-in-time occurrence.
    Event struct {
        // Data in this case is a simple int, but the actual
        // implementation would depend on the application.
        Data int64

    // Observer defines a standard interface for instances that wish to list for
    // the occurrence of a specific event.
    Observer interface {
        // OnNotify allows an event to be "published" to interface implementations.
        // In the "real world", error handling would likely be implemented.

    // Notifier is the instance being observed. Publisher is perhaps another decent
    // name, but naming things is hard.
    Notifier interface {
        // Register allows an instance to register itself to listen/observe
        // events.
        // Deregister allows an instance to remove itself from the collection
        // of observers/listeners.
        // Notify publishes new events to listeners. The method is not
        // absolutely necessary, as each implementation could define this itself
        // without losing functionality.

1.2. Usage

For usage, see observer/main.go or view in the Playground.

Copyright © studygolang.com 2013 all right reserved,powered by Gitbook该文件修订时间: 2017-07-21 20:12:30

results matching ""

    No results matching ""