2 people like it.

F# Observer Pattern

Observer Pattern

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
// Observer Pattern
module StringWithEvent =

    type StringChangedEvent() =
        member val RegisteredFunctions = new ResizeArray<string*string->unit>() with get
        member S.Add f = S.RegisteredFunctions.Add f

    type String( s :string ) =
        let mutable str = s
        member val Changed = new StringChangedEvent() with get
        member S.Value
            with get() = str
            and set v =
                if str <> v then
                    for f in S.Changed.RegisteredFunctions do
                        f (str,v)
                    str <- v

// Testing
let str = StringWithEvent.String("Ok")
str.Changed.Add(fun (oldStr, newStr) ->
                    printfn "String changed from %A to %A" oldStr newStr )
str.Value <- "Okey"
Multiple items
type StringChangedEvent =
  new : unit -> StringChangedEvent
  member Add : f:(string * string -> unit) -> unit
  member RegisteredFunctions : ResizeArray<(string * string -> unit)>

Full name: Script.StringWithEvent.StringChangedEvent

--------------------
new : unit -> StringChangedEvent
type ResizeArray<'T> = System.Collections.Generic.List<'T>

Full name: Microsoft.FSharp.Collections.ResizeArray<_>
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val S : StringChangedEvent
member StringChangedEvent.Add : f:(string * string -> unit) -> unit

Full name: Script.StringWithEvent.StringChangedEvent.Add
val f : (string * string -> unit)
property StringChangedEvent.RegisteredFunctions: ResizeArray<(string * string -> unit)>
System.Collections.Generic.List.Add(item: string * string -> unit) : unit
Multiple items
module String

from Microsoft.FSharp.Core

--------------------
type String =
  new : s:string -> String
  member Changed : StringChangedEvent
  member Value : string
  member Value : string with set

Full name: Script.StringWithEvent.String

--------------------
new : s:string -> String
val s : string
val mutable str : string
val S : String
member String.Value : string with set

Full name: Script.StringWithEvent.String.Value
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : string
property String.Changed: StringChangedEvent
val str : StringWithEvent.String

Full name: Script.str
module StringWithEvent

from Script
Multiple items
type String =
  new : s:string -> String
  member Changed : StringChangedEvent
  member Value : string
  member Value : string with set

Full name: Script.StringWithEvent.String

--------------------
new : s:string -> StringWithEvent.String
property StringWithEvent.String.Changed: StringWithEvent.StringChangedEvent
member StringWithEvent.StringChangedEvent.Add : f:(string * string -> unit) -> unit
val oldStr : string
val newStr : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property StringWithEvent.String.Value: string
Raw view Test code New version

More information

Link:http://fssnip.net/k7
Posted:11 years ago
Author:Evanescent Devil
Tags: design patterns , observer pattern