<?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; singleton</title>
	<atom:link href="http://www.lotushints.com/tag/singleton/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>Thu, 29 Dec 2011 09:47:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Design patterns &#8211; Part 4: Factory method pattern</title>
		<link>http://www.lotushints.com/2008/12/design-patterns-part-4-factory-method-pattern/</link>
		<comments>http://www.lotushints.com/2008/12/design-patterns-part-4-factory-method-pattern/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 08:00:51 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=148</guid>
		<description><![CDATA[Factory method pattern definition: The Factory method pattern defines an interface for creating and objects, but lets subclasses to decide which class they will create. Huh? I will try to present this pattern on a simple example. Imagine you work in a company that sales software. However, this software is, due to different laws, different [...]]]></description>
			<content:encoded><![CDATA[<p>Factory method pattern definition:</p>
<blockquote><p>The Factory method pattern defines an interface for creating and objects, but lets subclasses to decide which class they will create.</p></blockquote>
<p><strong>Huh?</strong></p>
<p>I will try to present this pattern on a simple example. Imagine you work in a company that sales software. However, this software is, due to different laws, different for each country/region it is sold in (in our case EU and USA). You have two types of software (Pro and Basic that differ by price and package). You will need to create a single store that will allow users to only buy products from their country.<span id="more-148"></span></p>
<p>First, you can create an abstract class that will represent a product.</p>
<pre>Class CSoftware
	m_strName As String

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

	Sub New
	End Sub

	Sub PreparePackage ()
	End Sub

	Sub PrintPrice ()
	End Sub
End Class</pre>
<p>As you can see, the class itself is pretty simple. It contains a member variable for product name and has two methods that will prepare a package and print it&#8217;s price.<br />
Now (as you need two product types for two countries) you need to create 4 product classes that will inherit from CSoftware class. I will only show you both EU classes as others are pretty self explanatory.</p>
<pre>Class CEUSoftwareBasic As CSoftware
	Sub New
		Me.m_strName = "EU Basic"
	End Sub

	Sub PreparePackage ()
		'TODO: put your code here
	End Sub

	Sub PrintPrice ()
		'TODO: put your code here
	End Sub
End Class

Class CEUSoftwarePro As CSoftware
	Sub New
		Me.m_strName = "EU Pro"
	End Sub

	Sub PreparePackage ()
		'TODO: put your code here
	End Sub

	Sub PrintPrice ()
		'TODO: put your code here
	End Sub
End Class</pre>
<p>There. You are done with products. Now, you need two stores, that will create desired products. But first, you should create an abstract store class.</p>
<pre>Class CSoftwareStore
	Function orderSoftware (swtype As String) As CSoftware
		Dim sw As CSoftware

		Set sw = createSoftwarePackage (swtype)
		Call sw.PreparePackage ()
		Call sw.PrintPrice ()

		Set orderSoftware = sw
	End Function

	Function createSoftwarePackage (swtype As String) As CSoftware
	End Function
End Class</pre>
<p>As you can see, createSoftwarePackage method is empty. This is because createSoftwarePackage differs from store to store. Method orderSoftware, though, is the same for all stores and can be implemented in abstract class.<br />
Now, a source of both stores that inherit from CSoftwareStore.</p>
<pre>Class CEUSoftwareStore As CSoftwareStore
	Function createSoftwarePackage (swtype As String) As CSoftware
		Dim sw As CSoftware

		Select Case (Lcase (swtype))
		Case "basic": Set sw = New CEUSoftwareBasic ()
		Case "pro": Set sw = New CEUSoftwarePro ()
		End Select

		Set createSoftwarePackage = sw
	End Function
End Class

Class CUSASoftwareStore As CSoftwareStore
	Function createSoftwarePackage (swtype As String) As CSoftware
		Dim sw As CSoftware

		Select Case (Lcase (swtype))
		Case "basic": Set sw = New CUSASoftwareBasic ()
		Case "pro": Set sw = New CUSASoftwarePro ()
		End Select

		Set createSoftwarePackage = sw
	End Function
End Class</pre>
<p>Each store creates and returns an object depending on the type we wanted.</p>
<p><strong>But what will that bring me?</strong></p>
<p>You will only have to know which store you need to use and not care about the product returned. That is a job of each separate store. Thus, your code will be easier to read and to maintain.<br />
Pasted below is a sample execution code.</p>
<pre>Sub Initialize
	Dim storeEU As CEUSoftwareStore
	Dim storeUSA As CUSASoftwareStore
	Dim sw As CSoftware

	Set storeEU = New CEUSoftwareStore ()
	Set storeUSA = New CUSASoftwareStore ()

	Set sw = storeEU.orderSoftware ("basic")
	Print "Package: " &amp; sw.ProductName

	Set sw = storeUSA.orderSoftware ("pro")
	Print "Package: " &amp; sw.ProductName
End Sub</pre>
<p>The agent creates both stores and then for each store orders a software. Printed results should be:</p>
<pre>Package: EU Basic
Package: USA Pro</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2008/12/design-patterns-part-4-factory-method-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Design patterns &#8211; Part 3: Singleton pattern</title>
		<link>http://www.lotushints.com/2008/11/design-patterns-part-3-singleton-pattern/</link>
		<comments>http://www.lotushints.com/2008/11/design-patterns-part-3-singleton-pattern/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 08:00:59 +0000</pubDate>
		<dc:creator>Vladimir Kocjancic</dc:creator>
				<category><![CDATA[Code optimization]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Object-oriented development]]></category>
		<category><![CDATA[LotusScript]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.lotushints.com/?p=128</guid>
		<description><![CDATA[The simplest way to describe singleton class is to look at the definition: The Singleton Pattern ensures a class has only one instance, and provides a point of access to it. So why would you need that? Well, there are many objects you only need one of in your application (e.g. Logging, text trimming, for [...]]]></description>
			<content:encoded><![CDATA[<p>The simplest way to describe singleton class is to look at the definition:</p>
<blockquote><p><strong>The Singleton Pattern</strong> ensures a class has only one instance, and provides a point of access to it.</p></blockquote>
<p>So why would you need that? Well, there are many objects you only need one of in your application (e.g. Logging, text trimming, for loading strings from a view, etc.). It assures you that you only have one instance of the class and that no one can create another instance on its own. Thus, you need a global access point to deliver the handle to the class.<span id="more-128"></span></p>
<p><strong>How do we do that? </strong></p>
<p>I will use my text-trimming class (called CLimitedString) for this presentation.  The class is pretty simple. It&#8217;s basic task is to trim text after n characters and append &#8220;&#8230;&#8221; afterwards. Default limit is set to 30, but user can set other value via Limit property.</p>
<pre>Private Class CLimitedString
    Private m_nLimit As Integer

    Public Sub New ()
        'setting default value
        Me.m_nLimit = 30
    End Sub

    Public Property Get Limit As Integer
        Limit = Me.m_nLimit
    End Property

    Public Property Set Limit As Integer
        Me.m_nLimit = Limit
    End Property

    Public Function Process (source As String) As String
        If (Len (source) &lt; Me.Limit) Then
            Process = source
            Exit Function
        End If

        Process = Strleftback (Left (source, Me.Limit), " ") &amp;_
        "..."
    End Function
End Class</pre>
<p>As LotusScript does not support private constructors, we will have to create a private class instead. This means that you won&#8217;t be able to access this class from anywhere else but this ScriptLibrary. Thus, we need to create a global variable to store class instance and a script library function as a global point of access.<br />
Variable:</p>
<pre>Private currLimitedString As CLimitedString</pre>
<p>Script library function:</p>
<pre>Function GetLimitedStringInstance As Variant
    If (currLimitedString Is Nothing) Then
        Set currLimitedString = New CLimitedString
    End If

    Set GetLimitedStringInstance = currLimitedString
End Function</pre>
<p>As your class is private, this function must return <strong>Variant</strong>, as code using this class isn&#8217;t aware of class&#8217; existence. The usage of the object remains the same.</p>
<p><strong>How do I call my class?</strong></p>
<p>It is quite simple. All you need to do is include your library into an agent for example and then do the following where needed:</p>
<pre>...
Dim vLimitedString As Variant
...
vLimitedString = GetLimitedStringInstance
vLimitedString.Limit = 10
Messagebox vLimitedString.Process ("My very first singleton class")</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lotushints.com/2008/11/design-patterns-part-3-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

