472,096 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Observer and Event design pattern.

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
0 6874

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Paolino | last post: by
3 posts views Thread by Michael Schneider | last post: by
4 posts views Thread by decrypted | last post: by
reply views Thread by Andy Read | last post: by
reply views Thread by FluffyCat | last post: by
22 posts views Thread by Krivenok Dmitry | last post: by
1 post views Thread by Christopher | last post: by
4 posts views Thread by Mohamed Mansour | last post: by
5 posts views Thread by Alan Isaac | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.