From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id B883EBCAE for ; Mon, 27 Jun 2005 00:13:56 +0200 (CEST) Received: from wetware.wetware.com (wetware.wetware.com [209.218.58.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j5QMDthY003631 for ; Mon, 27 Jun 2005 00:13:56 +0200 Received: from [69.12.155.90] (helo=[10.0.1.6]) by wetware.wetware.com with esmtp (Exim 4.43) id 1DmfNf-0003Lx-Ai; Sun, 26 Jun 2005 15:13:35 -0700 In-Reply-To: <200506262157.02201.micha-1@fantasymail.de> References: <200506262157.02201.micha-1@fantasymail.de> Mime-Version: 1.0 (Apple Message framework v622) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <3fce1487ee8fd9f28edbe70276980346@wetware.com> Content-Transfer-Encoding: 7bit Cc: caml-list@yquem.inria.fr From: james woodyatt Subject: Re: [Caml-list] state pattern... Date: Sun, 26 Jun 2005 15:13:36 -0700 To: Michael Wohlwend X-Mailer: Apple Mail (2.622) X-Miltered: at nez-perce with ID 42BF28A3.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; woodyatt:01 jhw:01 wetware:01 caml-list:01 'self:01 'self:01 val:01 endline:01 endline:01 woodyatt:01 jhw:01 wetware:01 ...:98 2005,:98 ...:98 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: 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 that's my village calling... no doubt, they want their idiot back.