-
Notifications
You must be signed in to change notification settings - Fork 18
/
06-DependencyInjection_Interface-2.fsx
191 lines (150 loc) · 5.67 KB
/
06-DependencyInjection_Interface-2.fsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(* ======================================
06-DependencyInjection_Interface.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Way #6: Dependency injection (using interfaces) - v2: records of functions
In this design, an API layer communicates with a Turtle Interface (OO style) or a record of TurtleFunctions (FP style)
rather than directly with a turtle.
The client injects a specific turtle implementation via the API's constructor.
====================================== *)
#load "Common.fsx"
#load "OOTurtleLib.fsx"
#load "FPTurtleLib.fsx"
open Common
// ============================================================================
// Dependency Injection (records of functions)
// ============================================================================
// ----------------------------
// Turtle Interface
// ----------------------------
// a quick alias for readability
type TurtleState = FPTurtleLib.Turtle.TurtleState
type TurtleFunctions = {
move : Distance -> TurtleState -> TurtleState
turn : Angle -> TurtleState -> TurtleState
penUp : TurtleState -> TurtleState
penDown : TurtleState -> TurtleState
setColor : PenColor -> TurtleState -> TurtleState
}
// Note that there are NO "units" in these functions, unlike the OO version.
// ----------------------------
// Turtle Api Layer
// ----------------------------
module TurtleApiLayer_FP =
open Result
open FPTurtleLib
/// Function to log a message
let log message =
printfn "%s" message
let initialTurtleState = Turtle.initialTurtleState
type ErrorMessage =
| InvalidDistance of string
| InvalidAngle of string
| InvalidColor of string
| InvalidCommand of string
// convert the distance parameter to a float, or throw an exception
let validateDistance distanceStr =
try
Ok (float distanceStr)
with
| ex ->
Error (InvalidDistance distanceStr)
// convert the angle parameter to a float, or throw an exception
let validateAngle angleStr =
try
Ok ((float angleStr) * 1.0<Degrees>)
with
| ex ->
Error (InvalidAngle angleStr)
// convert the color parameter to a PenColor, or throw an exception
let validateColor colorStr =
match colorStr with
| "Black" -> Ok Black
| "Blue" -> Ok Blue
| "Red" -> Ok Red
| _ ->
Error (InvalidColor colorStr)
type TurtleApi(turtleFunctions: TurtleFunctions) =
let mutable state = initialTurtleState
/// Update the mutable state value
let updateState newState =
state <- newState
/// Execute the command string, and return a Result
/// Exec : commandStr:string -> Result<unit,ErrorMessage>
member this.Exec (commandStr:string) =
let tokens = commandStr.Split(' ') |> List.ofArray |> List.map trimString
// return Ok of unit, or Error
match tokens with
| [ "Move"; distanceStr ] -> result {
let! distance = validateDistance distanceStr
let newState = turtleFunctions.move distance state
updateState newState
}
| [ "Turn"; angleStr ] -> result {
let! angle = validateAngle angleStr
let newState = turtleFunctions.turn angle state
updateState newState
}
| [ "Pen"; "Up" ] -> result {
let newState = turtleFunctions.penUp state
updateState newState
}
| [ "Pen"; "Down" ] -> result {
let newState = turtleFunctions.penDown state
updateState newState
}
| [ "SetColor"; colorStr ] -> result {
let! color = validateColor colorStr
let newState = turtleFunctions.setColor color state
updateState newState
}
| _ ->
Error (InvalidCommand commandStr)
// ----------------------------
// Multiple Turtle Implementations
// ----------------------------
module TurtleImplementation_FP =
open FPTurtleLib
let normalSize() =
let log = printfn "%s"
// return a record of functions
{
move = Turtle.move log
turn = Turtle.turn log
penUp = Turtle.penUp log
penDown = Turtle.penDown log
setColor = Turtle.setColor log
}
let halfSize() =
let normalSize = normalSize()
// return a reduced turtle
{ normalSize with
move = fun dist -> normalSize.move (dist/2.0)
}
// ----------------------------
// Turtle Api Client
// ----------------------------
module TurtleApiClient_FP =
open TurtleApiLayer_FP
open Result
let drawTriangle(api:TurtleApi) =
result {
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
} |> ignore
// ----------------------------
// Turtle Api Tests (FP style)
// ----------------------------
do
let turtleFns = TurtleImplementation_FP.normalSize() // a TurtleFunctions type
let api = TurtleApiLayer_FP.TurtleApi(turtleFns)
TurtleApiClient_FP.drawTriangle(api)
do
let turtleFns = TurtleImplementation_FP.halfSize()
let api = TurtleApiLayer_FP.TurtleApi(turtleFns)
TurtleApiClient_FP.drawTriangle(api)