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)

' Wiring

AddHandler subject. SendMessage, Adelegate

AddHandler subject. SendMessage, ADelegate1

' AddHandler subject. SendMessage, AddressOf Lscol. Add

' AddHandler subject. SendMessage, AddressOf Frcol. Drawmessage

Subject. Message = Rbut. Text

' Undo Wiring

RemoveHandler subject. SendMessage, Adelegate

RemoveHandler subject. SendMessage, ADelegate1

End Sub

Define the events that the delegate points to in the ListBox form and the Colorframe form respectively:

' ListBox form

Public Sub Add (ByVal mesg as System.String)

LSCOLORS.ITEMS.ADD (MESG)

End Sub

' Colorframe form

Public Sub drawmessage (ByVal mesg as System.String)

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

This can also achieve the same effect.



Resources:

Design pattern: Using C # 's delegate to improve observer mode Author: Lu Yan
Explore the Observer design pattern

Just started learning design patterns in. NET in the application, I hope that in the learning process to help!

Can write to me to communicate!


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.