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.

' Class colorframe

Public Class Colframe

Inherits System.Windows.Forms.Form

Implements Observer ' implementation Observer interface

Private ColName as String

Dim Fnt as Font

Dim Bbrush as SolidBrush

' Constructor

Public Sub New (ByVal subj as Subject)

MyBase.New ()

Subj.registerinterest (ME) ' Register Observer

Colframe = Me

InitializeComponent ()

FNT = New Font ("Arial", Drawing.FontStyle.Bold)

Bbrush = New SolidBrush (color.black)

AddHandler Pic.paint, New forms.painteventhandler (AddressOf painthandler)

End Sub



Public Shadows Sub Dispose ()

Mybase.dispose ()

Components. Dispose ()

End Sub

'----

Public Sub sendnotify (ByVal mesg as System.String) Implements VBNetObserver.Observer.sendNotify

ColName = MESG

Select case MESG. ToLower

Case "Red"

Pic.backcolor = Color.Red '

Case "Blue."

Pic.backcolor = Color.Blue

Case "green."

Pic.backcolor = Color.green

End Select

End Sub

End Class

Our ListBox form is also a observer observer, which adds color to the list. The following are some of the main code.

Public Class Listobs

Inherits System.Windows.Forms.Form

Implements Observer

Public Sub New (ByVal subj as Subject)

MyBase.New ()

Listobs = Me

InitializeComponent ()

Subj.registerinterest (Me)

End Sub

'-----

Public Shadows Sub Dispose ()

Mybase.dispose ()

Components. Dispose ()

End Sub

'-----

Public Sub sendnotify (ByVal mesg as System.String) Implements observer.sendnotify

Lscolors. Items.Add (MESG)

End Sub

End Class



In our program, click on any of the radio button in the main form and it will invoke the Sendnotify method of each registered observer (Observer).

The effect of the operation is:



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.