1 people like it.

Command Pattern - Simple

A simple implementation of a very basic command pattern that is being submitted to an Android app called GoF Design Patterns by Poash - highly recommended btw. This is imperative/oop in style, hope it helps someone. It is my first submission and I'm an F Sharp newbie!

 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: 
open System.IO
    type ICommandListener =
        abstract member Execute : unit -> unit
              
    type Application() =
        member x.OpenFile() = 
             printfn "[Application] OpenFile"
        //Implementation
    //Other Methods

    type OpenFileCommand(app: Application) =  //implied constructor
        member x._app = app
        interface ICommandListener with
            member x.Execute() = 
             printfn "[OpenFileCommand] Execute"
             x._app.OpenFile()

    type Widget(listener : ICommandListener) =
        member x._listener = listener
        member x.SendClick() = 
             printfn "[Widget] SendClick"
             x._listener.Execute()
    
let main() = 
    let app = new Application()
    let cmd = new OpenFileCommand(app)    
    let w = new Widget(cmd)
  
    //Simulate a button click
    w.SendClick()
    printfn "done"

main()
namespace System
namespace System.IO
type ICommandListener =
  interface
    abstract member Execute : unit -> unit
  end

Full name: Script.ICommandListener
abstract member ICommandListener.Execute : unit -> unit

Full name: Script.ICommandListener.Execute
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type Application =
  new : unit -> Application
  member OpenFile : unit -> unit

Full name: Script.Application

--------------------
new : unit -> Application
val x : Application
member Application.OpenFile : unit -> unit

Full name: Script.Application.OpenFile
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
type OpenFileCommand =
  interface ICommandListener
  new : app:Application -> OpenFileCommand
  member _app : Application

Full name: Script.OpenFileCommand

--------------------
new : app:Application -> OpenFileCommand
val app : Application
val x : OpenFileCommand
member OpenFileCommand._app : Application

Full name: Script.OpenFileCommand._app
override OpenFileCommand.Execute : unit -> unit

Full name: Script.OpenFileCommand.Execute
property OpenFileCommand._app: Application
member Application.OpenFile : unit -> unit
Multiple items
type Widget =
  new : listener:ICommandListener -> Widget
  member SendClick : unit -> unit
  member _listener : ICommandListener

Full name: Script.Widget

--------------------
new : listener:ICommandListener -> Widget
val listener : ICommandListener
val x : Widget
member Widget._listener : ICommandListener

Full name: Script.Widget._listener
member Widget.SendClick : unit -> unit

Full name: Script.Widget.SendClick
property Widget._listener: ICommandListener
abstract member ICommandListener.Execute : unit -> unit
val main : unit -> unit

Full name: Script.main
val cmd : OpenFileCommand
val w : Widget
member Widget.SendClick : unit -> unit
Raw view Test code New version

More information

Link:http://fssnip.net/hJ
Posted:11 years ago
Author:Richard Griffiths - SoulFireMage
Tags: design pattern , command pattern , imperative , oop