Changing the class of an instance

Ned Konz ned at bike-nomad.com
Wed Nov 12 15:08:09 UTC 2003


On Wednesday 12 November 2003 5:01 am, Giovanni Corriga wrote:
> Hi
>
> Is there a way to change at runtime the class of an instance?

You could do:

	oldObject become: (oldObject as: NewClass)

or similar. But this is not the cleanest way to go about doing things.

> In my application, XPSwiki (http://www.agilexp.org/XPSwiki) I need to be
> able to delete a page. Since a great part of the organizational
> functionalities of the tool depends on the class of the single pages
> (Task, UserStory etc), when deleting a page I need to change its class
> from i.e. UserStory to DeletedPage.

Though really what you have here (I think) is stateful behavior.

There are several ways of doing this:

* represent the behavior that changes in different states in a separate object 
and delegate to that object

* select at runtime based on state which method you'll call. There are several 
ways of doing this; the State pattern in the GOF book is uglier than it needs 
to be in Smalltalk. I've done a couple of state machine implementations in 
Squeak. One is a table-driven one that comes with Connectors v1, the other is 
a considerably faster hierarchical one that is part of Connectors v2. You can 
get my FSM code at

http://bike-nomad.com/squeak/monticello/Connectors/FSM-nk.27.mcz

I'd probably separate the role of the page from the parts that don't change 
between UserStory and DeletedPage.

So Page might have:
	name
	last edited
	edit history
	contents (which could be zero or more UserStory, Task, etc. objects)


-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE




More information about the Squeak-dev mailing list