Observer Pattern (1)

zhaozj2021-02-16  47

We usually represent our data in a variety of different forms, such as lists, graphics, and more.

We also wish to notify all objects that depend on its object when data changes.

For example: We can use graphics, forms, or list boxes to display stock prices. When the price of stock changes, we expect to change the other parts.

In this case we can use the OBServer mode. We can easily use Observer mode to make our program can easily solve the above problems.

Structural map:

The observer mode assumes that the object of the object and the object of display data are separated, and the objects to display the data objects to observe the changes in the data object. (As shown)

When we want to implement OBServer mode, the data object is usually used as a target, and the object of each display data is used as an Observer. Each Observer is registered in the data he is interested in data by calling a publication in the target (SubiC). Thus, when the data changes, each target transmits an update notification through the Observer interface.

We define two interfaces:

'Observer.vb

'Define Observer interface

Public Interface Observer

Sub sendnotify (Byval Mesg As String) 'is used to send update notifications

End interface

SUBJECT.VB

'Define the Subject interface

Public Interface Subject

Sub Registerinterest (Byval Obs As Observer) 'for registering the observer

End interface

We can write a simple program to better understand the obselver observer mode.. The first form-main form, which has 3 radio button, named Red, Blue and Green.

Our main window is implemented on the Subject interface, which means it must provide public method REGISTEREST for registration (OBSERVER) observer.

Public Sub Registerinterest (Byval Obs As VBServer.observer) IMPLEments VBNETOBSERVER.SUBJECT.REGISTERINTEREST

Observers.Add (OBS)

End Sub

We build two OBServer, a display color, and another color in a list box. We show them in the main window body class.

DIM LSCOL AS New Listobs (ME)

Lscol.show ()

Dim Frcol As New Colframe (ME)

Frcol.show ()

The first observer (OBServer) --------- ColorFrame, it implements the OBServer interface, below is part of the main code.

'Class Colorframe

Public Class Colframe

Inherits System.Windows.Forms.form

Implements Observer 'implements 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", 18, 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 an Observer observer that adds colors to the list. Here are some 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 any of the Radio Button in the main form, which will call the SendNotify method of each registered OBServer.

The effect of operation is:

Reference:

Design Patterns: the use of C # Delegate to improve the Observer pattern of: Lu Yan explore the observer design pattern

I just started learning the design mode in .NET, I hope to help everyone in the learning process!

Can write let me exchange!

转载请注明原文地址:https://www.9cbs.com/read-23477.html

New Post(0)