By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Observer and Event design pattern.

P: n/a
Hello all,

I have the requirement to produce source code that produces an object
hierarchy.

Example:

Root
|
Folder 1
|
Object 1
Object 2
Folder 2
|
Object 1

These objects will displayed in a treeview much in the same way that MS
explorer does. The same object can appear under many folders. A user is
able to delete objects in view. When this occurs I must remove the other
object from any other folders. Still with me?

Problems... To solve this problem I implemented an observer pattern. This
observer has a hashtable of all created objects. When one is deleted the
observer notifies the other objects and they are responsible for removing
themselves. This seems to work great, but after digging around in MSDN I
found documentation that stated Microsoft did not implement standard
interfaces for the observer pattern because the 'Event pattern' using
delegates and events was the recommended way to implement an observer
pattern. I have sample source code that shows the 'Event Pattern' but I
cannot see a way to solve my problem using it! I know I have solved the
problem using a regular observer pattern but I would like to understand how
the event pattern could have been used?

Any help would be appreciated.

Thanks

From
http://www.fawcette.com/dotnetmag/20...dfox/page3.asp
VB .NET . Implement the Event Pattern
Listing 1. This simple example implements the .NET Framework Event Pattern
that's based on the Observer pattern. In this example the Product class
declares a Delegate that's used to define the signature of an event fired
when the number of items in stock changes. The client (Observer) can then
create a new Delegate object and dynamically bind it to the event fired by
the Product. The common language runtime handles the notification of the
observers by invoking the Delegate when the RaiseEvent statement executes.
In this case, VB.NET includes the Delegate, Event, RaiseEvent, AddHandler,
and RemoveHandler keywords to work with Delegates, while C# includes the
Delegate and event keywords.
Public Class Product

' Delegate to represent the event
Delegate Sub ProductStockChangedHandler( _
ByVal sender As Object, _
ByVal e As ProductStockChangedEventArgs)
' Declare an event that uses the delegate
Public Event ProductStockChanged As _
ProductStockChangedHandler

' Instance data for a product
Dim _inStock As Integer
Dim _productId As Integer

Public Sub New(ByVal productId As Integer)
_productId = productId
End Sub

Public Property ProductId() As Integer
Get
Return _productId
End Get
Set(ByVal Value As Integer)
_productId = Value
End Set
End Property

Public Property InStock() As Integer
Get
Return _inStock
End Get
Set(ByVal Value As Integer)
_inStock = Value
' Call the protected method to fire the event
OnProductStockChanged()
End Set
End Property

' Method used to fire the event
Protected Sub OnProductStockChanged()
RaiseEvent ProductStockChanged(Me, _
New ProductStockChangedEventArgs( _
Me.ProductId, _inStock))
End Sub

End Class

Public Class ProductStockChangedEventArgs
Inherits EventArgs

' Instance data for the event args
Dim _inStock As Integer
Dim _productId As Integer

Sub New(ByVal productId As Integer, _
ByVal inStock As Integer)
_inStock = inStock
_productId = productId
End Sub

Public ReadOnly Property InStock() As Integer
Get
Return _inStock
End Get
End Property

Public ReadOnly Property ProductId() As Integer
Get
Return _productId
End Get
End Property
End Class
Module app

' Method to handle the event in the client
Public Sub Notify(ByVal sender As Object, _
ByVal e As ProductStockChangedEventArgs)
Console.WriteLine( _
"The inventory level for product ID " & _
e.ProductId.ToString() & " is now " & _
e.InStock.ToString())
End Sub

Sub Main()

' Create a new product
Dim p As New Product(1)

' Register the handler
Dim p1 As New _
Product.ProductStockChangedHandler( _
AddressOf Notify)
AddHandler p.ProductStockChanged, p1

' Note that VB allows the previous
' syntax to be shortcutted as:
'AddHandler p.ProductStockChanged, _
AddressOf Notify

' Make the change
p.InStock = 15

' Dynamically unregister the handler
RemoveHandler p.ProductStockChanged, _
AddressOf Notify

End Sub

End ModuleVB .NET . Implement the Event Pattern
Listing 1. This simple example implements the .NET Framework Event
Pattern that's based on the Observer pattern. In this example the Product
class declares a Delegate that's used to define the signature of an event
fired when the number of items in stock changes. The client (Observer) can
then create a new Delegate object and dynamically bind it to the event fired
by the Product. The common language runtime handles the notification of the
observers by invoking the Delegate when the RaiseEvent statement executes.
In this case, VB.NET includes the Delegate, Event, RaiseEvent, AddHandler,
and RemoveHandler keywords to work with Delegates, while C# includes the
Delegate and event keywords.
Public Class Product

' Delegate to represent the event
Delegate Sub ProductStockChangedHandler( _
ByVal sender As Object, _
ByVal e As ProductStockChangedEventArgs)
' Declare an event that uses the delegate
Public Event ProductStockChanged As _
ProductStockChangedHandler

' Instance data for a product
Dim _inStock As Integer
Dim _productId As Integer

Public Sub New(ByVal productId As Integer)
_productId = productId
End Sub

Public Property ProductId() As Integer
Get
Return _productId
End Get
Set(ByVal Value As Integer)
_productId = Value
End Set
End Property

Public Property InStock() As Integer
Get
Return _inStock
End Get
Set(ByVal Value As Integer)
_inStock = Value
' Call the protected method to fire the event
OnProductStockChanged()
End Set
End Property

' Method used to fire the event
Protected Sub OnProductStockChanged()
RaiseEvent ProductStockChanged(Me, _
New ProductStockChangedEventArgs( _
Me.ProductId, _inStock))
End Sub

End Class

Public Class ProductStockChangedEventArgs
Inherits EventArgs

' Instance data for the event args
Dim _inStock As Integer
Dim _productId As Integer

Sub New(ByVal productId As Integer, _
ByVal inStock As Integer)
_inStock = inStock
_productId = productId
End Sub

Public ReadOnly Property InStock() As Integer
Get
Return _inStock
End Get
End Property

Public ReadOnly Property ProductId() As Integer
Get
Return _productId
End Get
End Property
End Class
Module app

' Method to handle the event in the client
Public Sub Notify(ByVal sender As Object, _
ByVal e As ProductStockChangedEventArgs)
Console.WriteLine( _
"The inventory level for product ID " & _
e.ProductId.ToString() & " is now " & _
e.InStock.ToString())
End Sub

Sub Main()

' Create a new product
Dim p As New Product(1)

' Register the handler
Dim p1 As New _
Product.ProductStockChangedHandler( _
AddressOf Notify)
AddHandler p.ProductStockChanged, p1

' Note that VB allows the previous
' syntax to be shortcutted as:
'AddHandler p.ProductStockChanged, _
AddressOf Notify

' Make the change
p.InStock = 15

' Dynamically unregister the handler
RemoveHandler p.ProductStockChanged, _
AddressOf Notify

End Sub

End Module
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.