Implementing abstract classes, sealed classes, and interfaces in LotusScript
This post was prompted after following all the fun Tim Tripcomy was having building a mini-framework to demonstrate remote event binding....
Rather than just getting frustrated with the lack of progress by IBM
in developing the LotusScript language - in line with Microsoft's version
of BASIC (VB.Net) - I started building the .Domino Framework. This was
my way of getting more out of LotusScript so that I could be even more
productive. And it works. I find that by using a framework I can build new Notes applications around 50% faster than before. And we ALL know that Notes development is already signficantly faster than most (All?) other development platforms - right!!? LotusScript currently falls short in providing a number of important OOP concepts, including abstract classes, sealed classes, and interfaces. Within the .Domino Framework I have chosen to implement these constructs as described below. Note: One of the key difference with LotusScript is that errors for violating "the rules" are thrown at run-time and not at compile time. Other OOP languages such as C#, VB.Net, and Java implement these OOP constructs with class modifiers such as "abstract", "sealed", and "interface" (can you guess which is which?). These languages stop you making mistakes when you first try to write the errant code.
Abstract Classes An abstract class is defined as a way of providing attributes (properties, methods, and events) that can be shared by subclasses. e.g. An Employee class defines attributes common to FullTimeEmployee, PartTimeEmployee, and Contractor classes. The employee class is deemed "abstract" if you cannot implement an employee class directly but must implement one of the available subclasses. An abstract class can be simulated in LotusScript by using the constructor. When an instance of a class is defined the constructor of ALL base classes are also invoked. We can therefore do a check to ensure the class being implemented is not the abstract class itself:- Class AbstractClass Sub New(Source As Variant) If Typename(Me) = "ABSTRACTCLASS" Then Error ERROR_USER_FATAL,"AbstractClass is abstract and cannot be implemented directly" Exit Sub End If End Sub End Class
Sealed Classes Sealed classes are used when you wish to prevent a class being extended by another class. Again the constructor can be used to validate sealed classes. Class SealedClass As AbstractClass Sub New(Source As Variant) If Typename(Me) <> "SEALEDCLASS" Then Error ERROR_USER_FATAL,"SealedClass is sealed and cannot be extended by " + Typename(Me) Exit Sub End If End Sub End Class
Interfaces Like abstract classes, Interfaces are not designed to be implemented directly. They differ from abstract classes in that they provide no code for properties, methods, events but define the properties, classes, and events that must be present for a class to meet the requirements of the interface. e.g. in Tim's example he has certain methods that must be implemented to make a specifc class "bindable remotely". This could be achieved by implementing an IsBindable Interface. Another common example would be an "IsSortable" interface that is used to define classes that can be sorted. Classes implementing such an interface may require GetFirst, GetNext, and Compare methods for a sort function to work. Interfaces are more challenging to implement in LotusScript. Because we are restricted to extending classes in a single chain we can only implement multiple interfaces by definining them in the underlying base class(es). In C# we could implement multiple interfaces by listing each required interface as part of the class definition. e.g. if I have interface A, B, and C I cannot easily implement classes in LotusScript with interface requirements A, B ,C, AB, AC, BC, and ABC without repeating a LOT of code (YUK). We also have no way of checking if all of the required properties, methods, events have been implemented correctly, rather we can merely thow an error when we attempt to invoke one of those attributes. The code for an interface could look something like this.... Class Interface Sub New(Source As Variant) If Typename(Me) = "INTERFACE" Then Error ERROR_USER_FATAL,"Interface is defined as an interface and cannot be implemented directly" Exit Sub End If End Sub Sub Method1 Error ERROR_USER_FATAL,Typename(Me) + " has not implemented method Method1" End Sub Sub Method2 Error ERROR_USER_FATAL,Typename(Me) + " has not implemented method Method2" End Sub End Class
The next release of the .Domino Framework will provide an example agent that demonstrates the above.
|
Ratings
0
|
Comments (0)