All entries tagged with classes
After writing my blog about implementing abstract (and other) classes in LotusScript I started to think about ways to reduce the code that would be required to implement this into each and every abstract/sealed class in a framework. I have now gone back to my own .Domino Framework and added a new base class that implements much of the logic for abstract/sealed classes (and interfaces).
Class DominoClass Sub New(Source As Variant) Dim Params As Variant ' Parameters passed to this class Select Case Typename(Source) Case "STRING" Params = Fulltrim(Split(Source)) If (Ubound(Params) = 1) Then Call ValidateClass(Params(0),Params(1)) End Select End Sub '/** ' * Validate a class to ensure it conforms to rules for special class types ' */ Sub ValidateClass(ClassType As String,ClassName As String) Select Case ClassType$ Case ENUM_CLASS__ABSTRACT: ' Abstract classes cannot be implemented directly If (Typename(Me) = ClassName$ )Then Error ERROR_USER_FATAL,ClassName$ + " is abstract and cannot be implemented directly" End ERROR_USER_FATAL End If Case ENUM_CLASS__INTERFACE: ' Interfaces cannot be implemented directly If (Typename(Me) = ClassName$) Then Error ERROR_USER_FATAL,ClassName$ + " is an interface and cannot be implemented directly" End ERROR_USER_FATAL End If Case ENUM_CLASS__SEALED: ' Sealed classes cannot be extended If (Typename(Me) <> ClassName$) Then Error ERROR_USER_FATAL,ClassName$ + " is sealed and cannot be extended by " + Typename(Me) End ERROR_USER_FATAL End If End Select End Sub End Class
Now any class that extends this class (directly or indirectly) can assert itself as being an abstract class, sealed class, or interface on one of two ways:
Constructor: The following scenario can be used when the constructor shares the same footrpint as the base class (single parameter of type variant) and there is no need to pass the original parameter to any intermediate base/super class. It work by directing the constructor of the base class to take an alternative value than the one passed to the extended class.
Class AbstractClass As DominoClass Sub New(Source As Variant), DominoClass(ENUM_CLASS__ABSTRACT + " ABSTRACTCLASS" ) ... End Sub End Class
Method: The alternative is to invoke the ValidateClass method directly
Class SealedClass As AbstractClass Sub New(Source As Variant) Call ValidateClass(ENUM_CLASS__SEALED,"SEALEDCLASS" ) End Sub End Class
|
Ratings
0
|
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
|