Page 1 of 1

Implementing static methods

Posted: Wed Apr 14, 2010 12:05 pm
by allistar
Hi all,
Alas, Jade does not allow for static methods. There are often times though when a static method is the best way to implement a particular feature or help provide for a more extensible framework.

So how do we implement a workaround to static methods?

Options I see are:

1) use normal methods and create a transient instance of the class and call that method.
- won't work for abstract classes.
- has an inherent performance hit in creating the transient, especially when called many times. This could be mitigated by using a flyweight pattern, but then you'd have a additional dictionary lookup to get the flyweight object.

2) write a method on the Class class that does a large case-like if statement.
- not very nice.
- you cannot just compare classes with classes, you need to compare class names because of subschema copy classes have a different oid.
- need to work out the subclasses and/or superclasses of "self", which will require a transient dictionary. Not very efficient.

3) Create a user class that represents the Jade class and have "myParentClass", "allChildClasses", "name" properties and create an instance of that class for every class in your schema(s). Store these in a globally accessible collection keyed by class name. Put your "static" method on this and inside it have a large case-like if statement. The "default" of the case statement would be do call the same method on myParent.
- a pain to have to set up.
- will need to have the persistent parallel class structure maintained and kept in sync.
- at least it would be relatively efficient.

Are there any other (hopefully nicer) ways of implementing pseudo-static methods in Jade? The key is it needs to be efficient at runtime.