caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: james woodyatt <jhw@wetware.com>
To: Michael Wohlwend <micha-1@fantasymail.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] state pattern...
Date: Sun, 26 Jun 2005 15:13:36 -0700	[thread overview]
Message-ID: <3fce1487ee8fd9f28edbe70276980346@wetware.com> (raw)
In-Reply-To: <200506262157.02201.micha-1@fantasymail.de>

On 26 Jun 2005, at 12:57, Michael Wohlwend wrote:
>
> although it can be implemented with a 'match', just of interest, can 
> somebody
> help me with my oo implementation of the state-pattern?

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:

class type state =
     object('self)
         method show: unit
         method next: 'self
     end

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

class state1 =
     object
         method show = print_endline "state1"
         method next = new state2
     end
and state2 =
     object
         method show = print_endline "state2"
         method next = new state1
     end

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

You get into a problem pretty quickly if the state class needs to be 
aware of the full type of the context in order to compute the next 
state.  What that really means is that the context *is* the state, and 
you need to rethink how the pattern is supposed to be applied to your 
design.


-- 
j h woodyatt <jhw@wetware.com>
that's my village calling... no doubt, they want their idiot back.


  reply	other threads:[~2005-06-26 22:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-26 19:57 Michael Wohlwend
2005-06-26 22:13 ` james woodyatt [this message]
2005-06-26 22:45   ` [Caml-list] " 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=3fce1487ee8fd9f28edbe70276980346@wetware.com \
    --to=jhw@wetware.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=micha-1@fantasymail.de \
    /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).