Observer Mode
The Observer model is designed to meet the need for listening. That is, when something happens, one or more observers need to be aware of the event, and if each observer uses polling to determine whether the event occurs, it consumes more resources. So the task should be done by the observed, that is, the obser
The Observer pattern (sometimes referred to as publish (publish)-Subscription (Subscribe), model-view, source-listener (Listener) or slave mode) is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it, and actively notifies it when its own state changes. This is usually done by calling the methods provided by the various observers. This
Original articles, reproduced please be sure to put the following paragraph at the beginning of the article.This article is forwarded from Jason's Blog, the original link http://www.jasongj.com/design_pattern/observer/Observer pattern to introduce The Observer pattern definition
Observer mode is also called the Publish
In general, the definition of the observer pattern should be this: building a clean interface between the source of news that some object has changed and the Consumers O F that news.
The Observer pattern establishes a clean interface between the producer and the consumer of the message, thus making the coupling between the producer and the consumer of the message abstract. The
write two methods, such as flight methods, as shown below
public void Performfly () {flybehavior.fly ();}
So Ducks have their own flying behavior, we only need to instantiate the duck, such as in the constructor to initialize the corresponding flight and name of the specific class can be.
4. Third design principle: multi-use combination, less inheritance.
The above tells a design patternstrategy mode, and is a multiple-policy model, there are flight strategies, there is a name of the strategy,
The observer model is one of the more commonly used patterns and the basis for other common patterns, such as the MVC pattern, which is a model based on observer mode.
In the Java language, the implementation of observer mode is very simple, that is, let the observer inherit the observable class, and let the
Tag: Java logs this dependent for Code mode packThe observer pattern for Java design patternsHave always wanted to write a study of the Observer Pattern Summary no opportunity, today learning to block the principle of the queue to see in the implementation of the producer consumer when the notification mode, is the so-called observer mode, just by the way to tidy
The actors involved in the Observer pattern are:
Abstract theme (Subject) role: Abstract theme Roles Keep all references to observer objects in a cluster (such as a ArrayList object), each subject can have any number of observers. Abstract topics provide an interface for adding and removing observer objects, and abstract theme roles are also called abstract-
Observer mode:
The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.
Abstract topic role: The topic
Observer pattern: A one-to-many dependency between objects that allows multiple observer objects to simultaneously monitor a Subject object (observed).
So that when the state of an object changes, all objects that depend on it are notified and changed accordingly.There are many ways to implement the Observer pattern: The pattern must contain two roles of the
What is the observation of the pattern
When multiple observers want to observe the same subject object at the same time, the subject matter is the change of the state of our observer, and the way in which the observer can recall itself is old and updated.Implementation steps: First: Define an observer interface The second ob
Import java.util.Observable;
Import Java.util.Observer;
The/** Java API provides us with observer interfaces and observable classes to implement the so-called observer pattern.
The observable (Observer) class allows you to notify other objects (Implementation interface Observer,
Observer Pattern)
Overview
The observer mode is used to establish a dependency between an object and an object. When an object changes, other objects are automatically notified, and other objects are responded accordingly. In the observer mode, the changed object is called the observation object, and the notified object is called the
Observer Design Pattern
Observer mode: defines a one-to-many dependency between objects. When the status of an object changes,All objects dependent on it are notified and automatically updated.
In this definition, the first keyword is dependency, which is a type of relationship between classes. Dependency refers to dependency between one class and another class, generally, objects are transmitted in the met
Observer Pattern Definition:
Define a one-to-many dependency between objects so then one object changes state, all its dependents AER notified and Updated automatically.
Defines a one-to-many dependency between objects so that when an object changes state, all objects that depend on it are notified and automatically updated.
as shown in the figure above (intercepted from the head Patterns), it consists of four main parts:
1. Subject the observed per
Server [Introduction]
Microsoft's top technology guru Jeffrey Richter's work has always been a must-see. This article is no exception to help developers with this monograph on Observer mode. Observer mode is one of the most widely used and flexible models in classical design mode. This article in. NET technology under the framework of in-depth exploration of the connotation of the
What is the Observer pattern? Let's make an analogy, it's like you're ordering a newspaper. For example, you want to know that the United States recently released some news, you may subscribe to an American weekly, and once the United States has a new story, the United States Weekly will send a magazine, and mailed to you, when you receive the newspaper, then you will be able to understand the latest developments in the United States. In fact, this is
Use
Observer Mode (Observer)
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time.This subject object notifies all observer objects when the state changes, enabling them to a
In the previous section we explained some of the knowledge of the observer pattern and customized the classic code of the Observer Pattern, (Teleport: Design mode go through--observer mode (above))
This article briefly introduces the observer Pattern implementation code that comes with the JDK.
For the
One, the Observer pattern definition:Defines a one-to-many dependency between objects, so that when an object changes state, all of his dependents are notified and updated automatically.The Observer provides an object design that allows for a loose coupling between the subject and the observer.Second, the components:1. Abstract target Role (Subject): Also known as the subject, the target role knows his
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.