37 people like it.

Proxy Pattern

Proxy pattern is a class functioning as an interface to something else.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
type CoreComputation() = 
    member this.Add(x) = x + 1
    member this.Sub(x) = x - 1
    member this.GetProxy(name) = 
        match name with
        | "Add" -> (this.Add, "add")
        | "Sub" -> (this.Sub, "sub")
        | _ -> failwith "not supported"

let proxy() = 
    let core = CoreComputation()
    let proxy = core.GetProxy("Add")
    printfn "result = %d" ((fst proxy) 1)
Multiple items
type CoreComputation =
  new : unit -> CoreComputation
  member Add : x:int -> int
  member GetProxy : name:string -> (int -> int) * string
  member Sub : x:int -> int

Full name: Script.CoreComputation

--------------------
new : unit -> CoreComputation
val this : CoreComputation
member CoreComputation.Add : x:int -> int

Full name: Script.CoreComputation.Add
val x : int
member CoreComputation.Sub : x:int -> int

Full name: Script.CoreComputation.Sub
member CoreComputation.GetProxy : name:string -> (int -> int) * string

Full name: Script.CoreComputation.GetProxy
val name : string
member CoreComputation.Add : x:int -> int
member CoreComputation.Sub : x:int -> int
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val proxy : unit -> unit

Full name: Script.proxy
val core : CoreComputation
val proxy : (int -> int) * string
member CoreComputation.GetProxy : name:string -> (int -> int) * string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
Raw view Test code New version

More information

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