Adapter pattern

Category:
Design patterns
Description: Adapter pattern make incompatible types work (walk) together without changing existing code. In the sample the dog and cat are imcompatible types. For more information, please go to http://en.wikipedia.org/wiki/Adapter_pattern
Code:
type Cat() = 
member this.Walk() = printfn "cat walk"
type Dog() =
member this.Walk() = printfn "dog walk"

let adapter() =
let cat = Cat()
let dog = Dog()
let inline walk (x : ^T) = (^T : (member Walk : unit->unit) (x))
walk(cat)
walk(dog)

Execution Result:
cat walk
dog walk

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

Comments

No comments yet.