caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Michael Wohlwend <micha-1@fantasymail.de>
To: caml-list@yquem.inria.fr
Subject: state pattern...
Date: Sun, 26 Jun 2005 21:57:02 +0200	[thread overview]
Message-ID: <200506262157.02201.micha-1@fantasymail.de> (raw)

Hi,

although it can be implemented with a 'match', just of interest, can somebody 
help me with my oo implementation of the state-pattern?

this does not work (any many other tries also didn't :-)

class ['a] context (start_state : 'a)= object(this)
 constraint 'a = #state
 val mutable state = start_state
 method set_state st = state <- st
 method show = state#show
 method run = state#handle this 
end

and virtual state = object
  method virtual show : unit
  method virtual handle : state #context -> #state -> unit
end;;

class state1 = object (this)
 inherit state
 method show = print_endline "state1"
 method handle context = context#set_state (new state2)
end
and state2 = object (this)
 inherit state
 method show = print_endline "state2"
 method handle context = context#set_state (new state1)
end;;

for this to work:
let c = new context (new state1) in
c#show; (* state1 *)
c#run; (* change state *)
c#show; (* state2 *)
c#run;


cheers,
 Michael


             reply	other threads:[~2005-06-26 19:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-26 19:57 Michael Wohlwend [this message]
2005-06-26 22:13 ` [Caml-list] " james woodyatt
2005-06-26 22:45   ` Michael Wohlwend
2005-06-27 23:23   ` Michael Wohlwend
2005-06-28  0:54     ` Jacques Garrigue
2005-06-28 12:24       ` Michael Wohlwend
2005-06-28 13:20         ` Jacques Garrigue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200506262157.02201.micha-1@fantasymail.de \
    --to=micha-1@fantasymail.de \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).