From: Robert Klemme Date: 2003-05-20T02:07:41+09:00 Subject: Re: State Pattern Implementation "Mauricio Fern�ndez" schrieb im Newsbeitrag news:20030519162147.GA9282@student.ei.uni-stuttgart.de... > On Tue, May 20, 2003 at 01:04:34AM +0900, Robert Klemme wrote: > > > You mean this? > > > > > > class Client > > > def initialize > > > @state = InitialState.new > > > end > > > > > > def method_missing(meth, *args, &block) > > > @state = @state.send(meth, *args, &block) > > > nil # dummy > > > end > > > end > > > > Yes. > > > > > that implies that the methods cannot return useful values, they have to > > > give the next method. > > > > Is it reasonable to have a state transition method return anything other > > than the new state? You could put state dependent instance vars into the > > state instance. Hmmm... Not satisfying, too. > > As I see it, you simply use an object which goes through internal > transformations (state) depending on the operations you perform and > whatever else. Why should the client get the internal state? It should > be hidden inside the object. > > For instance (kinda like the example in the GOF, but cannot really > remember) > > socket = TCPConnection.new("www.somehost.com", 80) > socket.state # => "offline" > data = socket.receive(100) # on demand connection > socket.state # => "connected" > .. Hm. Maybe I got the state pattern wrong. With your example: if class Socket had status messages to print for some reason (i.e. they are NOT part of the state class implementations, maybe because you can set them from the outside for language customization or whatever). So we have an offlineMessage and an connectedMessage. Now, if behavior of Socket should change via a changed state instance I'd implement class Socket def statusMessage(); @state.statusMessage; end end Now, @state.statusMessage needs to access the Socket it's attached to. We can achieve this by - recording a ref to socket in state - augmenting every method call on state with the socket reference - put all socket state info that the state needs (i.e. the two messages) in all state classes (clearly not intended by this pattern) - ? I find all these a bit awkward... > My case is worse because I bought it but then left it 1800km away :-P Darn! robert