Observer and Event design pattern.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Read

    #1

    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

    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 ProductStockCha ngedHandler( _
    ByVal sender As Object, _
    ByVal e As ProductStockCha ngedEventArgs)
    ' Declare an event that uses the delegate
    Public Event ProductStockCha nged As _
    ProductStockCha ngedHandler

    ' 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
    OnProductStockC hanged()
    End Set
    End Property

    ' Method used to fire the event
    Protected Sub OnProductStockC hanged()
    RaiseEvent ProductStockCha nged(Me, _
    New ProductStockCha ngedEventArgs( _
    Me.ProductId, _inStock))
    End Sub

    End Class

    Public Class ProductStockCha ngedEventArgs
    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 ProductStockCha ngedEventArgs)
    Console.WriteLi ne( _
    "The inventory level for product ID " & _
    e.ProductId.ToS tring() & " is now " & _
    e.InStock.ToStr ing())
    End Sub

    Sub Main()

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

    ' Register the handler
    Dim p1 As New _
    Product.Product StockChangedHan dler( _
    AddressOf Notify)
    AddHandler p.ProductStockC hanged, p1

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

    ' Make the change
    p.InStock = 15

    ' Dynamically unregister the handler
    RemoveHandler p.ProductStockC hanged, _
    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 ProductStockCha ngedHandler( _
    ByVal sender As Object, _
    ByVal e As ProductStockCha ngedEventArgs)
    ' Declare an event that uses the delegate
    Public Event ProductStockCha nged As _
    ProductStockCha ngedHandler

    ' 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
    OnProductStockC hanged()
    End Set
    End Property

    ' Method used to fire the event
    Protected Sub OnProductStockC hanged()
    RaiseEvent ProductStockCha nged(Me, _
    New ProductStockCha ngedEventArgs( _
    Me.ProductId, _inStock))
    End Sub

    End Class

    Public Class ProductStockCha ngedEventArgs
    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 ProductStockCha ngedEventArgs)
    Console.WriteLi ne( _
    "The inventory level for product ID " & _
    e.ProductId.ToS tring() & " is now " & _
    e.InStock.ToStr ing())
    End Sub

    Sub Main()

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

    ' Register the handler
    Dim p1 As New _
    Product.Product StockChangedHan dler( _
    AddressOf Notify)
    AddHandler p.ProductStockC hanged, p1

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

    ' Make the change
    p.InStock = 15

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

    End Sub

    End Module


Working...