2 people like it.

F# Decorator Pattern

Decorator Pattern

 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: 
// Decorator Pattern
[<AbstractClassAttribute>]
type ComputerParts() =
    abstract member Description :unit -> unit

type Computer() =
    inherit ComputerParts()
    override O.Description() = printf "I'm a Computer with"

type CDROM( c :ComputerParts ) =
    inherit ComputerParts()
    override O.Description() = c.Description(); printf ", CDROM"

type Mouse( c :ComputerParts ) =
    inherit ComputerParts()
    override O.Description() = c.Description(); printf ", Mouse"

type Keyboard( c :ComputerParts ) =
    inherit ComputerParts()
    override O.Description() = c.Description(); printf ", Keyboard"

let mutable computer = Computer() :> ComputerParts
computer <- Mouse( computer )
computer <- CDROM( computer )
computer <- Keyboard( computer )

computer.Description()
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type ComputerParts =
  new : unit -> ComputerParts
  abstract member Description : unit -> unit

Full name: Script.ComputerParts

--------------------
new : unit -> ComputerParts
abstract member ComputerParts.Description : unit -> unit

Full name: Script.ComputerParts.Description
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type Computer =
  inherit ComputerParts
  new : unit -> Computer
  override Description : unit -> unit

Full name: Script.Computer

--------------------
new : unit -> Computer
val O : Computer
override Computer.Description : unit -> unit

Full name: Script.Computer.Description
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Multiple items
type CDROM =
  inherit ComputerParts
  new : c:ComputerParts -> CDROM
  override Description : unit -> unit

Full name: Script.CDROM

--------------------
new : c:ComputerParts -> CDROM
val c : ComputerParts
val O : CDROM
override CDROM.Description : unit -> unit

Full name: Script.CDROM.Description
abstract member ComputerParts.Description : unit -> unit
Multiple items
type Mouse =
  inherit ComputerParts
  new : c:ComputerParts -> Mouse
  override Description : unit -> unit

Full name: Script.Mouse

--------------------
new : c:ComputerParts -> Mouse
val O : Mouse
override Mouse.Description : unit -> unit

Full name: Script.Mouse.Description
Multiple items
type Keyboard =
  inherit ComputerParts
  new : c:ComputerParts -> Keyboard
  override Description : unit -> unit

Full name: Script.Keyboard

--------------------
new : c:ComputerParts -> Keyboard
val O : Keyboard
override Keyboard.Description : unit -> unit

Full name: Script.Keyboard.Description
val mutable computer : ComputerParts

Full name: Script.computer
Raw view Test code New version

More information

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