05.01.2012

OOP in LotusScript: Overloading Constructors the LotusScript Way

>>Author:  Thomas Bahn
>>Ort:     Antalya
        
URL: http://www.assono.de/blog/d6plinks/OOP-in-LotusScript-Overloading-Constructors-the-LotusScript-Way

Category: OOP, LotusScript, Entwicklung

A picture named M2
For many years now we have been - and still are - preaching and doing object oriented programming with LotusScript. But compared to a more modern kind of programming language LotusScript has some downsides. For example LotusScript doesn't know about multiple inheritance or at least interfaces. Another aspect I want to discuss is, that you cannot overload methods, that is you cannot create several methods (Subs or functions) with the same name only to be differed in the number and type of parameters (parameter signature). For normal methods you can work around this limitation just by using different names, but the constructor always has to be named Public Sub New.

To pass a "list" of parameters is one way to gain more power in creating the constructor.

But especially, when all is just about one parameter this way results in too much overhead. To use a Variant and an "object separator" is leaner and more elegant.
Lets use the constructor of the class OpenGeoDBRecord as an example. Objects of this class should be initialized either with a list, an array, a NotesDocument, a NotesViewEntry or just devoid with default values:

Private Class OpenGeoDBRecord

        Public Sub New(source As Variant)
               
                Dim openGeoDoc As NotesDocument
                Dim openGeoEntry As NotesViewEntry
               
                If TypeName(source) = "VARIANT LIST" Then
                        Call ReadFromList(source)
                                                                       
                ElseIf TypeName(source) = "STRING( )" Then
                        Call ReadFromArray(source)
                       
                ElseIf source Is Nothing Then
                        ' initialize with default values...

                ElseIf source IsA "NotesDocument" Then
                        Set openGeoDoc = source
                        Call ReadFromDoc(openGeoDoc)
                       
                ElseIf source IsA "NotesViewEntry" Then
                        Set openGeoEntry = source
                        Call ReadFromViewEntry(openGeoEntry)
                End If
        End Sub ' OpenGeoDBRecord.New


        Public Sub ReadFromList(paramList As Variant)
                '/**
                ' * reads data from a list with the parameter names as keys.
                ' *
                ' * @param        paramList Variant list of parameters with parameter names as keys.
                ' */
        End Sub ' OpenGeoDBRecord.ReadFromList
       

        Public Sub ReadFromArray(paramArray As Variant)
                '/**
                ' * reads data from an array
                ' *
                ' * @param        paramArray String array of parameters.
                ' */
        End Sub ' OpenGeoDBRecord.ReadFromArray


        Public Sub ReadFromDoc(openGeoDoc As NotesDocument)
                '/**
                ' * reads data from a document
                ' *
                ' * @param        openGeoDoc NotesDocument object to read from.
                ' */
        End Sub ' OpenGeoDBRecord.ReadFromDoc

       
        Public Sub ReadFromViewEntry(openGeoEntry As NotesViewEntry)
                '/**
                ' * reads data from a view entry (more efficient than from a NotesDocument)
                ' *
                ' * @param        openGeoDoc NotesViewEntry object to read from.
                ' */
        End Sub ' OpenGeoDBRecord.ReadFromViewEntry

End Class ' OpenGeoDBRecord


Please mind the typecast using the typed variables.

Instead of the IsA operator you can of course also use TypeName(source) = "NOTESDOCUMENT" and accordingly = "NOTESVIEWENTRY".

Comments

#1 toller tip! Danke, Thomas. Gravatar Image

Post A Comment

Comments

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Tags

Deutsche RSS-Feeds (German)

Custom Button Custom Button

English RSS feeds

Custom Button Custom Button