1 people like it.

F# Factory Pattern

Factory Pattern

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
// Factory Pattern
[<AbstractClassAttribute>]
type Connection() =
    abstract member Description :unit -> unit

type OracleConnection() =
    inherit Connection()
    override O.Description() = printfn "I'm Oracle Connection"

type SQLServerConnection() =
    inherit Connection()
    override O.Description() = printfn "I'm SQLServer Connection"

type MySQLConnection() =
    inherit Connection()
    override O.Description() = printfn "I'm MySQL Connection"

type Factory() =
    member O.CreateConnection( connName ) =
        match connName with
        | "Oracle" -> OracleConnection() :> Connection
        | "SQLServer" -> SQLServerConnection() :> Connection
        | "MySQL" -> MySQLConnection() :> Connection
        | _ -> failwith "No such connection "

// Testing
let factory = Factory()
let connection = "Oracle" |> factory.CreateConnection
connection.Description()
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type Connection =
  new : unit -> Connection
  abstract member Description : unit -> unit

Full name: Script.Connection

--------------------
new : unit -> Connection
abstract member Connection.Description : unit -> unit

Full name: Script.Connection.Description
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type OracleConnection =
  inherit Connection
  new : unit -> OracleConnection
  override Description : unit -> unit

Full name: Script.OracleConnection

--------------------
new : unit -> OracleConnection
val O : OracleConnection
override OracleConnection.Description : unit -> unit

Full name: Script.OracleConnection.Description
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
type SQLServerConnection =
  inherit Connection
  new : unit -> SQLServerConnection
  override Description : unit -> unit

Full name: Script.SQLServerConnection

--------------------
new : unit -> SQLServerConnection
val O : SQLServerConnection
override SQLServerConnection.Description : unit -> unit

Full name: Script.SQLServerConnection.Description
Multiple items
type MySQLConnection =
  inherit Connection
  new : unit -> MySQLConnection
  override Description : unit -> unit

Full name: Script.MySQLConnection

--------------------
new : unit -> MySQLConnection
val O : MySQLConnection
override MySQLConnection.Description : unit -> unit

Full name: Script.MySQLConnection.Description
Multiple items
type Factory =
  new : unit -> Factory
  member CreateConnection : connName:string -> Connection

Full name: Script.Factory

--------------------
new : unit -> Factory
val O : Factory
member Factory.CreateConnection : connName:string -> Connection

Full name: Script.Factory.CreateConnection
val connName : string
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val factory : Factory

Full name: Script.factory
val connection : Connection

Full name: Script.connection
member Factory.CreateConnection : connName:string -> Connection
abstract member Connection.Description : unit -> unit
Raw view Test code New version

More information

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