<?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>simonpena.com &#187; Composite Pattern</title>
	<atom:link href="http://simonpena.com/blog/tag/composite-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://simonpena.com/blog</link>
	<description>Una mezcla heterogénea de tecnología y desvaríos</description>
	<lastBuildDate>Tue, 11 Oct 2011 19:14:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Undo, redo support using patterns</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/undo-redo-support-using-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=undo-redo-support-using-patterns</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/undo-redo-support-using-patterns/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 17:49:12 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[Command Pattern]]></category>
		<category><![CDATA[Composite Pattern]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[Template method Pattern]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=65</guid>
		<description><![CDATA[More report progress regarding undo, redo and execute using design patterns.<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/undo-redo-support-using-patterns/' addthis:title='Undo, redo support using patterns '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This Easter Week has been very productive. Although I didn't upload the screenshots as I said, I've made several interesting improvements.</p>
<ul>
<li>The delete menu item changes according to the current user interface selection. If an account is selected, it displays <em>delete account</em>. If an user is selected, it displays <em>delete user</em>, and if an account operation is selected, it displays <em>delete account operation</em>.</li>
<li>It is possible to undo/redo the actions made. The major improvement I pointed out on the previous post is almost done, so you can reverse the addition, edition and deletion of users, accounts and account operations.</li>
</ul>
<p>I've made a new hierarchy in the controller layer. I created the interface <a title="UIAction.java" href="http://code.google.com/p/pennybank/source/browse/trunk/src/main/java/com/googlecode/pennybank/swing/controller/actions/UIAction.java" target="_blank">UIAction</a>, with methods <em>execute</em>, <em>undo</em>, <em>redo</em> and <em>getName</em>. I created an abstract implementation, <a title="GenericAction.java" href="http://code.google.com/p/pennybank/source/browse/trunk/src/main/java/com/googlecode/pennybank/swing/controller/actions/GenericAction.java" target="_blank">GenericAction</a>, where I made <em>final</em> those methods -to avoid the concrete classes reimplementing them-, calling <em>doExecute</em>, <em>doUndo,</em> <em>doRedo</em> and <em>doGetName</em> (which currently doesn't make much sense), followed by a call to update (a private method which updates the information displayed on the main window).</p>
<p>The do<em>Wathever</em> methods are <em>protected</em> and <em>abstract</em>, so the actions extending GenericAction must override them. This way, each action implements its own behavior for that methods, but at the same time complying with the generic behavior without the an explicit call to <em>super</em>. It follows the <a title="Template method pattern" href="http://en.wikipedia.org/wiki/Template_method_pattern" target="_blank">Template method pattern</a>.</p>
<p>Besides, I made a <a title="CompositeAction.java" href="http://code.google.com/p/pennybank/source/browse/trunk/src/main/java/com/googlecode/pennybank/swing/controller/actions/CompositeAction.java" target="_blank">CompositeAction</a> class, following the <a title="Composite pattern" href="http://en.wikipedia.org/wiki/Composite_pattern" target="_blank">Composite pattern</a>, so several actions can be grouped into another.</p>
<p>This way, all the defined actions are commands, and the application itself has the responsibility of executing them. Whenever an action is executed, it is stored in an <em>undoable actions stack</em>, and its name is set in the edit menu, so you can read "Undo <em>actionName</em>". If you undo the action, the application takes it out from the stack, invokes its <em>undo </em>method, and adds it to the <em>redoable actions stack</em>. After that, its name will appear in the edit menu, displaying "Redo <em>actionName</em>". Again, if you redo the action, the application takes it out from that stack and invokes its <em>redo</em> method, adding it again to the <em>undoable actions stack</em>. And over and over again (as far as it is well implemented).</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/undo-redo-support-using-patterns/' addthis:title='Undo, redo support using patterns '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/undo-redo-support-using-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

