<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lotushints &#187; custom classes</title>
	<atom:link href="http://www.lotushints.com/tag/custom-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lotushints.com</link>
	<description>Lotus Notes tips &#38; tricks you always hoped you will not need</description>
	<lastBuildDate>Wed, 14 Jul 2010 06:00:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Cleaning behind you</title>
		<link>http://www.lotushints.com/2010/04/cleaning-behind-you/</link>
		<comments>http://www.lotushints.com/2010/04/cleaning-behind-you/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 06:00:03 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[erase]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[Web services]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=494</guid>
		<description><![CDATA[Ending up a big project, I was brimming with confidence. The code was well structured, it ran fast despite using external web services and on top of that, it worked on test system. Oh boy, did it work. I should have seen a warning sign there. But no. I let my vanity kick  in and [...]]]></description>
			<content:encoded><![CDATA[<p>Ending up a big project, I was brimming with confidence. The code was well structured, it ran fast despite using external web services and on top of that, it worked on test system. Oh boy, did it work. I should have seen a warning sign there. But no. I let my vanity kick  in and enjoyed the good times. Yes, everything worked. Until&#8230;</p>
<p><span id="more-494"></span></p>
<p>&#8230;server crashed. With access violation problem. I feverishly scrolled through code, looking for an obvious error, but was unable to locate one at first glance. And than, slowly it dawned on me. I let garbage collection do my own dirty work. Unfortunately garbage collection in LotusScript has a small &#8220;feature&#8221;. It does not collect and disposes of web service consumers.</p>
<p>This is what my problem was. I created an abstract class that took care of initialization (including service consumer object). Then each data type had it&#8217;s own class that inherited from that abstract class. Needless to say that I forgot to add destructor that would clear web service consumer object. So, now, I had n web service consumer objects pointing to the same web service consumer, resulting in a memory leak of a size of an elephant after a huge meal and consequently a server crash.</p>
<p>But enough about me. Cleaning behind you in your code is a good practice. Not only it will make your code run smoother and look more spiffy, but it will also save you from feverish last minute code scrambling, busting your head and yelling like a mad man with a look of a rabid squirrel. So, let&#8217;s look what you can do.</p>
<p>1. <strong>Use destructors that actually do something useful in your classes.</strong></p>
<p>Having an empty Sub Delete (or none at all) is just not enough. Try  clearing your member variables as well. Specially if they are your  custom classes, web services, arrays or lists.<strong></strong></p>
<p><strong>2. Don&#8217;t be afraid to use Erase statement to clear arrays and lists you don&#8217;t need.<br />
</strong></p>
<p>Arrays and lists can take up a lot of your servers memory. Specially if you do an array of Notes objects. Be sure to clear them as soon as they are not needed anymore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2010/04/cleaning-behind-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lotus Notes 8.5.1</title>
		<link>http://www.lotushints.com/2009/10/lotus-notes-8-5-1/</link>
		<comments>http://www.lotushints.com/2009/10/lotus-notes-8-5-1/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 06:00:47 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Lotus Domino]]></category>
		<category><![CDATA[Lotus Notes]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=444</guid>
		<description><![CDATA[With the abundance of posts out there, I thought I would chip in my 5 cents worth. Yes, I know I am a bit late. Release 8.5.1 is for LotusScript developer everything he could ever hope for. Specially if you are into OO programming. Finally you don&#8217;t have to check your old classes what parameters [...]]]></description>
			<content:encoded><![CDATA[<p>With the abundance of posts out there, I thought I would chip in my 5 cents worth. Yes, I know I am a bit late.</p>
<p>Release 8.5.1 is for LotusScript developer everything he could ever hope for. Specially if you are into OO programming. Finally you don&#8217;t have to check your old classes what parameters they take, you can easily locate them by going to class definition and helper works. Now, you have really no excuse to start OO development in LotusScript.</p>
<p>On the other hand, 8.5.1 introduced some strange bugs. My colleague found an interesting bug while editing forms. For some reason, formula wasn&#8217;t saved when the document was. I think he solved it by removing formula completely and then re-pasting it back. Another funny thing is code formatting. Still not working properly. Hope to see it solved in 8.5.2.</p>
<p>Don&#8217;t get me wrong. I take 8.5.1 over any release any day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/10/lotus-notes-8-5-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 11: Bridge pattern</title>
		<link>http://www.lotushints.com/2009/06/design-patterns-part-11-bridge-pattern/</link>
		<comments>http://www.lotushints.com/2009/06/design-patterns-part-11-bridge-pattern/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 07:00:38 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[bridge]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[Lotus Notes]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=361</guid>
		<description><![CDATA[In part 11 of Design pattern series we will go into creating and using the Bridge design pattern. It is moderately used and uses encapsulation, inheritance and aggregation to separate responsibilities into other classes. The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently. Usage The pattern is most [...]]]></description>
			<content:encoded><![CDATA[<p>In part 11 of <a href="../category/design-patterns/">Design pattern series</a> we will go into creating and using the Bridge design pattern. It is moderately used and uses encapsulation, inheritance and aggregation to separate responsibilities into other classes.</p>
<blockquote><p><strong>The Bridge pattern</strong> decouples an abstraction from its implementation so that the two can vary independently.</p></blockquote>
<p><span id="more-361"></span></p>
<p><strong>Usage</strong></p>
<p>The pattern is most useful, when both classes (implementation and abstraction) are assigned to different task handling. In our example, we will look at how to separate data and actions to build a list of products and navigate it.</p>
<p><strong>Data representation class</strong></p>
<p>This is an abstract class with only navigational methods specified. Data representation class must implement navigational logic in its methods and thus assures that no other class will change or handle any properties without data representation class knowledge.</p>
<p>Also, for needs of our example, we will also define a type that will contain product id, name and price.</p>
<pre>Type TBridgeData
   strId As String
   strName As String
   dPrice As Double
End Type

Class CBridgeDataObject
   Sub NextRecord ()
   End Sub

   Sub PrevRecord ()
   End Sub

   Sub AddRecord (strId As String, strName As String,_
   dPrice As Double)
   End Sub

   Sub RemoveRecord (strId As String )
   End Sub

   Sub ShowRecord ()
   End Sub

   Function ShowAllRecords () As String
   End Function
End Class</pre>
<p><strong>Product data class</strong></p>
<p>Class itself inherits from Data representation abstract class. Also, it contains all navigational logic as well as data handling.</p>
<p>Beware that for this example, I did not actually load the data from the database. Instead I created five (5) products in the class construct. In real life, you should have read the data from your database!</p>
<pre>Class CBridgeProductData As CBridgeDataObject
   Private m_nCurrent As Integer
   Private m_AProducts () As TBridgeData

   Sub New ()
      m_ncurrent = 0

      Redim m_AProducts(4)

      m_AProducts(0).strId = "PR-0001"
      m_AProducts(0).strName = "Product 1"
      m_AProducts(0).dPrice = 100.00

      m_AProducts(1).strId = "PR-0002"
      m_AProducts(1).strName = "Product 2"
      m_AProducts(1).dPrice = 1000.00

      m_AProducts(2).strId = "PR-0003"
      m_AProducts(2).strName = "Product 3"
      m_AProducts(2).dPrice = 150.00

      m_AProducts(3).strId = "PR-0004"
      m_AProducts(3).strName = "Product 4"
      m_AProducts(3).dPrice = 400.00

      m_AProducts(4).strId = "PR-0005"
      m_AProducts(4).strName = "Product 5"
      m_AProducts(4).dPrice = 330.00
   End Sub

   Sub NextRecord ()
      If (m_nCurrent < Ubound (m_AProducts)) Then
         m_nCurrent = m_nCurrent + 1
      End If
   End Sub

   Sub PrevRecord ()
      If (m_nCurrent > 0) Then
         m_nCurrent = m_nCurrent - 1
      End If
   End Sub

   Sub AddRecord (strId As String, strName As String,_
   dPrice As Double)
      Dim n As Integer

      n = Ubound (m_AProducts) + 1
      Redim Preserve m_AProducts (n)

      m_AProducts(n).strId = strId
      m_AProducts(n).strName = strName
      m_AProducts(n).dPrice = dPrice
   End Sub

   Sub RemoveRecord (strId As String)
      Dim n As Integer
      Dim m As Integer
      Dim nCount As Integer
      Dim nIndex As Integer

      m = 0
      nIndex = -1
      nCount = Ubound (m_AProducts)
      For n = 0 To nCount
         If (m_AProducts (n).strId <> strId) Then
            m_AProducts (m).strId = m_AProducts (n).strId
            m_AProducts (m).strName = m_AProducts (n).strName
            m_AProducts (m).dPrice = m_AProducts (n).dPrice
            m = m + 1
         Else
            nIndex = n
         End If
      Next

      Redim Preserve m_AProducts (nCount - 1)
      If (m_nCurrent > n) Then Call Me.PrevRecord ()
   End Sub

   Sub ShowRecord ()
      Dim strDisplay As String

      strDisplay = m_AProducts (m_nCurrent).strId &#038; { } &#038;_
      m_AProducts (m_nCurrent).strName &#038; { €} &#038;_
      m_AProducts (m_nCurrent).dPrice

      Messagebox strDisplay
   End Sub

   Function ShowAllRecords() As String
      Dim n As Integer
      Dim strDisplay As String

      strDisplay = ""
      For n = 0 To Ubound (m_AProducts)
         strDisplay = strDisplay &#038; Chr(13) &#038;_
         m_AProducts (n).strId &#038; { } &#038;_
         m_AProducts (n).strName &#038; { €} &#038;_
         m_AProducts (n).dPrice
      Next

      ShowAllRecords = strDisplay
   End Function
End Class</pre>
<p><strong>Product base class</strong></p>
<p>Now, that we have our data classes created, we need to build an abstraction class that will call actual navigation and display the list contents or item if necessary. As we might have multiple classes that should have same functionality, it is a must to create a base class first. This class will encapsulate Data representation abstract class.</p>
<pre>Class CBridgeProductBase
   Private m_DataObj As CBridgeDataObject
   Private m_strGroup As String

   Public Property Get Data
      Set Data = m_DataObj
   End Property

   Public Property Set Data
      Set m_DataObj = Data
   End Property

   Sub New (strGroup As String)
      m_strGroup = strGroup
   End Sub

   Sub Next ()
      Call m_DataObj.NextRecord ()
   End Sub

   Sub Prev ()
      Call m_DataObj.PrevRecord ()
   End Sub

   Sub Add (strId As String, strName As String, dPrice As Double)
      Call m_DataObj.AddRecord (strId, strName, dPrice)
   End Sub

   Sub Remove (strId As String)
      Call m_DataObj.RemoveRecord (strId)
   End Sub

   Sub Show ()
      Call m_DataObj.ShowRecord()
   End Sub

   Sub ShowAll ()
      Messagebox "Product group: " &#038; m_strGroup &#038; Chr (13) &#038;_
      m_DataObj.ShowAllRecords()
   End Sub
End Class</pre>
<p><strong>Concrete product class</strong></p>
<p>Concrete product class in our case, will only be used to alter presentation when ShowAll method is called. This is not a must.</p>
<pre>Class CBridgeProducts As CBridgeProductBase
   Sub New (strGroup As String)
   End Sub

   Sub ShowAll ()
      Messagebox "Displaying all products"
      Call CBridgeProductBase..ShowAll ()
   End Sub
End Class</pre>
<p><strong>Implementation</strong></p>
<p>For test purposes, I have created an agent that will:</p>
<ul>
<li>create product list</li>
<li>navigate forward and backward</li>
<li>add an item to the list</li>
<li>remove an item from the list</li>
<li>display single product or all products at any time,</li>
</ul>
<pre>Sub Initialize
   Dim products As CBridgeProducts
   Dim data As CBridgeProductData

   Set products = New CBridgeProducts ("product group 1")
   Set products.Data = New CBridgeProductData ()

   Call products.Show ()
   Call products.Next ()
   Call products.Show ()
   Call products.Prev ()
   Call products.Show ()
   Call products.ShowAll ()

   Call products.Add ("PR-0010", "New product", 1475.00)
   Call products.ShowAll ()
   Call products.Remove ("PR-0004")
   Call products.ShowAll ()
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/06/design-patterns-part-11-bridge-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 10: Builder pattern</title>
		<link>http://www.lotushints.com/2009/05/design-patterns-part-10-builder-pattern/</link>
		<comments>http://www.lotushints.com/2009/05/design-patterns-part-10-builder-pattern/#comments</comments>
		<pubDate>Mon, 18 May 2009 06:00:47 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Domino]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[absract factory]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=349</guid>
		<description><![CDATA[In part 10 of Design pattern series we will take look at Builder design pattern. Albeit much similarity, this pattern it is not to be mistaken with Abstract factory pattern. As Abstract Factory emphasizes a family of products and returns the product immediately, Builder focuses on constructing complex object step by step, returning product in [...]]]></description>
			<content:encoded><![CDATA[<p>In part 10 of <a href="/category/design-patterns/">Design pattern series</a> we will take look at Builder design pattern. Albeit much similarity, this pattern it is not to be mistaken with Abstract factory pattern. As Abstract Factory emphasizes a family of products and returns the product immediately, Builder focuses on constructing complex object step by step, returning product in final step.</p>
<blockquote><p>The intention of the <strong>The Builder pattern</strong> is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.</p></blockquote>
<p><span id="more-349"></span></p>
<p><strong>Huh?</strong></p>
<p>The best way to explain how builder pattern works is to look at the example. Imagine you have to build product presentation for a company that sells built PCs. For clarity&#8217;s sake, let&#8217;s pretend that they sell only two types of computers. One for basic office needs, running Linux OS and one for pro gaming needs running Windows.</p>
<p><strong>Let&#8217;s go!</strong></p>
<p>First, you need a product class. This class will be able to store product type, add components and display itself when needed. This allows you to build product with as many components as needed.</p>
<pre>Class CBuilderProduct
	Private m_strProductName As String
	Private m_strLParts List As String

	Sub New (productName As String)
		m_strProductName = productName
	End Sub

	Sub Add (part As String, desc As String)
		m_strLParts (part) = desc
	End Sub

	Sub Show ()
		Dim strDisplay As String

		strDisplay = "Product: " &amp; m_strProductName
		Forall part In m_strLParts
			strDisplay = strDisplay &amp; Chr(13) &amp;_
                      Listtag (part) &amp; ": " &amp; part
		End Forall
		Messagebox strDisplay
	End Sub
End Class</pre>
<p>Next, you need a builder class. This is an abstract class, only specifying methods and variables used by end product classes that inherit from it.</p>
<pre>Class CBuilder
	Private m_Product As CBuilderProduct

	Property Get Product As CBuilderProduct
		Set Product = m_Product
	End Property

	Sub AddOS()
	End Sub

	Sub AddPC()
	End Sub
End Class</pre>
<p>Now, you are ready to create real product classes. In our case, this classes will represent Basic and Pro products.</p>
<pre>Class CBuilderProductBasic As CBuilder
   Sub New()
      Set m_Product = New CBuilderProduct ("Basic Package")
   End Sub

   Sub AddOS()
      Call m_Product.Add ("Operating system", "Fedora Core 10")
   End Sub

   Sub AddPC()
      Call m_Product.Add ("Computer", "Basic home computer")
   End Sub
End Class

Class CBuilderProductPro As CBuilder
   Sub New()
      Set m_Product = New CBuilderProduct ("Pro Package")
   End Sub

   Sub AddOS()
      Call m_Product.Add ("Operating system", "Windows 7")
   End Sub

   Sub AddPC()
      Call m_Product.Add ("Computer", "Pro gaming computer")
   End Sub
End Class</pre>
<p>You are almost done. But first, you need a catalogue (or shop) class. This class will actually contain algorithm for building desired products. Beware that<em> builder</em> parameter of <em>Create </em>method must be of type <em>Variant </em>(it should be <em>CBuilder</em>) or you will get an error while compiling your code that will use this design pattern!</p>
<pre>Class CBuilderCatalog
	Sub Create (builder As Variant)
		Call builder.AddPC()
		Call builder.AddOS()
	End Sub
End Class</pre>
<p>To test the code, I wrote a simple agent that simply outputs two message boxes. One for each product.</p>
<pre>Sub Initialize
	Dim builder As CBuilder
	Dim shop As CBuilderCatalog

	Set shop = New CBuilderCatalog ()

	Set builder = New CBuilderProductBasic ()
	Call shop.Create (builder)
	Call builder.Product.Show()

	Set builder = New CBuilderProductPro ()
	Call shop.Create (builder)
	Call builder.Product.Show()
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/05/design-patterns-part-10-builder-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 9: Mediator pattern</title>
		<link>http://www.lotushints.com/2009/04/design-patterns-part-9-mediator-pattern/</link>
		<comments>http://www.lotushints.com/2009/04/design-patterns-part-9-mediator-pattern/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 08:00:28 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Domino]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[mediator]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=320</guid>
		<description><![CDATA[This week, I present to you not much used, but quite useful Mediator pattern. Mediator pattern provides a unified interface to set of interfaces in a subsystem. Or, to simplify&#8230; Mediator pattern is mediator for communication between several classes. Example Now, the easiest way to explain this pattern is via example. Example itself is borrowed [...]]]></description>
			<content:encoded><![CDATA[<p>This week, I present to you not much used, but quite useful Mediator pattern.</p>
<blockquote><p><strong>Mediator pattern</strong> provides a unified interface to set of interfaces in a subsystem.</p></blockquote>
<p>Or, to simplify&#8230; Mediator pattern is mediator for communication between several classes.</p>
<p><span id="more-320"></span></p>
<p><strong>Example</strong></p>
<p>Now, the easiest way to explain this pattern is via example. Example itself is borrowed from <a href="http://www.dofactory.com/Patterns/PatternMediator.aspx" target="_blank">dofactory page</a>, but modified to fit LotusScript. It is about a simple support chat room. This chat room has several users of two types: admins and customers. I will call them participants. These participants can communicate amongst each other, presuming, they know user ids.</p>
<p>First, we need a chatroom template class, as we might end up with more than one chat room. Chat room will need to register participants and to be able to deliver messages to them.</p>
<pre>Class CChatroomTemplate
   Sub Register (participant As CParticipant)
   End Sub

   Sub Send (strFrom As String, strTo As String, strMsg As String)
   End Sub
End Class</pre>
<p>As participants need instance of chatroom class and chatroom needs list of participant class instances, we need to create chatroom and participant class frameworks and only then put code into it. Participants need to be able to send and receive messages. Finished classes are displayed below.</p>
<pre>Class CSupportChat As CChatroomTemplate
   m_LMembers List As CParticipant

   Sub Register(participant As CParticipant)
      If (Not Iselement (m_LMembers (participant.UserId))) Then
         Set m_LMembers (participant.UserId) = participant
      End If
      Set participant.Chatroom = Me
   End Sub

   Sub Send (strFrom As String, strTo As String, strMsg As String)
      Dim participant As CParticipant

      If (Not Iselement (m_LMembers (strTo))) Then Exit Sub
      Set participant = m_LMembers (strTo)
      Call participant.Receive (strFrom, strMsg)
   End Sub
End Class

Class CParticipant
   m_chatroom As CSupportChat
   m_strUserId As String

   Property Set Chatroom As CSupportChat
      Set m_chatroom = Chatroom
   End Property

   Property Get UserId As String
      UserId = m_strUserId
   End Property

   Property Set UserId As String
      m_strUserId = UserId
   End Property

   Sub new (strUserId As String)
      m_strUserId = strUserId
   End Sub

   Sub Send (strTo As String, strMsg As String)
      Call m_chatroom.Send (m_strUserId, strTo, strMsg)
   End Sub

   Sub Receive (strFrom As String, strMsg As String)
      Messagebox strFrom &amp; " to " &amp; m_strUserId &amp; ": " &amp; strMsg
   End Sub
End Class</pre>
<p>Now that base classes are set, it is time to create real participant classes. In our case, we need one for admins and one for regular users.</p>
<pre>Class CAdmin As CParticipant
   Sub new (strUserId As String)
   End Sub

   Sub Receive (strFrom As String, strMsg As String)
      Messagebox "Admin message"
      Call CParticipant..Receive (strFrom, strMsg)
   End Sub
End Class

Class CUser As CParticipant
   Sub new (strUserId As String)
   End Sub

   Sub Receive (strFrom As String, strMsg As String)
      Messagebox "User message"
      Call CParticipant..Receive (strFrom, strMsg)
   End Sub
End Class</pre>
<p><strong>Testing the code</strong></p>
<p>For testing purposes, I put it all in agent, created four users (of which one is admin) and sent 3 messages to admin and admin responded to first one. The code is below.</p>
<pre>Sub Initialize
   Dim chatroom As CSupportChat
   Dim user1 As CParticipant
   Dim user2 As CParticipant
   Dim user3 As CParticipant
   Dim user4 As CParticipant

   Set chatroom = New CSupportChat()
   Set user1 = New CUser ("User 1")
   Set user2 = New CUser ("User 2")
   Set user3 = New CUser ("User 3")
   Set user4 = New CAdmin ("Admin 1")

   Call chatroom.Register (user1)
   Call chatroom.Register (user2)
   Call chatroom.Register (user3)
   Call chatroom.Register (user4)

   Call user1.Send ("Admin 1", "Help?")
   Call user4.Send ("User 1", "What's wrong?")
   Call user2.Send ("Admin 1", "My server just crashed!!!")
   Call user3.Send ("Admin 1", "Network died!")
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/04/design-patterns-part-9-mediator-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 8: State pattern</title>
		<link>http://www.lotushints.com/2009/03/design-patterns-part-8-state-pattern/</link>
		<comments>http://www.lotushints.com/2009/03/design-patterns-part-8-state-pattern/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 08:00:35 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Domino]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=234</guid>
		<description><![CDATA[State pattern is probably most used in review cycles. The definition says: State pattern allows an object to appear as it can change its class by altering its behaviour and state. What are you talking about? Well, imagine you are in need to build a bug tracking database.  Bug reports in basics have four states. [...]]]></description>
			<content:encoded><![CDATA[<p>State pattern is probably most used in review cycles. The definition says:</p>
<blockquote><p><strong>State pattern</strong> allows an object to appear as it can change its class by altering its behaviour and state.</p></blockquote>
<p><strong>What are you talking about?</strong></p>
<p>Well, imagine you are in need to build a bug tracking database.  Bug reports in basics have four states. First they are <strong>unassigned</strong> until someone is solving them. When they are taken over, they become <strong>assigned</strong>, specifying that someone is already working on them. After bug fix has been produced, it is a good practice to <strong>send </strong>it<strong> to testing</strong>. Then it is up to QA to confirm that bug fix is <strong>complete </strong>or whether it is not.</p>
<p><span id="more-234"></span></p>
<p><strong>Show me the code!</strong></p>
<p>Example will be based on simple bug report specified above. However, I will skip actual action implementation as it varies based on application needs. Instead, I will use message box to print out the action report.</p>
<p>So, now, first what you need is a state abstract class that will implement all methods needed for transition between states (in our case: unassigned, assigned, send to testing and complete).</p>
<pre>Class CState
	Sub TakeOver()
	End Sub

	Sub SendToTesting()
	End Sub

	Sub Complete()
	End Sub
End Class</pre>
<p>Then, what you need is a context class that will be used when changing state of a bug report.</p>
<pre>Class CStateBugReport
   stateUnassigned As CState
   stateAssigned As CState
   stateReadyForTesting As CState
   stateCompleted As CState
   stateCurrent As CState

   Sub New
      Set stateUnassigned = New CStateUnassigned (Me)
      Set stateAssigned = New CStateAssigned (Me)
      Set stateReadyForTesting = New CStateSentToTesting (Me)
      Set stateCompleted = New CStateCompleted (Me)

      Set stateCurrent = stateUnassignedb
   End Sub

   Sub Assign()
      Call stateCurrent.TakeOver()
   End Sub

   Sub SendToTesting()
      Call stateCurrent.SendToTesting()
   End Sub

   Sub Complete()
      Call stateCurrent.Complete()
   End Sub

   Function GetAssignedState() As CState
      Set GetAssignedState = stateAssigned
   End Function

   Function GetReadyForTestingState() As CState
      Set GetReadyForTestingState = stateReadyForTesting
   End Function

   Function GetCompletedState() As CState
      Set GetCompletedState = stateCompleted
   End Function

   Sub SetState( state As CState )
      Set Me.stateCurrent = state
   End Sub
End Class</pre>
<p>And now, we need to implement all four states classes.</p>
<pre>Class CStateUnassigned As CState
   bugReport As CStateBugReport

   Sub New (bugReport As CStateBugReport)
      Set Me.bugReport = bugReport
   End Sub

   Sub TakeOver ()
      Messagebox "Took over!"
      Call Me.bugReport.SetState (Me.bugReport.GetAssignedState())
   End Sub

   Sub SendToTesting()
      Messagebox "Unassigned reports cannot be sent to testing"
   End Sub

   Sub Complete()
      Messagebox "Unassigned reports cannot be completed"
   End Sub
End Class

Class CStateAssigned As CState
   bugReport As CStateBugReport

   Sub New (bugReport As CStateBugReport)
      Set Me.bugReport = bugReport
   End Sub

   Sub TakeOver ()
      Messagebox "Report already assigned"
   End Sub

   Sub SendToTesting()
      Messagebox "Sent to testing!"
      Call Me.bugReport.SetState
         (Me.bugReport.GetReadyForTestingState())
   End Sub

   Sub Complete()
      Messagebox "Untested reports cannot be completed"
   End Sub
End Class

Class CStateSentToTesting As CState
   bugReport As CStateBugReport

   Sub New (bugReport As CStateBugReport)
      Set Me.bugReport = bugReport
   End Sub

   Sub TakeOver ()
      Messagebox "Report already assigned"
   End Sub

   Sub SendToTesting()
      Messagebox "Report already in testing"
   End Sub

   Sub Complete()
      Messagebox "Complete!"
      Call Me.bugReport.SetState (Me.bugReport.GetCompletedState())
   End Sub
End Class

Class CStateCompleted As CState
   bugReport As CStateBugReport

   Sub New (bugReport As CStateBugReport)
      Set Me.bugReport = bugReport
   End Sub

   Sub TakeOver ()
      Messagebox "Report already completed"
   End Sub

   Sub SendToTesting()
      Messagebox "Report already completed"
   End Sub

   Sub Complete()
      Messagebox "Report already completed"
   End Sub
End Class</pre>
<p>This is all there is to it. To test it, I wrote a simple agent that goes through states and in each new state also tests wrong transitions (e.g. from unassigned to complete etc.).</p>
<pre>Sub Initialize
	Dim bugReport As New CStateBugReport()

	Call bugReport.SendToTesting()
	Call bugReport.Complete()

	Call bugReport.Assign()
	Call bugReport.Assign()
	Call bugReport.Complete()

	Call bugReport.SendToTesting()
	Call bugReport.Assign()
	Call bugReport.SendToTesting()

	Call bugReport.Complete()
	Call bugReport.Assign()
	Call bugReport.SendToTesting()
	Call bugReport.Complete()
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/03/design-patterns-part-8-state-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling Java classes from LotusScript</title>
		<link>http://www.lotushints.com/2009/02/calling-java-classes-from-lotusscript/</link>
		<comments>http://www.lotushints.com/2009/02/calling-java-classes-from-lotusscript/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 08:00:19 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Notes]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[user registration]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=210</guid>
		<description><![CDATA[Have you ever wondered how you could call already written and quite useful Java classes in your LotusScript code? To me, this moment was, when I was trying to implement other department&#8217;s code into my own. Unfortunately I wasn&#8217;t skilled enough in ways of object development, LS2J  and googling back then to actually figure out [...]]]></description>
			<content:encoded><![CDATA[<p><span>Have you ever wondered how you could call already written and quite useful Java classes in your <span>LotusScript</span> code? To me, this moment was, when I was trying to implement other department&#8217;s code into my own. Unfortunately I wasn&#8217;t skilled enough in ways of object development, LS2J  and googling back then to actually figure out that was possible. I remember thinking: &#8220;IBM implemented Java in Lotus Notes, they must have done some adaptor for it.&#8221;, but was unable to figure it out. Until couple of years back, when I got a </span><a href="http://www.randsinrepose.com/archives/2002/07/10/the_dark_underbelly_of_holy_shit.html">Holy shit!</a> moment, finding <a href="http://www.nsftools.com/blog/blog-11-2004#11-19-04">this blog post</a>.</p>
<p><span id="more-210"></span><strong>But why would I need that?</strong></p>
<p><span>As you can see from the above examples, you can implement Java functionality to your <span>LotusScript</span> code. This can be good if your Java programming skills aren&#8217;t that great or you already have some code in <span>LotusScript</span> and you wish to add functionality. For me, it would do fine in cases where I converted documents in a database to PDF on user request, or when I needed an agent to <span>login</span> user to notes via web in the background, or as I mentioned, when I needed to implement some functionality from other department in my already existing code. Instead, I re-wrote several agents into Java, which in the end didn&#8217;t prove to be that bad of a decision, but about that some other day.</span></p>
<p><strong>But how do I do that?</strong></p>
<p>There is a functionality called <strong>LS2J </strong><span>that does exactly that for you. If you will, you can imagine it as an adaptor for <span>LotusScript</span> that allows you to call Java classes. I will give you a small example. It is based on my user <span>login</span> class, although, there is really no code there to log in the user.</span></p>
<p><span>First you need to create/obtain a java library that does what you want. I named that library <span>jclass</span>:<span>UserLogin</span>.</span></p>
<pre><span>public class CUserLogin {
   String m_strUsername;
   String m_strPassword;

   public void Initialize(String strUsername, String strPassword) {
      if ((strUsername.length() == 0) ||
          (strPassword.length() == 0))
         return;

      m_strUsername = strUsername;
      m_strPassword = strPassword;
   }

   public boolean run() {
      // add code that will perform auto-login
      return true;
   }
}</span></pre>
<p><span>Then, I strongly suggest you create a <span>LotusScript</span> library, containing a class that will do actual Java to <span>LotusScript</span> conversion. Why? So you have to do it only once.</span><br />
My script library called class:UserLogin is depicted below.</p>
<pre><span><span>Uselsx</span> "*<span>javacon</span>"</span>
<span>Use "<span>jclass</span>:<span>UserLogin</span>"</span>

<span>Class <span>CUserLogin</span></span>
<span>	<span>loginObj</span> As <span>JavaObject</span></span>

<span>	Sub New (<span>strUsername</span> As String, <span>strPassword</span> As String)</span>
		Dim js As New JAVASESSION
<span>		Dim <span>loginClass</span> As JAVACLASS</span>

<span>		Set <span>loginClass</span> = <span>js</span>.<span>GetClass</span> ("<span>CUserLogin</span>")</span>
<span>		Set <span>loginObj</span> = <span>loginClass</span>.<span>CreateObject</span> ()</span>

<span>		Call <span>loginObj</span>.Initialize (<span>strUsername</span>, <span>strPassword</span>)</span>
	End Sub

	Function run() As Boolean
<span>		run = <span>loginObj</span>.run()</span>
	End Function
End Class</pre>
<p><span>And that is it. Now all you have to do, is use this script library in your code. Where you need it. I created a simple agent that will pop-up a window with text depicting <span>login</span> success.</span></p>
<pre><span>Use "class:<span>UserLogin</span>"</span>

Sub Initialize
<span>	Dim <span>userLogin</span> As <span>CUserLogin</span></span>

<span>	Set <span>userLogin</span> = New <span>CUserLogin</span> ("test", "test123")</span>
	If (userLogin.run()) Then
<span>		<span>Messagebox</span> "<span>Login</span> succeeded"</span>
	Else
<span>		<span>Messagebox</span> "<span>Login</span> failed!"</span>
	End If
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/02/calling-java-classes-from-lotusscript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 7: Template method pattern</title>
		<link>http://www.lotushints.com/2009/02/design-patterns-part-7-template-method-pattern/</link>
		<comments>http://www.lotushints.com/2009/02/design-patterns-part-7-template-method-pattern/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 08:00:35 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Domino]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[template method]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=200</guid>
		<description><![CDATA[In this article, I would like to present you a design pattern that is not so common, but for sure, I wish, that in the past I would have used it. It would certainly make my life much easier. So, what is this Template method pattern all about? The Template Method Pattern defines the skeleton [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I would like to present you a design pattern that is not so common, but for sure, I wish, that in the past I would have used it. It would certainly make my life much easier. So, what is this Template method pattern all about?</p>
<blockquote><p><strong>The Template Method Pattern</strong> defines the skeleton of a an algorithm in one method. Some steps are deferred to subclasses, which are allowed to alter certain algorithm steps without changing the skeleton of an algorithm.</p></blockquote>
<p><span id="more-200"></span></p>
<p><strong>Say what?</strong></p>
<p>Well, to simplify the definition, let&#8217;s look at an example. Imagine you have several product types. Your assigned task was to build a product catalogue on the web. All product types will use tables for representation, however not all products can be displayed the same. This is a perfect task for Template method pattern.</p>
<p><strong>But why?</strong></p>
<p>Think about it. Your catalogue needs to be displayed differently for each product, meaning you need to get different sets of product for each display. I agree, you could have written multiple agents, or just use multiple methods in an agent, but think about maintainability. And on top of it all, to display each product type, your algorithm is the same. First you need to do some <strong>initialization</strong>, then you need to <strong>select</strong> desired products. Next step is to <strong>process</strong> document set to create a display. In the end, all you need to do is <strong>print</strong> the results and <strong>terminate</strong> (or perform a clean up). Steps <strong>select</strong> and <strong>process</strong> are product type dependant. All other steps of the algorithm are common.</p>
<p><strong>Example</strong></p>
<p>For example let&#8217;s pretend that we have two types of products: regular classroom courses and e-courses. Regular classroom courses (or just courses) are scheduled courses, while e-courses are actually a 1 month subscription to some content (like video, PDF, etc.).</p>
<p>First, we need to create some storage classes: CProduct and CPrice, that will store information about products. This is more efficient than just have a collection of NotesDocuments.</p>
<pre>Class CPrice
	Public strDesc As String
	Public dPrice As Double
End Class

Class CProduct
	m_strName As String
	m_strType As String
	Public m_Price() As CPrice

	Property Get Label As String
		Label = Me.m_strName
	End Property

	Property Get MyType As String
		MyType = Me.m_strType
	End Property

	Sub New (strName As String, strType As String)
		Me.m_strName = strName
		Me.m_strType = strType
		Redim Me.m_Price (0)
	End Sub

	Sub AddPrice (strDesc As String, dPrice As Double)
		Dim nIndex As Integer

		nIndex = 0
		If (Not Me.m_Price (0) Is Nothing) Then
                     nIndex = Ubound (Me.m_Price) + 1
              End If

		Redim Preserve Me.m_price (nIndex)
		Set Me.m_price (nIndex) = New CPrice
		Me.m_Price (nIndex).strDesc = strDesc
		Me.m_Price (nIndex).dPrice = dPrice
	End Sub
End Class</pre>
<p>Class CPrice contains a description of price (subscription or schedule date) and the price itself. Class CProduct defines a product. Each product can have multiple prices.</p>
<p>Now, to our abstract class that will define the algorithm and required methods.</p>
<pre>Class CTemplateProduct
	m_strHtml As String
	m_products() As CProduct

	Sub Initialize()
		Redim m_products (0)
	End Sub

	Sub Select()
	End Sub

	Sub Process()
	End Sub

	Sub Print()
		Print m_strHtml
	End Sub

	Sub Terminate()
		Erase m_products
	End Sub

	Sub Run()
		Call Me.Initialize()
		Call Me.Select()
		Call Me.Process()
		Call Me.Print()
		Call Me.Terminate()
	End Sub
End Class</pre>
<p>As you can see, methods Initialize, Print and Terminate are already implemented.<br />
Next, we need to create classes for each product type. These classes will inherit from the abstract class.</p>
<pre>Class CTemplateCourse As CTemplateProduct
   Sub Select()
      Redim Preserve Me.m_products (1)
      Set Me.m_products (0) =
         New CProduct ("Course 1", "Lecture")

      Call Me.m_products (0).AddPrice ("Feb 3rd, 2009", 1000.0)

      Set Me.m_products (1) =
         New CProduct ("Exam 1", "Lecture &amp; exam")

      Call Me.m_products (1).AddPrice ("Feb 20th, 2009", 2000.0)
   End Sub

   Sub Process()
      Dim n As Integer
      Dim nPrice As Integer
      Dim nLBound As Integer
      Dim nUBound As Integer
      Me.m_strHtml = {&lt;table border="0"&gt;}

      For n = Lbound (Me.m_products) To Ubound (Me.m_products)
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;tbody&gt;&lt;tr&gt;}
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;td colspan="2"&gt;}
         Me.m_strHtml = Me.m_strHtml &amp; _
         Me.m_products (n).Label &amp; { - }

         Me.m_strHtml = Me.m_strHtml &amp; _
         Me.m_products (n).MyType &amp; {&lt;/td&gt;}

         Me.m_strHtml = Me.m_strHtml &amp; {&lt;/tr&gt;}
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;tr&gt;}

         nLBound = Lbound(Me.m_products (n).m_Price)
         nUBound = Ubound (Me.m_products (n).m_Price)
         For nPrice = nLBound To nUBound
            Me.m_strHtml = Me.m_strHtml &amp; {&lt;td&gt;}
            Me.m_strHtml = Me.m_strHtml &amp; _
            Me.m_products (n).m_Price (nPrice).strDesc 

            Me.m_strHtml = Me.m_strHtml &amp; {&lt;/td&gt;}
            Me.m_strHtml = Me.m_strHtml &amp; {&lt;td&gt;}
            Me.m_strHtml = Me.m_strHtml &amp; _
            Me.m_products (n).m_Price (nPrice).dPrice
            Me.m_strHtml = Me.m_strHtml &amp; {&lt;/td&gt;}
         Next
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;/tr&gt;}
      Next

      Me.m_strHtml = Me.m_strHtml &amp; {&lt;/tbody&gt;}
   End Sub
End Class

Class CTemplateECourse As CTemplateProduct
   Sub Select()
      Redim Preserve Me.m_products (1)
      Set Me.m_products (0) =
         New CProduct ("E-Course 1", "eBook")
      Call Me.m_products (0).AddPrice ("1 month", 100.0)

      Set Me.m_products (1) =
         New CProduct ("E-Course 2", "ePresentation")

      Call Me.m_products (1).AddPrice ("1 month", 200.0)
   End Sub

   Sub Process()
      Dim n As Integer
      Dim nPrice As Integer
      Dim nLBound As Integer
      Dim nUBound As Integer

      Me.m_strHtml = {&lt;table border="0"&gt;}

      For n = Lbound (Me.m_products) To Ubound (Me.m_products)
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;tbody&gt;&lt;tr&gt;}
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;td&gt;} &amp;_
         Me.m_products (n).Label &amp; {&lt;/td&gt;}
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;td&gt;} &amp; _
         Me.m_products (n).MyType &amp; {&lt;/td&gt;}

         nLBound = Lbound(Me.m_products (n).m_Price)
         nUBound = Ubound (Me.m_products (n).m_Price)
         For nPrice = nLBound To nUBound
            Me.m_strHtml = Me.m_strHtml &amp; {&lt;td&gt;} &amp; _
            Me.m_products (n).m_Price (nPrice).strDesc

            Me.m_strHtml = Me.m_strHtml &amp; {&lt;br/&gt;} &amp; _
            Me.m_products (n).m_Price (nPrice).dPrice &amp; {&lt;/td&gt;}
         Next
         Me.m_strHtml = Me.m_strHtml &amp; {&lt;/tr&gt;}
      Next

      Me.m_strHtml = Me.m_strHtml &amp; {&lt;/tbody&gt;&lt;/table&gt;}
   End Sub
End Class</pre>
<p>As you can no doubt see, I have simplified the Select method (this is due to me being to lazy to actually create a form, a view and some 6 documents). Also, method Process creates different HTML for each product type.</p>
<p>Interesting thing, this is about it.<br />
All you have to do is some code that will use this. Here is my test agent, that should be run via browser.</p>
<pre>Sub Initialize
	Dim course As CTemplateProduct
	Dim eCourse As CTemplateProduct

	Set course = New CTemplateCourse ()
	Call course.Run()

	Set eCourse = New CTemplateECourse()
	Call eCourse.Run()
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/02/design-patterns-part-7-template-method-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 6: Observer pattern</title>
		<link>http://www.lotushints.com/2009/02/design-patterns-part-6-observer-pattern/</link>
		<comments>http://www.lotushints.com/2009/02/design-patterns-part-6-observer-pattern/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 08:00:33 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Lotus Notes]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[observer]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=183</guid>
		<description><![CDATA[One of the most used patterns is also a pattern that hears by the name Observer. What does it do? Well, imagine you have a stock portfolio and you need to notify your investors every time the value of their stock changes. First, your investors need to subscribe to the stock they would like to [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most used patterns is also a pattern that hears by the name Observer. What does it do? Well, imagine you have a stock portfolio and you need to notify your investors every time the value of their stock changes. First, your investors need to <strong>subscribe</strong> to the stock they would like to be notified about. Next, whenever new stock value is <strong>published</strong>, subscribers are notified. Much like RSS subscription. But what about definition:</p>
<blockquote><p><strong>The Observer pattern</strong> defines a one-to-many dependency between objects so that when one object changes state, all of its dependants are notified and updated automatically.</p></blockquote>
<p><span id="more-183"></span><br />
<strong>Fine, but what can I do with that?</strong></p>
<p>We will build an above mentioned example of a stock and it&#8217;s investors. First, you will need to create an observer abstract class:</p>
<pre>Class CObserver
	m_strName As String

	Property Get ObserverName As String
		ObserverName = Me.m_strName
	End Property

	Sub New (strName As String)
		Me.m_strName = strNAme
	End Sub

	Sub Update()
	End Sub
End Class</pre>
<p>As you can see, it contains a name, which will be used as unique id, a constructor, where the name is set and subroutine called Update that will be called when new value of our stock will be set.</p>
<p>Now, that we have an observer, we can create a subject class that will be an abstract class used by our stock.</p>
<pre>Class CObserverSubject
	m_oList List As CObserver

	Sub Attach (observer As CObserver)
	  If (Not Iselement (m_oList (observer.ObserverName))) Then
	     Set m_oList (observer.ObserverName) = observer
	  End If
	End Sub

	Sub Detach (observer As CObserver)
	  If (Iselement (m_oList (observer.ObserverName))) Then
	     Erase m_oList (observer.ObserverName)
	  End If
	End Sub

	Sub Notify ()
		Forall observer In m_oList
			Call observer.Update()
		End Forall
	End Sub
End Class</pre>
<p>The class contains list of all investors (observers) and operations to add (Attach ()) or remove (Detach ()) them. There is also routine Notify, that will update each and every investor.</p>
<p>Framework is now set. Moving on to concrete classes. First, we will create a concrete subject class called ObserverStock.</p>
<pre>Class CObserverStock As CObserverSubject
	m_strName As String
	m_dValue As Double

	Property Get StockName As String
		StockName = Me.m_strName
	End Property

	Property Get Value As Double
		Value = Me.m_dValue
	End Property

	Property Set Value As Double
		Me.m_dValue = Value
		Call Notify()
	End Property

	Sub New (strName As String, dValue As Double)
		Me.m_strName = strName
		Me.m_dValue = dValue
	End Sub
End Class</pre>
<p>The class inherits from ObserverSubject class. Also, it has a property for obtaining stock name and properties for obtaining and setting stock value. However, when setting stock value, not only value is set, but there is also a call to Notify method, specified in ObserverSubject class. This will notify all subscribers of new value.</p>
<p>The only thing missing now is observer concrete class called ObserverInvestors.</p>
<pre>Class CObserverInvestor As CObserver
	m_Stock As CObserverStock

	Property Get Stock As CObserverStock
		Set Stock = Me.m_Stock
	End Property

	Property Set Stock As CObserverStock
		Set Me.m_Stock = Stock
	End Property

	Sub New (strName As String)
	End Sub

	Sub Update()
		Messagebox Me.m_strName &amp; ": " &amp;_
		m_Stock.StockName &amp; " value is " &amp; m_Stock.Value
	End Sub
End Class</pre>
<p>And it is simple enough. It needs a constructor with name as parameter (due to his parent class), a property that ties stock to the class and Update () function that will ideally send an e-mail to investors, but in this case just pops up a message with investor and stock info.</p>
<p><strong>Usage of previous classes</strong></p>
<p>To use previously stated classes, I wrote an agent that will create a stock, add three investors to it, and change value. Then, last observer will be removed from notification list and value will be updated again. When running this agent, you should first get three pop up messages and then only two.</p>
<pre>Sub Initialize
	Dim observer As CObserverInvestor
	Dim stock As CObserverStock

	Set stock = New CObserverStock ("MyStock", 120.0)

	Set observer = New CObserverInvestor ("Investor 1")
	Set observer.Stock = stock
	Call stock.Attach (observer)

	Set observer = New CObserverInvestor ("Investor 2")
	Set observer.Stock = stock
	Call stock.Attach (observer)

	Set observer = New CObserverInvestor ("Investor 3")
	Set observer.Stock = stock
	Call stock.Attach (observer)

	stock.Value = 100.0

	Call stock.Detach (observer)
	stock.Value = 102.5
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/02/design-patterns-part-6-observer-pattern/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 5: Adapter pattern</title>
		<link>http://www.lotushints.com/2009/01/design-patterns-part-5-adapter-pattern/</link>
		<comments>http://www.lotushints.com/2009/01/design-patterns-part-5-adapter-pattern/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 08:00:29 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[Lotus Notes]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[custom classes]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[Object-oriented development]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=162</guid>
		<description><![CDATA[Another quite useful pattern is Adapter pattern. Adapters represent an interface between two different classes. For example imagine being a continental EU citizen travelling to UK. For you to plug in to UK power plug, you need an adapter that will take the interface your connector has and output the interface UK power plug demands. [...]]]></description>
			<content:encoded><![CDATA[<p>Another quite useful pattern is Adapter pattern. Adapters represent an interface between two different classes. For example imagine being a continental EU citizen travelling to UK. For you to plug in to UK power plug, you need an adapter that will take the interface your connector has and output the interface UK power plug demands.<span id="more-162"></span><br />
Definition of Adapter pattern is:</p>
<blockquote><p>The<strong> Adapter pattern</strong> converts the interface of a class into an interface the client expects. Adapter lets classes work together even if they couldn&#8217;t otherwise due to incompatible interfaces.</p></blockquote>
<p><strong>So how does this work in practice?</strong></p>
<p>You will mostly need adapters when you will obtain code from someone else. But just for the sake of simplicity, we will look into unlikely scenario, when you will get a code that prints &#8220;Hello World!&#8221; and write an adaptor that displays &#8220;Hello adapters!&#8221; in message box using the same interface.</p>
<p>First you need a client class and it&#8217;s interface. As we know, interfaces are not implemented in LotusScript. Thus, an abstract class will be used instead. Then we&#8217;ll need a client class. Both are pasted below:</p>
<pre>Class CHelloWorldInterface
	Sub printText ()
	End Sub
End Class

Class CHelloWorld As CHelloWorldInterface
	Sub printText ()
		Print "Hello World!"
	End Sub
End Class</pre>
<p>Now, we need to create our interface that displays &#8220;Hello adapters!&#8221; in message box.</p>
<pre>Class CHelloAdaptersInterface
	Sub msgboxText ()
	End Sub
End Class

Class CHelloAdapters As CHelloAdaptersInterface
	Sub msgboxText ()
		Messagebox "Hello Adapters!"
	End Sub
End Class</pre>
<p>All we need now is an adapter.</p>
<pre>Class CHelloAdpatersAdapter As CHelloWorldInterface
	m_helloAdapter As CHelloAdaptersInterface

	Sub new (helloAdapter As CHelloAdaptersInterface)
		Set Me.m_helloAdapter = helloAdapter
	End Sub

	Sub printText()
		Call m_helloAdapter.msgboxText()
	End Sub
End Class</pre>
<p>And to bind it together, here is an agent that will output both results.</p>
<pre>Sub Initialize
	Dim hw As CHelloWorld
	Dim ha As CHelloAdapters
	Dim haa As CHelloWorldInterface

	Set hw = New CHelloWorld ()
	Set ha = New CHelloAdapters ()
	Set haa = New CHelloAdaptersAdapter (ha)

	'test HelloAdapters
	Call ha.msgboxText()

	'test HelloWorld
	Call testHello (hw)

	'test HelloAdaptersAdapter
	Call testHello (haa)
End Sub

Sub testHello (hello As CHelloWorldInterface)
	Call hello.printText()
End Sub</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2009/01/design-patterns-part-5-adapter-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
