Insoftware design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
It is often used for implementing distributed event-handling systems in event-driven software. In such systems, the subject is usually named a "stream of events" or "stream source of events" while the observers are called "sinks of events." The stream nomenclature alludes to a physical setup in which the observers are physically separated and have no control over the emitted events from the subject/stream source. This pattern thus suits any process by which data arrives from some input that is not available to the CPU at startup, but instead may arrive at arbitrary or indeterminate times (HTTP requests, GPIO data, user input from peripherals and distributed databases, etc.).
The observer design pattern is a behavioural pattern listed among the 23 well-known "Gang of Four" design patterns that address recurring design challenges in order to design flexible and reusable object-oriented software, yielding objects that are easier to implement, change, test and reuse.[1]
Defining a one-to-many dependency between objects by defining one object (subject) that updates the state of dependent objects directly is inflexible because it couples the subject to particular dependent objects. However, it might be applicable from a performance point of view or if the object implementation is tightly coupled (such as low-level kernel structures that execute thousands of times per second). Tightly coupled objects can be difficult to implement in some scenarios and are not easily reused because they refer to and are aware of many objects with different interfaces. In other scenarios, tightly coupled objects can be a better option because the compiler is able to detect errors at compile time and optimize the code at the CPU instruction level.
The sole responsibility of a subject is to maintain a list of observers and to notify them of state changes by calling their update() operation. The responsibility of observers is to register and unregister themselves with a subject (in order to be notified of state changes) and to update their state (to synchronize their state with the subject's state) when they are notified. This makes subject and observers loosely coupled. Subject and observers have no explicit knowledge of each other. Observers can be added and removed independently at run time. This notification-registration interaction is also known as publish-subscribe.
The observer pattern can cause memory leaks, known as the lapsed listener problem, because in a basic implementation, it requires both explicit registration and explicit deregistration, as in the dispose pattern, because the subject holds strong references to the observers, keeping them alive. This can be prevented if the subject holds weak references to the observers.
Typically, the observer pattern is implemented so that the subject being observed is part of the object for which state changes are being observed (and communicated to the observers). This type of implementation is considered tightly coupled, forcing both the observers and the subject to be aware of each other and have access to their internal parts, creating possible issues of scalability, speed, message recovery and maintenance (also called event or notification loss), the lack of flexibility in conditional dispersion and possible hindrance to desired security measures. In some (non-polling) implementations of the publish-subscribe pattern, this is solved by creating a dedicated message queue server (and sometimes an extra message handler object) as an extra stage between the observer and the object being observed, thus decoupling the components. In these cases, the message queue server is accessed by the observers with the observer pattern, subscribing to certain messages and knowing (or not knowing, in some cases) about only the expected message, while knowing nothing about the message sender itself; the sender may also know nothing about the observers. Other implementations of the publish-subscribe pattern, which achieve a similar effect of notification and communication to interested parties, do not use the observer pattern.[3][4]
In early implementations of multi-window operating systems such as OS/2 and Windows, the terms "publish-subscribe pattern" and "event-driven software development" were used as synonyms for the observer pattern.[5]
The observer pattern, as described in the Design Patterns book, is a very basic concept and does not address removing interest in changes to the observed subject or special logic to be performed by the observed subject before or after notifying the observers. The pattern also does not deal with recording change notifications or guaranteeing that they are received. These concerns are typically handled in message-queueing systems, in which the observer pattern plays only a small part.
The observer pattern may be used in the absence of publish-subscribe, as when model status is frequently updated. Frequent updates may cause the view to become unresponsive (e.g., by invoking many repaint calls); such observers should instead use a timer. Instead of becoming overloaded by change message, the observer will cause the view to represent the approximate state of the model at a regular interval. This mode of observer is particularly useful for progress bars, in which the underlying operation's progress changes frequently.
In this UML class diagram, the Subject class does not update the state of dependent objects directly. Instead, Subject refers to the Observer interface (update()) for updating state, which makes the Subject independent of how the state of dependent objects is updated. The Observer1 and Observer2 classes implement the Observer interface by synchronizing their state with subject's state.
The UML sequence diagram shows the runtime interactions: The Observer1 and Observer2 objects call attach(this) on Subject1 to register themselves. Assuming that the state of Subject1 changes, Subject1 calls notify() on itself. notify() calls update() on the registered Observer1 and Observer2objects, which request the changed data (getState()) from Subject1 to update (synchronize) their state.
Below is an example written in Java that takes keyboard input and handles each input line as an event. When a string is supplied from System.in, the method notifyObservers() is then called in order to notify all observers of the event's occurrence, in the form of an invocation of their update methods.
JavaScript has a deprecated Object.observe function that was a more accurate implementation of the observer pattern.[7] This would fire events upon change to the observed object. Without the deprecated Object.observe function, the pattern may be implemented with more explicit code:[8]
The North Pacific Observer Program (Observer Program) plays a vital role in the conservation and management of the Bering Sea, Aleutian Islands, and Gulf of Alaska groundfish and halibut fisheries. The program trains, briefs, debriefs, and oversees over 450 observers annually who collect catch data onboard fishing vessels and at onshore processing plants that is used for in-season management and scientific purposes such as stock assessments and ecosystem studies. The program ensures that the data collected by observers are of the highest quality possible by implementing rigorous quality control and quality assurance processes for the data collected by observers.
The Observer Program provides the regulatory framework for NOAA Fisheries certified observers to collect data on groundfish and halibut fisheries. The information collected by observers provides the best scientific information to manage the fisheries and to develop measures to minimize bycatch. Observers collect biological samples and fishery-dependent information on total catch and interactions with protected species. Managers use data collected by observers to monitor quotas, manage groundfish and prohibited species catch, and document and reduce fishery interactions with protected resources. Division staff process data and make it available to the Sustainable Fisheries Division of the Alaska Regional Office for quota monitoring, to scientists at the Alaska Fisheries Science Center for stock assessment, ecosystem investigations, and an array of research investigations, as well as the fishing industry itself which relies on observer data to monitor quotas and prohibited species catch (PSC).
In January 2013, NOAA Fisheries changed how observers in the partial coverage category are deployed, how observer coverage in the partial coverage category is funded, and which vessels and processors must have some or all of their operations observed. These changes increased the statistical reliability of data collected by the program, addressed cost inequality among fishery participants, and expanded observer coverage to previously unobserved fisheries. This program information constitutes the Small Entity Compliance Guide required under section 212 of the Small Business Regulatory Enforcement Fairness Act of 1996.
The Observer Program is implemented by regulations at subpart E of 50 CFR part 679 which authorize the deployment of observers and EM to collect information necessary for the conservation and management of the Bering Sea and Aleutian Islands and Gulf of Alaska groundfish and halibut fisheries. The information collected by observers provides the best available scientific information to manage the fisheries and to develop measures to minimize bycatch. Observers collect biological samples and fishery-dependent information on total catch and interactions with protected species. Managers use data collected by observers and electronic monitoring to monitor quotas, manage groundfish and prohibited species catch, and document and reduce fishery interactions with protected resources. Scientists use observer-collected data for stock assessments and marine ecosystem research.
3a8082e126