<?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>Vinothbabu&#039;s Desk! &#187; Design Patterns</title>
	<atom:link href="http://vinothbabu.com/category/flex/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinothbabu.com</link>
	<description>When Smart becomes Dumb!</description>
	<lastBuildDate>Thu, 31 Mar 2011 09:53:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Design Patterns : Singleton Pattern</title>
		<link>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/</link>
		<comments>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 04:40:59 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Chess board]]></category>
		<category><![CDATA[Singleton Pattern]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=283</guid>
		<description><![CDATA[The Singleton pattern is very similar to creator pattern. This is very widely used pattern. Now a question might raise in the mind that why do we need this pattern and when it should be applied. Let us take a situation where you want to build a chess game. Where does the Singleton Pattern apply [...]]]></description>
			<content:encoded><![CDATA[<p>The Singleton pattern is very similar to creator pattern. This is very widely used pattern. Now a question might raise in the mind that why do we need this pattern and when it should be applied. Let us take a situation where you want to build a chess game. Where does the Singleton Pattern apply here?<span id="more-283"></span></p>
<p>chessboard is one, which is a square-checkered board with 64 squares arranged in an eight-by-eight grid. At the start, each player controls sixteen pieces: one king, one queen, two rooks, two knights, two bishops, and eight pawns. The object of the game is to checkmate  the opponent&#8217;s king, whereby the king is under immediate attack (in &#8220;check&#8221;) and there is no way to remove or defend it from attack on the next move.</p>
<p>Here your chessboard is Singleton class as its state should never change. It means even at nth movement, the board should hold all the records of pieces moved and its state. Let us now drive back to technical discussion.</p>
<p>The Singleton pattern allows a class to only one instance and it does provide a global access to the instance. How do we go on to make a Singleton class.</p>
<blockquote><p>Sample code : SingletonClass.as</p>
<p>package com.sophiacom.as3.designpatterns.creational.singleton<br />
{<br />
public class SingletonClass<br />
{<br />
private static var _instance:SingletonClass;</p>
<p>public function SingletonClass(enforcer:SingletonEnforcer) {}</p>
<p>public static function getInstance():SingletonClass {<br />
if (SingletonClass._instance == null) {<br />
SingletonClass._instance = new SingletonClass(new SingletonEnforcer());<br />
}<br />
return SingletonClass._instance;<br />
}<br />
}<br />
}</p>
<p>class SingletonEnforcer {}</p>
</blockquote>
<p>First we are defining a private static property that holds the single instance of the class.<br />
Then we go with a public static function that provides access to the single instance created.<br />
A private constructor that restrict access in instantiating the class.</p>
<p>Oops ActionScript 3.0 does not have a private constructor! So, to do this, the solution is to force a parameter in the constructor while type isn&#8217;t accessible. Just with a class outer the package of the class but defined in the same file, because in ActionScript 3, only one class can be visible.</p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to MVP for Flex</title>
		<link>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/</link>
		<comments>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:46:00 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=231</guid>
		<description><![CDATA[I was today having a discussion with my amigos on the various design patterns and we were usually struck with mind blowing MVP pattern. I did a Bing on reseearch but could not eventually find a single article which explains the users very clear of this pattern. That&#8217;s the reason of this post. Many people [...]]]></description>
			<content:encoded><![CDATA[<p>I was today having a discussion with my amigos on the various design patterns and we were usually struck with mind blowing MVP pattern. I did a Bing on reseearch but could not eventually find a single article which explains the users very clear of this pattern. That&#8217;s the reason of this post.<span id="more-231"></span></p>
<p>Many people might initially get confused over the MVC [ Model, view and Controller ] and the MVP [ Model, View and Presenter ] pattern. There is a slight variation, but this pattern reduces the coupling.</p>
<p><strong>Model View and Control Pattern</strong></p>
<p><img class="aligncenter size-full wp-image-232" title="mvc" src="http://vinothbabu.com/wp-content/uploads/2010/03/mvc.png" alt="mvc" width="323" height="310" /><br />
In this pattern, view dispatches the events to the controller which in turn interacts with business logic of the application and the data binding techniques which updates the view automatically when ever there is a change in our model.</p>
<p><strong>Model View and Presenter Pattern</strong></p>
<p><img class="aligncenter size-full wp-image-233" title="MVP_Model4" src="http://vinothbabu.com/wp-content/uploads/2010/03/MVP_Model4.jpg" alt="MVP_Model4" width="400" height="228" /><br />
In this pattern, controller plays a major role in handling the user input and also an interaction with business logic also responsible of the update of the view. In this way there remains now link of view with Model and it totally relies on controller for all the presentation logic. As a result, there is no dependencies in either direction between the view and the model.</p>
<p>Let us now go through an example in RajiniKanth style. The below defines the the structure of our application.</p>
<p><img class="alignright size-medium wp-image-242" title="Structure" src="http://vinothbabu.com/wp-content/uploads/2010/03/Structure-300x230.jpg" alt="Structure" width="300" height="230" />The presenter folder contains the AdditionPresenter which acts as a controller handling the input and also does the business logic.</p>
<p>The view is area where our form is displayed with an <strong>Interface </strong>IAddView (implemented by the AdditionForm.mxml file) define  the methods we need in order to update the view and get the the numbers from the view to complete our <strong>Addition </strong>operation.</p>
<p>The <strong>MVPAddition.mxml</strong> is the main file, it only defines the custom component view.</p>
<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
&lt;mx:Application xmlns:mx=&#8221;http://www.adobe.com/2006/mxml&#8221; layout=&#8221;absolute&#8221; xmlns:view=&#8221;com.vinoth.MVP.view.*&#8221;&gt;<br />
&lt;view:AdditionForm/&gt;<br />
&lt;/mx:Application&gt;</p></blockquote>
<p>The Interfact IAddView defines the methods we need in order to update the view and get the numbers from the view. It is implemented by the AdditionForm.mxml</p>
<blockquote><p>package com.vinoth.MVP.view<br />
{<br />
public interface IAddView<br />
{</p>
<p>function set Number3(value:String):void;</p>
<p>function get Number1():Number;<br />
function get Number2():Number;</p>
<p>}<br />
}</p></blockquote>
<p>The AddView custom component contains the MXML tag needed to define the  UI and it calls the addData() method in the Presenter.</p>
<blockquote><p>&lt;mx:Label text=&#8221;Addition of Two Numbers&#8221; width=&#8221;100%&#8221; fontWeight=&#8221;bold&#8221;/&gt;</p>
<p>&lt;mx:Form width=&#8221;100%&#8221; height=&#8221;50%&#8221;&gt;<br />
&lt;mx:FormItem label=&#8221;Number One&#8221; width=&#8221;100%&#8221;&gt;<br />
&lt;mx:TextInput id=&#8221;_Number1&#8243; editable=&#8221;true&#8221; width=&#8221;100%&#8221; restrict=&#8221;0-9&#8243;/&gt;<br />
&lt;/mx:FormItem&gt;<br />
&lt;mx:FormItem label=&#8221;Number Two&#8221; width=&#8221;100%&#8221;&gt;<br />
&lt;mx:TextInput id=&#8221;_Number2&#8243; editable=&#8221;true&#8221; width=&#8221;100%&#8221; restrict=&#8221;0-9&#8243;/&gt;<br />
&lt;/mx:FormItem&gt;<br />
&lt;/mx:Form&gt;<br />
&lt;mx:HBox width=&#8221;100%&#8221;&gt;<br />
&lt;mx:Label text=&#8221;Result&#8221; /&gt;<br />
&lt;mx:Label width=&#8221;100%&#8221; id=&#8221;_Number3&#8243; /&gt;<br />
&lt;mx:Button label=&#8221;Add&#8221; click=&#8221;_presenter.addData()&#8221; /&gt;<br />
&lt;/mx:HBox&gt;</p></blockquote>
<p>It also contains the methods defined in the IAddView and a private  method that is registered as a listener for the creationComplete event  that initialize the presenter.</p>
<blockquote><p>import com.vinoth.MVP.presenter.AdditonPresenter;</p>
<p>private var _presenter:AdditonPresenter;</p>
<p>private function init():void{<br />
_presenter = new AdditonPresenter(this);<br />
}</p>
<p>public function get Number1():Number {<br />
return Number(_Number1.text);<br />
}</p>
<p>public function get Number2():Number{<br />
return Number(_Number2.text);<br />
}</p>
<p>public function set Number3(value:String):void{<br />
_Number3.text = value;<br />
}</p></blockquote>
<p>The presenter is the one which handles the business logic and updates the view through the interface without accessing the View Form.The presenter stores a private member of the view and it updates accordingly.</p>
<blockquote><p>public var _view:IAddView</p></blockquote>
<p>The Patter is simple but yet very powerful because of its least dependency and the presenter relies only on the interface and not on the UI.</p>
<p><a href="http://vinothbabu.com/Flex-Samples/MVP-Addition.rar" target="_blank">Application Souce</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to Mate Framework</title>
		<link>http://vinothbabu.com/2010/03/21/introduction-to-mate-framework/</link>
		<comments>http://vinothbabu.com/2010/03/21/introduction-to-mate-framework/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 13:22:38 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Mate Framework]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=198</guid>
		<description><![CDATA[Mate is one of the best framework i have ever played with. It is completely unobtrusive. My application code has no dependencies on the framework, it is highly decoupled, reusable and testable. Mate framework is developed by AsFusion. Let me point out why i have switched over to Mate over Cairngorm 1. You can dispatch [...]]]></description>
			<content:encoded><![CDATA[<p>Mate is one of the best framework i have ever played with. It is completely unobtrusive.  My application code has no dependencies on the framework, it is highly decoupled, reusable and testable. <strong>Mate framework is developed by AsFusion.</strong><span id="more-198"></span></p>
<p>Let me point out why i have switched over to Mate over <strong>Cairngorm</strong></p>
<p>1. You can dispatch an event from anywhere in the view hierarchy and have it bubble up to the framework automatically, instead of having to have a direct line, like Cairngorms CairngormEventDispatcher or PureMVC&#8217;s notification system.</p>
<p>2. Mate is extremely light and readable, being a Flex framework which is built on the Event Map idea. This is a collation of handlers that can respond to all the incoming events and complete several actions that are appropriate for the target.</p>
<p>3. By using the Mate framework, you will need no more the Singletons. I have seen a quite a few people using Singleton pattern, when it is actually not needed in the application. Now Mate completely removes it, which actually makes development further easier.</p>
<p>4. Mate also uses a form of dependency injection (leveraging bindings) that makes it possible to connect your models to your views without either one knowing about the other. <strong>This is probably the most powerful feature of the framework.</strong></p>
<p>5. The RIA development is most certainly more interesting and flexible with Mate.</p>
<p>My View on <strong>Cairngorm</strong></p>
<p><strong>Cairngorm is a bundle of anti-patterns that lead to applications that are tightly coupled to global variables.</strong></p>
<p style="text-align: center;">Let me explain you the flow on how Mate really goes, its so easy and interesting like <strong>RajiniKanth </strong>sir Movie.<img class="aligncenter" src="http://mate.asfusion.com/assets/content/diagrams/two_way_view_injection.png" alt="" width="594" height="629" /></p>
<p>This tutorial explains how to get started with Mate. As an example, we&#8217;ll be sending two numbers to our manager class to add , receives the total value and stores it in the model for the view to show.</p>
<p>All Mate projects must have:</p>
<ol>
<li>One or more events (custom or built-in)</li>
<li>One or more Event Maps</li>
</ol>
<p><strong>Let us go step by step</strong></p>
<p>Create a new Flex project named AdditionExample. In the libs folder it creates, place the compiled framework SWC (Mate.swc). This will let you use all Mate classes and tags.Create a folder structure as displayed in the image below, i will explain you how the entire flow goes.</p>
<p style="text-align: center;"><a href="http://httpguru.com/wp-content/uploads/2009/12/structureimage.jpg"><img class="size-medium wp-image-191 aligncenter" title="structureimage" src="http://httpguru.com/wp-content/uploads/2009/12/structureimage-279x300.jpg" alt="" width="279" height="300" /></a></p>
<h2>The custom event</h2>
<p>Every Mate project is driven by events. In our example, when user enters two values and clicks on the Add button, we&#8217;ll create a new event containing that information that will be sent to the manager class to perform the addiiton. For this purpose, we create a custom event to indicate that the user wants to submit the data and retrieve the total.</p>
<p>Our event will be very simple and it will contain two property: the <strong>number1 </strong>and <strong>number2</strong>.</p>
<blockquote><p>package com.vinoth.example.events<br />
{<br />
import flash.events.Event;</p>
<p>public class AdditionEvent extends Event<br />
{<br />
public static const GET: String = &#8220;getQuoteEvent&#8221;;</p>
<p>public var number1 : Number;<br />
public var number2 : Number;</p>
<p>public function AdditionEvent(type:String, bubbles:Boolean=true,        cancelable:Boolean=false)<br />
{<br />
super(type, bubbles, cancelable);<br />
}</p>
<p>}<br />
}</p></blockquote>
<p>The code above assumes this event is contained within the package: <strong>com.vinoth.example.events</strong></p>
<p>The event also contains a constant that we will use to specify the event type. One event can specify more than one event type. We also make this event bubble up by default (the second argument of the constructor). Otherwise, we will need to remember to specify it when we instantiate it.</p>
<h2>The View</h2>
<p style="text-align: center;"><a href="http://httpguru.com/wp-content/uploads/2009/12/ui.jpg"><img class="size-medium wp-image-192 aligncenter" title="ui" src="http://httpguru.com/wp-content/uploads/2009/12/ui-300x224.jpg" alt="" width="300" height="224" /></a></p>
<p>The user interface will only need two text input and a button.</p>
<h2>Dispatching the Custom Event</h2>
<p>When the user clicks the Add button, we&#8217;ll create the AdditionEvent and dispatch it:</p>
<blockquote><p>package com.vinoth.example.events<br />
{<br />
import flash.events.Event;</p>
<p>public class AdditionEvent extends Event<br />
{<br />
public static const GET: String = &#8220;getQuoteEvent&#8221;;</p>
<p>public var number1 : Number;<br />
public var number2 : Number;</p>
<p>public function AdditionEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)<br />
{<br />
super(type, bubbles, cancelable);<br />
}</p>
<p>}<br />
}</p></blockquote>
<h2>The Event Map</h2>
<p>EventMap is a place where the we place all the handlers for the events which the application creates. There are be n number of event map. Now let me put down before you on how to create an event map.</p>
<p>We need to create a MXML file with name&#8221;MainEventMap&#8221;. This MXML component must extend from EventMap.</p>
<blockquote><p><code>&lt;?xml version=<span class="cc_value">"1.0"</span> encoding=<span class="cc_value">"utf-8"</span>?&gt;<br />
<span class="cc_normaltag">&lt;EventMap<br />
xmlns:mx=<span class="cc_value">"http://www.adobe.com/2006/mxml"</span><br />
xmlns=<span class="cc_value">"http://mate.asfusion.com/"</span>&gt;</span></code></p>
<p><span class="cc_normaltag">&lt;/EventMap&gt;</span></p></blockquote>
<p>This is how your blank event map file will look like. We&#8217;ll add the event map to our main Application file.</p>
<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
&lt;mx:Application xmlns:mx=&#8221;http://www.adobe.com/2006/mxml&#8221; layout=&#8221;absolute&#8221;<br />
xmlns:maps=&#8221;com.vinoth.example.maps.*&#8221;<br />
&gt;</p>
<p>&lt;!&#8211; Styles______________________________________________________ &#8211;&gt;</p>
<p>&lt;mx:Style source=&#8221;/assets/styles/main.css&#8221;/&gt;</p>
<p>&lt;!&#8211; Event Maps __________________________________________________ &#8211;&gt;</p>
<p>&lt;maps:MainEventMap /&gt;</p>
<p>&lt;/mx:Application&gt;</p></blockquote>
<p>Now here a question might rise, how could i know whether my event map is receiving the events being dispatched.</p>
<blockquote><p><code><span class="cc_normaltag">&lt;Debugger level=<span class="cc_value">"{Debugger.ALL}"</span> /&gt;</span></code></p></blockquote>
<p>This Tag does come handy and helps you in debugging whether you are going on the right way. Adding this to the event map looks like this.</p>
<blockquote><p><code>&lt;?xml version=<span class="cc_value">"1.0"</span> encoding=<span class="cc_value">"utf-8"</span>?&gt;<br />
<span class="cc_normaltag">&lt;EventMap<br />
xmlns:mx=<span class="cc_value">"http://www.adobe.com/2006/mxml"</span><br />
xmlns=<span class="cc_value">"http://mate.asfusion.com/"</span>&gt;</span></code></p>
<p><code><span class="cc_normaltag">&lt;Debugger level=<span class="cc_value">"{Debugger.ALL}"</span> /&gt;</span></code><br />
<code><br />
<span class="cc_normaltag">&lt;/EventMap&gt;</span></code></p></blockquote>
<h2>Listening for AdditionEvent.GET</h2>
<p>In our event map, we will listen for the addition event so that we can send the request to the manager class. We&#8217;ll add EventHandlers tag which will specify what event type we are listening too.</p>
<blockquote><p><code><span class="cc_normaltag">&lt;EventHandlers type=<span class="cc_value">"{AdditionEvent.GET}"</span> debug=<span class="cc_value">"true"</span>&gt;</span></code></p>
<p><span class="cc_normaltag">&lt;/EventHandlers&gt;</span></p></blockquote>
<p>At the top of the event map we&#8217;ll need to import the event class.</p>
<blockquote><p><code><span class="cc_normaltag">&lt;mx:Script&gt;</span><br />
&lt;![CDATA[<br />
import com.vinoth.example.events.AdditionEvent<br />
]]&gt;<br />
<span class="cc_normaltag">&lt;/mx:Script&gt;</span></code></p></blockquote>
<p>Inside this EventHandlers block, we&#8217;ll place the actions we want to perform when the event is dispatched. In our example we will call the method addValues in the AddManager Class.</p>
<blockquote><p>&lt;EventHandlers type=&#8221;{AdditionEvent.GET}&#8221; debug=&#8221;true&#8221;&gt;<br />
&lt;MethodInvoker generator=&#8221;{AddManager}&#8221;<br />
method=&#8221;addValues&#8221; arguments=&#8221;{[event.number1,event.number2]}&#8221;/&gt;<br />
&lt;/EventHandlers&gt;</p></blockquote>
<p>We are calling the method addValues on that service and sending the values coming from the event  as an argument.</p>
<h2>Creating our Business, the AddManager</h2>
<blockquote><p>package com.vinoth.example.business<br />
{<br />
import mx.controls.Alert;</p>
<p>public class AddManager<br />
{</p>
<p>[Bindable]<br />
public var totalValue:Number;</p>
<p>// &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
public function addValues(number1:Number,number2:Number):void {<br />
totalValue = number1+number2;</p>
<p>}</p>
<p>}<br />
}</p></blockquote>
<p>In this Class we are getting the two numbers as arguments from the event class which dispatches and then we are doing our calculation. Once the value is calculated, we will inject the result in our view.</p>
<blockquote><p>&lt;Injectors target=&#8221;{YourViewPanel}&#8221;&gt;<br />
&lt;PropertyInjector targetKey=&#8221;value&#8221; source=&#8221;{AddManager}&#8221; sourceKey=&#8221;totalValue&#8221; /&gt;<br />
&lt;/Injectors&gt;</p></blockquote>
<p>This makes life simple, here we get the final result. You can have a <strong>look </strong>at the application and view source is <strong>enabled</strong>.</p>
<p><a href="http://httpguru.com/Flex-Examples/mateExample/mateExample.html">Application</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/03/21/introduction-to-mate-framework/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

