One of the simplest and most commonly used patterns is definitely strategy pattern. We cannot pass a boring definition, so here you go:

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy algorithm vary independently from clients that use it.

You will find out that all strategy pattern really enforces is you to plan your code for change. In this article, I would like to present use of Strategy pattern in LotusScript.

More details can be found at Wikipedia or Head First Design Patterns book.

For the presentation example I have chosen product handling in a shopping cart. Why? Mainly because e-commerce is something most of us, Lotus developers have to deal with at some point in our careers.

So, imagine following scenario. You got a job at a software company that produces and sales business software. They used to do it via a simple product (e.g. events, electronic downloads, hard copies etc.) list on the web (using notes database) and a telephone number next to it. The company feels they should focus more on their customers and make them a friendly web environment for browsing and shopping. Thus, eliminating that phone call that no one likes to make. Now, you are the person to do it. And of course, they want it yesterday.

First, you need to sit down and think. They have at least three types of products, each displaying differently due to its delivery behavior. Also, a user can have a discount for a certain product. Here, the strategy pattern jumps in to help.

You need to create a behavioral interfaces. Or in this case two (first for delivery, second for discount). Unfortunately, LotusScript doesn’t do interfaces, so you will have to replace that with an abstract class.
So, two abstract classes, with single methods in it as shown below.

Class CProductDeliveryBehaviour
	Sub doDeliveryDisplay()
	End Sub
End Class

Class CProductDiscountBehaviour
	Sub doDiscount()
	End Sub
End Class

Next, you need to create all behavioral classes that you need. For delivery, you have three behaviors: on-site event, electronic download, or mailing the hard copy. For discount, there are only two behaviors thus far: discount and no discount. Pseudo-code is shown below.

Class CEDownload As CProductDeliveryBehaviour
	Sub doDeliveryDisplay
		'do e-download delivery display
	End Sub
End Class

Class CHardCopy As CProductDeliveryBehaviour
	Sub doDeliveryDisplay
		'do hardCopy delivery display
	End Sub
End Class

Class COnSiteEvent As CProductDeliveryBehaviour
	Sub doDeliveryDisplay
		'do onSite delivery display
	End Sub
End Class

Class CDiscount As CProductDiscountBehaviour
	Sub doDiscount
		'do discount handling
	End Sub
End Class

Class CNoDiscount As CProductDiscountBehaviour
	Sub doDiscount
		'do no discount handling
	End Sub
End Class

Now it is time you start thinking of products. Different product types normally do not share all properties. Thus, it is wise to create an abstract class for products and then several classes that inherit from that abstract class. In this example we obviously have at least two different product types. One are events, the other one is software. Pseudo-code is shown below.

Class CProduct
	Private m_deliveryBehaviour As CProductDeliveryBehaviour
	Private m_discountBehaviour As CProductDiscountBehaviour
	Private m_dPrice As Double
	Private m_strCode As String
	Private m_strName As String
	Private m_strDescription As String

	Property Set DeliveryBehaviour As CProductDeliveryBehaviour
		Set m_deliveryBehaviour = DeliveryBehaviour
	End Property

	Property Set DiscountBehaviour As CProductDiscountBehaviour
		Set m_discountBehaviour = DiscountBehaviour
	End Property

	Sub addToCart
	End Sub

	Sub addDiscount
		If (m_discountBehaviour Is Nothing) Then Exit Sub
		Call m_discountBehaviour.doDiscount
	End Sub

	Sub doBuy
	End Sub

	Sub doDisplay
		If (m_deliveryBehaviour Is Nothing) Then Exit Sub
		Call m_deliveryBehaviour.doDeliveryDisplay
	End Sub

	Sub removeFromCart
	End Sub

	Sub showDisplay
	End Sub
End Class

Class CEvent As CProduct
	Private m_dtStart As NotesDateTime
	Private m_dtEnd As NotesDateTime
	Private m_strLocation As String

	Sub New
	End Sub

	Sub showDisplay
		'do whatever you need to display a product
		Messagebox "I am an Event!"
	End Sub
End Class

Class CSoftware As CProduct
	Private m_strUrl As String

	Sub New
	End Sub

	Sub showDisplay
		'do whatever you need to display a product
		Messagebox "I am Software!"
	End Sub
End Class

Usage:

Sub Initialize
	Dim product As CProduct

	Set product = New CEvent
	Set product.deliveryBehaviour = New COnSiteEvent
	Set product.discountBehaviour = New CNoDiscount

	Call product.doDisplay
	Call product.addDiscount
	Call product.showDisplay

	Set product = New CSoftware
	Set product.deliveryBehaviour = New CHardCopy
	Set product.discountBehaviour = New CDiscount

	Call product.doDisplay
	Call product.addDiscount
	Call product.showDisplay

	Set product.deliveryBehaviour = New CEDownload
	Set product.discountBehaviour = New CDiscount

	Call product.doDisplay
	Call product.addDiscount
	Call product.showDisplay
End Sub

That is all there is to the Strategy Pattern. Please refer to above mentioned links for further guidance. If you have any further questions, comment.