责任链模式(在F#中的实现:)
以下示例代码:确保人的年龄在18到65之间,体重不超过200,身高大于120
type Record = { Name : string; Age : int; Weight: float; Height: float;} let ChainOfResponsibility() = let validAge (record:Record) = record.Age < 65 && record.Age > 18 let validWeight (record:Record) = record.Weight < 200. let validHeight (record:Record) = record.Height > 120. let check (f:Record->bool) (record:Record, result:bool) = if result=false then (record, false) else (record, f(record)) let chainOfResponsibility = check(validAge) >> check(validWeight) >> check(validHeight) let john = { Name = "John"; Age = 80; Weight = 180.; Height=180. } let dan = { Name = "Dan"; Age = 20; Weight = 160.; Height=190. } printfn "john result = %b" ((chainOfResponsibility (john, true)) |> snd) printfn "dan result = %b" ((chainOfResponsibility (dan, true)) |> snd)
修饰模式
以下在F#中实现了修饰模式,修饰模式对一个对象在运行时添加新的功能
type Divide() = let mutable divide = fun (a,b) -> a / b member this.Function with get() = divide and set(v) = divide <- v member this.Invoke(a,b) = divide (a,b) let decorate() = let d = Divide() let checkZero (a,b) = if b = 0 then failwith "a/b and b is 0" else (a,b) try d.Invoke(1, 0) |> ignore with e -> printfn "without check, the error is = %s" e.Message d.Function <- checkZero >> d.Function try d.Invoke(1,0) |> ignore with e -> printfn "after add check, error is = %s" e.Message
F#中的观察者模式
type Subject() = let mutable notify = fun _ -> () member this.Subscribe (notifyFunction) = let wrap f i = f(i); i notify <- (wrap notifyFunction) >> notify member this.Reset() = notify <- fun _ -> () member this.SomethingHappen(k) = notify k type ObserverA() = member this.NotifyMe(i) = printfn "notified A %A" i type ObserverB() = member this.NotifyMeB(i) = printfn "notified B %A" i let observer() = let a = ObserverA() let b = ObserverB() let subject = Subject() subject.Subscribe(a.NotifyMe) subject.Subscribe(b.NotifyMeB) subject.SomethingHappen("good")
F#中的策略模式
let quicksort l = printfn "quick sort" let shellsort l = printfn "shell short" let bubblesort l = printfn "bubble sort" type Strategy(sortFunction) = member this.SortFunction with get() = sortFunction member this.Execute(list) = sortFunction list let strategy() = let s = Strategy(quicksort) s.Execute([1..6]) strategy()
F#中的状态模式
利率被内部状态(账户余额)所决定
type AccountState = | Overdrawn | Silver | Gold [<Measure>] type USD type Account<[<Measure>] 'u>() = let mutable balance = 0.0<_> member this.State with get() = match balance with | _ when balance <= 0.0<_> -> Overdrawn | _ when balance > 0.0<_> && balance < 10000.0<_> -> Silver | _ -> Gold member this.PayInterest() = let interest = match this.State with | Overdrawn -> 0. | Silver -> 0.01 | Gold -> 0.02 interest * balance member this.Deposit(x:float<_>) = let (a:float<_>) = x balance <- balance + a member this.Withdraw(x:float<_>) = balance <- balance - x let state() = let account = Account() account.Deposit(10000.<USD>) printfn "interest = %A" (account.PayInterest()) account.Withdraw(20000.<USD>) printfn "interest = %A" (account.PayInterest())