State Pattern

the interest rate is decided by the internal state: account balance

Copy Source
Copy Link
Tools:
 1: type AccountState = 
 2:     | Overdrawn
 3:     | Silver
 4:     | Gold
 5: [<Measure>] type USD
 6: type Account<[<Measure>] 'u>() =
 7:     let mutable balance = 0.0<_>   
 8:     member this.State
 9:         with get() = 
10:             match balance with
11:             | _ when balance <= 0.0<_> -> Overdrawn
12:             | _ when balance > 0.0<_> && balance < 10000.0<_> -> Silver
13:             | _ -> Gold
14:     member this.PayInterest() = 
15:         let interest = 
16:             match this.State with
17:                 | Overdrawn -> 0.
18:                 | Silver -> 0.01
19:                 | Gold -> 0.02
20:         interest * balance
21:     member this.Deposit(x:float<_>) =  
22:         let (a:float<_>) = x
23:         balance <- balance + a
24:     member this.Withdraw(x:float<_>) = balance <- balance - x
25: 
26: let state() = 
27:     let account = Account()
28: 
29:     account.Deposit(10000.<USD>)
30:     printfn "interest = %A" (account.PayInterest())
31: 
32:     account.Withdraw(20000.<USD>)
33:     printfn "interest = %A" (account.PayInterest())
34: 
union case AccountState.Overdrawn: AccountState
union case AccountState.Silver: AccountState
union case AccountState.Gold: AccountState
Multiple items
module Measure

from Microsoft.FSharp.Math

--------------------

type MeasureAttribute =
  class
    inherit System.Attribute
    new : unit -> MeasureAttribute
  end

Full name: Microsoft.FSharp.Core.MeasureAttribute

  type: MeasureAttribute
  implements: System.Runtime.InteropServices._Attribute
  inherits: System.Attribute
[<Measure>]
type USD

Full name: Snippet.USD
type Account<'u> =
  class
    new : unit -> Account<'u>
    member Deposit : x:float<USD> -> unit
    member PayInterest : unit -> float<USD>
    member Withdraw : x:float<USD> -> unit
    member State : AccountState
  end

Full name: Snippet.Account<_>
val mutable balance : float<USD>

  type: float<USD>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<USD>>
  implements: System.IEquatable<float<USD>>
  inherits: System.ValueType
val this : Account<'u>
property Account.State: AccountState
member Account.PayInterest : unit -> float<USD>

Full name: Snippet.Account.PayInterest
val interest : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
member Account.Deposit : x:float<USD> -> unit

Full name: Snippet.Account.Deposit
val x : float<USD>

  type: float<USD>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<USD>>
  implements: System.IEquatable<float<USD>>
  inherits: System.ValueType
Multiple items
val float : 'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------

type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>

  type: float<'Measure>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<'Measure>>
  implements: System.IEquatable<float<'Measure>>
  inherits: System.ValueType


--------------------

type float = System.Double

Full name: Microsoft.FSharp.Core.float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val a : float<USD>

  type: float<USD>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<USD>>
  implements: System.IEquatable<float<USD>>
  inherits: System.ValueType
member Account.Withdraw : x:float<USD> -> unit

Full name: Snippet.Account.Withdraw
val state : unit -> unit

Full name: Snippet.state
val account : Account<1>
member Account.Deposit : x:float<USD> -> unit
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
member Account.PayInterest : unit -> float<USD>
member Account.Withdraw : x:float<USD> -> unit

More information

Link: http://fssnip.net/7l
Posted: 3 years ago
Author: Tao Liu (website)
Tags: Design Patterns