Composite pattern with variable in a class-like structure

Category:
Design patterns
Description: Composite pattern with variable in a class-like structure. The computation result will be brought back by the class-like structure. For more information, please go to http://en.wikipedia.org/wiki/Composite_pattern
Code:
type IA2<'T> =
abstract member Do : 'T -> unit

let OtherCompositePatternSample4() =
let tree = Tree(1, Tree(11, Node(12), Node(13)), Node(2))
let wrapper =
let result = ref 0
({ new IA2 with
member this.Do(n) =
result := !result + n
}, result)
tree.PreOrder (fst wrapper).Do
printfn "result = %d" !(snd wrapper)

Execution Result:
result = 39

Last edited Sep 14, 2011 at 3:22 AM by ttliu2000, version 1

Comments

No comments yet.