Observer mode for Design patterns (Observer pattern) (I.)
Source: Internet
Author: User
server| design We usually represent our data in a variety of different forms at the same time, such as lists, graphs, and so on.
We also hope that when the data changes, it is convenient to notify all the objects that depend on it.
For example: we can use graphs, tables, or list boxes to display the price of a stock, and when the price of a stock changes, we expect to change the other parts conveniently.
In this case, we can use the Observer mode. We can easily use the Observer mode so that our program can easily solve the above problems.
Structure Chart:
The Observer pattern assumes that the object that holds the data is separate from the object that is displaying the data, and that the object that displays the data is the change in the observation data object. (as pictured)
When we implement the Observer pattern, the data objects are usually targeted (Subject), and the objects that display the data are the observers (Observer). Each observer (Observer) registers (registers) himself in the data that he or she is interested in by calling a publicly (public) method in the target (Subject). Thus, when the data changes, each target (Subject) sends an update notification through the Observer (Observer) interface.
We define the following two interfaces:
' Observer.vb
' Define observer interface
Public Interface Observer
Sub sendnotify (ByVal mesg as String) ' used to send update notifications
End Interface
Subject.vb
' Define subject Interface
Public Interface Subject
Sub registerinterest (ByVal OBS as observer) ' for registering observers
End Interface
We can write a simple program to better understand the (OBSERVER) observer pattern. First form-The main form, which has 3 radio Button, named Red, Blue and Green.
Our main form class implements the subject interface, which means that it must provide a public method registerinterest for registering (Observer) observers.
Public Sub registerinterest (ByVal obs as Vbnetobserver.observer) Implements VBNetObserver.Subject.registerInterest
Observers. ADD (OBS)
End Sub
We created two observer (Observer), one display color, and the other added color in a list box. We show them in the main form class.
Dim Lscol as New listobs (Me)
Lscol. Show ()
Dim Frcol as New colframe (Me)
Frcol.show ()
The first observer (Observer)---------Colorframe, which implements the Observer interface, and the following is part of the main code.
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.