
OTHER
Tony Higham's Object-oriented LotusScript
Tony Higham 11.18.2001
Rating: -3.52- (out of 5)




|
Domino applications can be object-oriented in every sense of the word. This is an example of some truly object-oriented LotusScript code that will help you take your development skills to the next level.
In case you missed Tony Higham in our Live Expert Event on Tuesday, November 20th when he discussed how to use objects and LotusScript and how to use Domino and the UML (Unified Modeling Language) you can view the archived transcript here. Tony used the object-oriented LotusScript example featured in this tip.
Code
Public Class Cart
'Define the itemList attribute as a List of Item objects. The List enables us to
'store Item objects using the ProductId as a key value. Similar to a HashTable
'in Java
Private itemList List As Item
'Here is the constructor method for this class. This method is called when
'a new instance of the Cart class is created. Though it's not being used
'here, I am including it for completeness
Public Sub New()
End Sub
'This method adds an item to the shopping cart
Public Sub add( pid As String )
Dim cartItem As Item
'See if the item is already on the shopping cart
If Iselement( itemList( pid )) Then
'It's already in the cart so increase it's quantity
Set cartItem = itemList( pid )
' Increment the quantity of this item
Call cartItem.increment()
Else
'This particular product does not exist in the shopping cart
'so create a new Item object (which in turn will create a
'Product object for the Product ID passed to the add method
'and place it on the ItemList
Set cartItem = New Item( pid )
'Now add the new Item object to the itemList using the product ID
'as a key value so that we can look it up later to increment or
'decrement the quantity of this product that is in the cart
Set itemList( pid ) = cartItem
End If
End Sub
'This method removes an item from the shopping cart
Public Sub remove( pid As String )
Dim cartItem As Item
'Check that the product is already in the shopping cart
If Iselement( itemList( pid )) Then
'It's already in the cart so increase it's quantity
Set cartItem = itemList( pid )
'Increment the quantity of this item
Call cartItem.decrement()
'If the quantity has reached zero remove the item from the cart
If cartItem.getQty <= 0 Then
Erase Me.itemList( pid )
End If
'This particular product does not exist in the shopping cart
'so create a new Item object (which in turn will create a
'Product object for the Product ID passed to the add method
'and place it on the ItemList
Set cartItem = New Item( pid )
'Now add the new Item object to the itemList using the product ID
'as a key value so that we can look it up later to increment or
'decrement the quantity of this product that is in the cart
Set itemList( pid ) = cartItem
End If
End Sub
End Class
'The Item class serves as a container for a Product object with a count
'of how many of that product has been ordered. From a memory usage
'perspective this object containment hierarchy makes a lot of sense
'as the user may order the same Product several times
Public Class Item
'Define the properties provided by this class as private so that we can
'control whether they are read-only or read-write using get/set methods
'for reading from then and writing to the respectively. An Item object
'is used to store Product objects along with the quantity of the Product
'that the user desires
Private prod As Product
Private qty As Integer
'The constructor sub is used to initialize the contents of the Item object
Public Sub New( pid As String )
'Create a new Product object and store it in the internal property
Set Me.prod = New Product( pid )
'Set the quantity to one
Me.qty = 1
End Sub
'Methods for increasing and decreasing the quantity as more of this product
'is added to the shopping cart (or removed from the cart)
Public Sub increment()
Me.qty = Me.qty + 1
End Sub
Public Sub decrement
Me.qty = Me.qty - 1
End Sub
'The following two methods are used to access the value of the Product object
'and quantity stored by the Item object
Public Function getProduct() As Product
Set getProduct = Me.prod
End Function
Public Function getQty() As Integer
getQty = Me.qty
End Function
End Class
'The Product object contains all the basic data about the Products in a
'product catalog. It has private attributes to store the details about a
'given product, and makes that data available through get methods so
'they it's read-only data.
Public Class Product
'Define the properties provided by the class as private so that we can control
'whether they are read-only or read-write using get/set methods for reading
'and writing to them. This makes the class work just like a JavaBean or COM
'object
Private productId As String
Private description As String
Private price As Double
Sub New( pid As String )
'Here's where we'd look up the product information using the product ID as a key
'value into a database. As product information is relational in nature, and Domino
'is NOT a relational data store, it is likely that the lookup would be into a relational
'database such as DB2, SQL Server, Oracle etc. For the purposes of this example
'we'll just dummy up some results
productId = pid
' Store the product ID provided in the constructor description = "Widget"
' Total dummy response price = 24.99
End Sub
'The following functions are provide read-only access to the properties (attributes)
'offered by this class. The Me keyword refers to the variables defined above at
'the instance scope of this class
Public Function getProductId() As String
getProductId = Me.productId
End Function
Public Function getDescription() As String
getDescription = Me.description
End Function
Public Function getPrice() As Double
getPrice = Me.price
End Function
End Class
 |

|
|
 |
|
 |