Version française
Home     About     Download     Resources     Contact us    
Browse thread
state pattern...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Wohlwend <micha-1@f...>
Subject: Re: [Caml-list] state pattern...
On Monday 27 June 2005 00:13, james woodyatt wrote:
> I tend to work in the functional style first, then transfer what I've
> done into imperative style if there is a performance gain to be had by
> it.  Here's what I would do to make something like what you want:
> ...

I have modified the code, so that some method(s) of the state classes get the 
context class as parameter; I think it took me 3 hour to get the type 
definitions working :-)

Can this be done simpler? It works, although I don't completly understand all 
of it :-) 
-------------------------
class type param =
 object 
 method name : string
 end

class type state =
 object('self)
  method show: 'a. (#param as 'a) -> unit
  method next: 'self
 end

class ['state] context s =
 object (this:'self)
 val name = "context"
 val state_: #state = s 
 method name = name
 method show: unit = state_#show this
 method run = {< state_ = state_#next >}
 end

class state1 = object (this)
 method show: 'a. (#param as 'a) -> unit = fun c -> print_endline ("state1 of 
" ^ c#name)
 method next = new state2 
end
and state2 = object (this)
 method show: 'a. (#param  as 'a) -> unit = fun c -> print_endline ("state2 of 
" ^ c#name) 
 method next = new state1 
end;;

let c = new context (new state1) in
c#show;
let c = c#run in
c#show

----------------------------


thanks,
 Michael