From: "Mauricio Fernández" Date: 2003-05-13T21:47:25+09:00 Subject: Re: State Pattern Implementation On Tue, May 13, 2003 at 08:11:38PM +0900, Robert Klemme wrote: > > > I'd like to hear what people think: is this a reasonable > implementation of > > > the state pattern? > > > > You are representing each state by a number... with the State pattern > > isn't each state supposed to be represented by a class ? > > Normally, yes. The association with a number is just for the ease of > keying the example in. My main idea was to replace the state instance by > a redefinition of instance methods. Pro is, you safe an instance and a > redirection; on the con side is increased code size for the class since > you have to include code for all states. > > I'm curios to know whether anybody did this before. I might be missing > some grave disadvantage. Probably stupid, but all I can think of right now: * easier to screw up things as the state is defined implicitly by the singleton methods, so you could probably reach "non-valid states" (if you forget to redefine one method) * cannot serialize these things, i.e. cannot capture the state of the object (related to the latter) Finally: * no clean separation between the place where you define state transitions and the state methods/attributes (which can be a good or bad thing depending on how you see it) OTOH, it is clever (extra points!!!! ;-), faster than delegation, and uses singleton methods (we love them, don't we? :) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Q: What's the big deal about rm, I have been deleting stuff for years? And never lost anything.. oops! A: ... -- From the Frequently Unasked Questions