The observer pattern is highlighted in design mode because it uses a lot of scenarios and plays a very important role in modular design. The bottom of the MVC pattern is the observer pattern, and the current popular JavaScript framework backbone is one of the best frameworks for using this pattern. The Observer pattern gives us some ideas that we can use to write
Observer mode (also known as publisher-Subscriber mode) should be one of the most common patterns. are used in a lot of languages. Including the DOM events we normally touch. It is also an observer pattern implemented between JS and Dom.
Copy Code code as follows:
Div.onclick = function Click () {
Alert ("click")
}
Just subscribe to the div click event. When you click on the di
Design patterns are not as difficult as we think. We carefully understand the concept of a good design pattern, know what it is to solve the problem of this to better grasp. Let's look at the Observer pattern in the design pattern below.
(1) The concept of the Observer model
Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified
One: What is the Observer pattern:Official definition: Defines a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.For example, many people will subscribe to the weather forecast, and when the weather station gets tomorrow ( target object ), it will notify the person who subscribed to the weather forecast (the observer
On a blog with you to share the Android source of the decorator mode, a little meaning, today with you to share the viewer mode in Android, by the way, the observer mode and the relationship between the callback mechanism, welcome everyone to shoot bricks.Observer Pattern DefinitionThe Observer pattern defines a one-to-many dependency that allows multiple observer
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,
The observer mode perfectly separates the observer from the observed object. For example, the user interface can act as an observer. business data is observed. The user interface observes changes in business data and displays the changes on the interface. One principle of object-oriented design is that every class in the system focuses on a certain function, rath
Ubiquitous Design Pattern in Android development-Observer PatternUbiquitous Design Pattern in Android development-Singleton pattern: ubiquitous Design Pattern in Android development-Builder Pattern
The Singleton mode and Builder mode are introduced earlier. For more information, see the two links above. This article focuses on the observer mode. Let's take a look at the definition of this mode.
Defines a
PrefaceLet's briefly talk about the definition and composition of the observer pattern.
The Observer pattern is set in relation to the compositionIn layman's terms, the Observer pattern defines a one-to-many dependency that allows multiple observer objects to simultaneously listen to a Subject object. When the subjec
The Design Observer pattern (Observer) perfectly separated the observer from the observed object. For example, the user interface can be used as an observer, the business data is observed, the user interface observes the changes in the business data, and when the data changes, it is displayed in the interface. One of t
* In simple terms, the viewer mode = Publisher + Subscriber. Here is a typical example of a headhunter. In the following diagram, there are two characters:* Headhunters and people looking for work. The person who is looking for a job subscribes to a headhunter and tells that he wants to get a job, and when there is a new job opportunity,* The Headhunter will notify the person who has subscribed to the message.Headhunting Interface:Package com.pattern.observer;/** * @author Qixuan.chen * Created:
first, the definitionObserver mode (Observer), when the state of an object changes, all dependent objects are notified and updated automatically.# The Observer pattern uses "combination" to combine many observers into the subject. This relationship between objects (the Observer-subject) is not generated by inheritance, but by the way the composition is used at ru
ConceptThe observer pattern, also called the Publish subscription pattern (Publish/subscribe), defines a one-to-many relationship that allows multiple observer objects to listen to a Subject object at the same time, notifying all observer objects when the state of the subject changes, so that they can automatically update themselves.Role
Supports simple
Observer.h
#ifndef _observer_h_ #define _observer_h_ #include
Observer.cpp
#include
Subject.h
#ifndef _subject_h_ #define _subject_h_ #include
Subject.cpp
#include
Main.cpp
#include
Observer mode is one of the most widely used and influential models. Our well-known MVC (MODEL/VIEW/CONTROL) employs the Observer model. MVC realizes the decoupling of the business logic and the presentation layer. In MFC,
The observer mode is a mature mode for multiple objects to know data changes in an object. In the observer mode, there is an object called "theme" and several objects called "Observer". The relationship between "theme" and "Observer" is a one-to-many dependency.. When the status of the "topic" changes, all "observers"
Intention:
Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated "GOF95"
Also known as publish-subscribe (publish-subscribe) mode, model-view (model-view) mode, source-monitor (Source-listener) mode, or dependent (dependents) mode
Structure Chart:
Main roles
Abstract theme (Subject) role: The theme role holds all references to the Observer
Intention:Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.Applicability:When an abstract model has two facets, one aspect depends on the other. The two are encapsulated in separate objects so that they can be individually changed and reused.When a change to an object needs to change other objects at the same time, it is not known how many objects need to be changed.When an object must notif
There have been a lot of online sharing and notes on the design model. I am still writing this article about nothing new. This is mainly to record my recent learning and life, in addition, I hope to use this blog post to restart my blog to fill my life. I haven't written anything for a long time. I have been watching my blog for a long time and looking back at the blank days of my memory, I felt a little scared. after such a long period of time, I did not even leave any trace. My existence was t
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.