2 people like it.

F# Strategy Pattern

Implementing the strategy pattern by putting all the strategies as a static member function for a class that can't be instantiated like the singleton

 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: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
// Put all the strategies as a static members for a class that can't be instantiated

type GoStrategies =
    static member Driving = printfn "I'm Driving"
    static member Flying = printfn "I'm Flying"
    static member Swimming = printfn "I'm Swimming"

[<AbstractClassAttribute>]
type Vehicle() =
    member val Brand = "" with get,set
    member val Model = "" with get,set
    abstract Type :string with get
    abstract member Go :unit -> unit
    member O.Print() = printfn "%s\t%s\t%s" O.Type O.Brand O.Model

type Car() =
    inherit Vehicle()
    override O.Type with get() = "Car"
    override O.Go() = GoStrategies.Driving

type Plane() =
    inherit Vehicle()
    override O.Type with get() = "Plane"
    override O.Go() = GoStrategies.Flying

type Boat() =
    inherit Vehicle()
    override O.Type with get() = "Boat"
    override O.Go() = GoStrategies.Swimming

// Testing
let car1 = Car( Brand="BMW", Model="X6" )
let car2 = Car( Brand="Dodge", Model="Viper" )
let plane1 = Plane( Brand="AirBus", Model="A321" )
car1.Print()
car1.Go()
car2.Print()
car2.Go()
plane1.Print()
plane1.Go()
static member GoStrategies.Driving : unit

Full name: Script.GoStrategies.Driving
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
static member GoStrategies.Flying : unit

Full name: Script.GoStrategies.Flying
static member GoStrategies.Swimming : unit

Full name: Script.GoStrategies.Swimming
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type Vehicle =
  new : unit -> Vehicle
  abstract member Go : unit -> unit
  abstract member Type : string
  member Print : unit -> unit
  member Brand : string
  member Model : string
  member Brand : string with set
  member Model : string with set

Full name: Script.Vehicle

--------------------
new : unit -> Vehicle
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
abstract member Vehicle.Type : string

Full name: Script.Vehicle.Type
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
abstract member Vehicle.Go : unit -> unit

Full name: Script.Vehicle.Go
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val O : Vehicle
member Vehicle.Print : unit -> unit

Full name: Script.Vehicle.Print
property Vehicle.Type: string
property Vehicle.Brand: string
property Vehicle.Model: string
Multiple items
type Car =
  inherit Vehicle
  new : unit -> Car
  override Go : unit -> unit
  override Type : string

Full name: Script.Car

--------------------
new : unit -> Car
val O : Car
override Car.Type : string

Full name: Script.Car.Type
Multiple items
abstract member Vehicle.Go : unit -> unit

--------------------
override Car.Go : unit -> unit

Full name: Script.Car.Go
type GoStrategies =
  static member Driving : unit
  static member Flying : unit
  static member Swimming : unit

Full name: Script.GoStrategies
property GoStrategies.Driving: unit
Multiple items
type Plane =
  inherit Vehicle
  new : unit -> Plane
  override Go : unit -> unit
  override Type : string

Full name: Script.Plane

--------------------
new : unit -> Plane
val O : Plane
override Plane.Type : string

Full name: Script.Plane.Type
Multiple items
abstract member Vehicle.Go : unit -> unit

--------------------
override Plane.Go : unit -> unit

Full name: Script.Plane.Go
property GoStrategies.Flying: unit
Multiple items
type Boat =
  inherit Vehicle
  new : unit -> Boat
  override Go : unit -> unit
  override Type : string

Full name: Script.Boat

--------------------
new : unit -> Boat
val O : Boat
override Boat.Type : string

Full name: Script.Boat.Type
Multiple items
abstract member Vehicle.Go : unit -> unit

--------------------
override Boat.Go : unit -> unit

Full name: Script.Boat.Go
property GoStrategies.Swimming: unit
val car1 : Car

Full name: Script.car1
val car2 : Car

Full name: Script.car2
val plane1 : Plane

Full name: Script.plane1
member Vehicle.Print : unit -> unit
override Car.Go : unit -> unit
override Plane.Go : unit -> unit
Raw view Test code New version

More information

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