38 people like it.

Template pattern

Template pattern is a behavior-based pattern. Realized the importance of high order function, this is a way to use high order function. Still keep the type structure to organize my code.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
let connect conStr = 
    printfn "connect to database: %s" conStr
let query queryStr = 
    printfn "query database %s" queryStr
let disconnect ()  = 
    printfn "disconnect"
type Template(connF, queryF, disconnF) =     
    member this.Execute(conStr, queryStr) = 
        this.TemplateF conStr queryStr
    member this.TemplateF 
        with get() = 
            let f conStr queryStr = 
                connF conStr
                queryF queryStr
                disconnF ()
            f

let template() = 
    let s = Template(connect, query, disconnect)    
    s.Execute("<connection string>", "select * from tableA")

template()
val connect : conStr:string -> unit

Full name: Script.connect
val conStr : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val query : queryStr:string -> unit

Full name: Script.query
val queryStr : string
val disconnect : unit -> unit

Full name: Script.disconnect
Multiple items
type Template =
  new : connF:(string -> unit) * queryF:(string -> unit) * disconnF:(unit -> unit) -> Template
  member Execute : conStr:string * queryStr:string -> unit
  member TemplateF : (string -> string -> unit)

Full name: Script.Template

--------------------
new : connF:(string -> unit) * queryF:(string -> unit) * disconnF:(unit -> unit) -> Template
val connF : (string -> unit)
val queryF : (string -> unit)
val disconnF : (unit -> unit)
val this : Template
member Template.Execute : conStr:string * queryStr:string -> unit

Full name: Script.Template.Execute
property Template.TemplateF: string -> string -> unit
member Template.TemplateF : (string -> string -> unit)

Full name: Script.Template.TemplateF
val f : (string -> string -> unit)
val template : unit -> unit

Full name: Script.template
val s : Template
member Template.Execute : conStr:string * queryStr:string -> unit
Raw view Test code New version

More information

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