Updated 2006-09-18 12:25:06

Object Oriented facilities gives higher abstraction power. But iterative development is mostly bottom-up. One tries out some function, then finds some repetition and makes it a generic facility. The typical Xotcl tutorial starts with class. Mostly in protoyping one does not start with class.Here we will see how xotcl is employed in prototyping.ie, how we employ object-oriented mechanisms in a bottom-up fashion.

Say we have a need for counter. Now, there are action, like add,decr on a counter. The functions add,decr share a common state variable, count.

First approach. count as global variable, add and decr acting on that.

Second iteration. We decide not to pollute the global space, so we need an isolated context. We have two choices , either namespace or using object. We will use the second

using XOTcl
 Object counter

 counter set count 0

 counter proc add { val } {
  my set count [expr [my set count]  + $val]

 counter proc decr { val } {
  my set count [expr [my set count] - $val]

 counter add 3
 counter decr 1

Now, we encounter a need for another counter object in the program. We can define a class. But the system should not force you. One should accomplish instantiation by other means too. So that we can abstract/generalise whenever we want to. Instantiation by cloning.
 counter copy counter2
 counter2 set count 0

Here using copy, we can clone an object and thereby we can create multiple instance of an object. Here comes a major limitation, if we adopted namespace approach. namespace and object define an isolated context, but namespace does not allow cloning to give multiple instances.

Now we decide, we will have some kind of factory object(class object), that can create multiple instances of counter object on demand.

( Someone fill in how to create a class object from an already existing instance.) The approach should be to know the procs of an object using introspection and call instproc for the class using the same definition. If we could bundle this as one function, then invoking that function will create a Counter class object, so that for future instances we can use that class object instead of cloning a master copy.