Observer mode for Design patterns (Observer pattern) (II.)
Source: Internet
Author: User
server| Design the message passed in the example above is a string, and when we click on one of the radio button, we can get the name of the radio button (Red Blue Green) and send that name to the Observer (Observer).
Observer provides an abstract connection to a target principal (Subject), a target principal (Subject) does not know any information about his observer (Observer), but can update the notification when he changes to a registered observer (OBSERVER).
We can also use a delegate (Delegate) to implement the observer pattern
Public Class Subject
' Define Delegate
Public Delegate Sub sendmessagedelegate (ByVal aprice as String)
' Define Event
Public Event SendMessage as Sendmessagedelegate
Dim _askprice as String
Public WriteOnly Property Message () as String
Set (ByVal Value as String)
_askprice = Value
' Raising an Event
RaiseEvent SendMessage (_askprice)
End Set
End Property
End Class
In the main form:
Dim Lscol as New listobs
Dim Frcol as New FRMD
Public Sub New ()
MyBase.New ()
Form1 = Me
InitializeComponent ()
Dim EVH as EventHandler = New EventHandler (AddressOf radiohandler)
AddHandler Opred.click, EVH
AddHandler Opblue.click, EVH
AddHandler Opgreen.click, EVH
Lscol. Show () ' shows two observer
Frcol. Show ()
End Sub
' Handle the Click event of the radio button
Protected Sub Radiohandler (ByVal sender as Object, ByVal e as EventArgs)
Dim I as Integer
Dim rbut as Windows.Forms.RadioButton = CType (sender, Windows.Forms.RadioButton)
Dim subject as New subject
Dim Adelegate as subject. Sendmessagedelegate = New subject. Sendmessagedelegate (AddressOf Lscol. ADD)
Dim aDelegate1 as subject. Sendmessagedelegate = New subject. Sendmessagedelegate (AddressOf Frcol. Drawmessage)
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.