81 people like it.

Factory Pattern

Factory pattern in F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
type IA = 
  abstract Action : unit -> unit

type Type = 
  | TypeA 
  | TypeB

let factory = function
  | TypeA -> { new IA with 
                   member this.Action() = printfn "type A" }
  | TypeB -> { new IA with 
                  member this.Action() = printfn "type B" }
abstract member IA.Action : unit -> unit

Full name: Script.IA.Action
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
type Type =
  | TypeA
  | TypeB

Full name: Script.Type
union case Type.TypeA: Type
union case Type.TypeB: Type
val factory : _arg1:Type -> IA

Full name: Script.factory
type IA =
  interface
    abstract member Action : unit -> unit
  end

Full name: Script.IA
val this : IA
abstract member IA.Action : unit -> unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/7q
Posted:13 years ago
Author:Tao Liu
Tags: design patterns