473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6995

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paolino | last post by:
Lately I was needing to use multiple inheritance to split behaviour of a class and modularize it. But the problem raises when the need is to add operations to a method already present in one of...
3
by: Michael Schneider | last post by:
Hello All, I am comming back to python after being away for several years. I would like to use weak refs in an observer pattern implementation. The problme that I have seems to be that...
4
by: decrypted | last post by:
Since I couldn't find a OO design/architext forum, I thought I would post here... I have a win app with forms management. forms are grouped by category (pertains to company, pertains to project....
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
0
by: FluffyCat | last post by:
Last week I continued my series of design patterns examples using PHP 5. Here now is my 14th example, the Observer pattern. http://www.fluffycat.com/PHP-Design-Patterns-Observer/ In the...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
1
by: Christopher | last post by:
The observer pattern itself is easy enough. I've implemented it using a Event that contains data for any Event type I forsee my application using. My problem is I don't want one and only one...
4
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a...
5
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #1. (I'll put the other is a separate post.) Here is a standard example of the observer pattern in Python:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.