<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Cyberward.net &#187; Programming</title>
	<atom:link href="http://www.cyberward.net/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cyberward.net/blog</link>
	<description>The Drive Failures and Shutter Clicks of Christopher Ward</description>
	<lastBuildDate>Wed, 21 Jul 2010 12:00:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New Version of bgStretcher2</title>
		<link>http://www.cyberward.net/blog/2010/07/new-version-of-bgstretcher2/</link>
		<comments>http://www.cyberward.net/blog/2010/07/new-version-of-bgstretcher2/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 21:08:52 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Site News]]></category>
		<category><![CDATA[bgStretcher]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=2219</guid>
		<description><![CDATA[
Yea, we have a new version of the bgStretcher2 script. The version has been bumped from 2.0 to 2.1 and it is available for download right now. See the bgStretcher page.
What is bgStretcher?
Well, check out the official page, but simply, it is a way to have a javascript based photo gallery and slideshow that appears [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cyberward.net/blog/wp-content/uploads/bgStretcher1.png"><img class="alignnone size-full wp-image-2221" title="bgStretcher" src="http://www.cyberward.net/blog/wp-content/uploads/bgStretcher1.png" alt="" width="500" height="280" /></a></p>
<p>Yea, we have a new version of the bgStretcher2 script. The version has been bumped from 2.0 to 2.1 and it is available for download right now. See the <a href="http://www.cyberward.net/blog/code/bgstretcher2/">bgStretcher</a> page.</p>
<p>What is bgStretcher?</p>
<p>Well, check out the official page, but simply, it is a way to have a javascript based photo gallery and slideshow that appears full screen in your browser.</p>
<p>So, what&#8217;s new?</p>
<ul>
<li>First, there was a general rewrite to clean up and remove duplicate code.</li>
<li>The code was documented, so it should be easier to follow.</li>
<li>Fixed the issues people were having when clicking on albums quickly. (The slideshow would appear to stop working)</li>
<li>Moved some setup code from the html page to the bgStretcher2.js script.</li>
</ul>
<p>You should be able to just drop the new bgStretcher2.js file in. I would advise cleaning up the html page that you are using to avoid conflicts. I have moved a lot of the setup code that was here into the main script file, which makes the html look much cleaner and simpler.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/07/new-version-of-bgstretcher2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thickbox content over flash movie.</title>
		<link>http://www.cyberward.net/blog/2010/06/thickbox-content-over-flash-movie/</link>
		<comments>http://www.cyberward.net/blog/2010/06/thickbox-content-over-flash-movie/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 12:00:55 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=2149</guid>
		<description><![CDATA[
I have been using the ThickBox plugin for Wordpress on my site for a while now. When you click on a link, the images are shown in their own window with a darkened background. This worked great until I put together something on another of my photography sites here. At the top is a flash [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cyberward.net/blog/wp-content/uploads/3962443686_583f82e471_o.jpg"><img class="alignnone size-full wp-image-1520" title="St. Anthony Main" src="http://www.cyberward.net/blog/wp-content/uploads/3962443686_583f82e471_o.jpg" alt="" width="500" height="753" /></a></p>
<p>I have been using the ThickBox plugin for Wordpress on my site for a while now. When you click on a link, the images are shown in their own window with a darkened background. This worked great until I put together something on another of my photography sites <a href="http://www.christopherwardphotography.com/rgc.html">here</a>. At the top is a flash movie (yes, I know I don&#8217;t like having a flash movie, I will move on to HTML5 video when I can) and at the bottom are images. When you click on the images you get the same lightbox effect as the plugin. The only trouble was, it was pointed out to me that in IE, the video stayed on top. Oops.</p>
<p>A little searching the internet found the solution. Check out <a href="http://thewichitacomputerguy.com/blog/z-index-flash-objects-thickbox-fix">here</a> for the fix, and <a href="http://www.communitymx.com/content/article.cfm?cid=E5141">here</a> for some background on how it works and for other options you have. It is now working as expected. Thanks Steve for pointing that out to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/06/thickbox-content-over-flash-movie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPad Ready and bgStretcher2 Update</title>
		<link>http://www.cyberward.net/blog/2010/04/ipad-ready-and-bgstretcher2-update/</link>
		<comments>http://www.cyberward.net/blog/2010/04/ipad-ready-and-bgstretcher2-update/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 10:04:55 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1936</guid>
		<description><![CDATA[
I took this picture, from my iPod, of an iPad showing off my portfolio. It looks amazing. The images are actually sized perfectly for this device. But there was something weird. All the images in the night album, and the occasional other image won&#8217;t show. I get the blue image not found icon. Very weird. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cyberward.net/blog/wp-content/uploads/iPad-CWW.jpg"><img class="alignnone size-full wp-image-1937" title="iPad-CWW" src="http://www.cyberward.net/blog/wp-content/uploads/iPad-CWW.jpg" alt="" width="500" height="394" /></a></p>
<p>I took this picture, from my iPod, of an iPad showing off my <a href="http://www.cyberward.net/photography/">portfolio</a>. It looks amazing. The images are actually sized perfectly for this device. But there was something weird. All the images in the night album, and the occasional other image won&#8217;t show. I get the blue image not found icon. Very weird. I wonder if they are exported with some weird settings. Not sure how that happened.</p>
<p>I was also able to confuse the javascript by pressing on the album and next/previous buttons. I will have to take a look at that. As noted yesterday, it doesn&#8217;t work well on the iPhone either. I also checked Kelby&#8217;s site again, and it looks great too, just not on the iPhone. I am wondering how that device reports it&#8217;s screen size. I might just need to look for that browser and then display presized smaller images.</p>
<p>If you want to get a rough idea of what your site would look like on the iPhone, check out<a href="http://ipadpeek.com/"> iPad Peek</a>. It doesn&#8217;t show the broken images like the iPad did, but it will give you a sense of size and how your content will fit. Just enter your site in the address bar at the top.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/04/ipad-ready-and-bgstretcher2-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are you iPad Ready? (No flash allowed)</title>
		<link>http://www.cyberward.net/blog/2010/04/are-you-ipad-ready-no-flash-allowed/</link>
		<comments>http://www.cyberward.net/blog/2010/04/are-you-ipad-ready-no-flash-allowed/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 20:17:29 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1931</guid>
		<description><![CDATA[
Are you a photographer with a flash site? Do you know that the iPad, just like the iPhone, will not display flash content? What&#8217;s a photographer to do? Scott Kelby looked at this earlier today.
When Kelby first put up his flash portfolio, I decided to create a javascript only portfolio site to do the same [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cyberward.net/blog/wp-content/uploads/screen.jpg"><img class="alignnone size-full wp-image-1777" title="Gallery Slideshow Screen Shot" src="http://www.cyberward.net/blog/wp-content/uploads/screen.jpg" alt="" width="500" height="396" /></a></p>
<p>Are you a photographer with a flash site? Do you know that the iPad, just like the iPhone, will not display flash content? What&#8217;s a photographer to do? Scott Kelby looked at this <a href="http://www.scottkelby.com/blog/2010/archives/9503">earlier today</a>.</p>
<p>When Kelby first put up his flash portfolio, I decided to create a javascript only portfolio site to do the same thing. See my portfolio <a href="http://cyberward.net/photography">here</a>, and my post <a href="http://www.cyberward.net/blog/2010/02/full-screen-slideshow-in-javascript/">here</a>.</p>
<p>I think my code is successful, to a certain extent. It works great on the desktop, but doesn&#8217;t work as well on the iPhone, and I don&#8217;t yet know on the iPad. Need to try that out. Anyone that wants to comment about that, please do.</p>
<p>I think that my issue on the iPhone is the size of the images. I think they need to be smaller to save on bandwidth, and prevent the phone&#8217;s browser from having to scale the images so much. That may be the issue on Kelby&#8217;s site too, because his new non flash site didn&#8217;t work well on the iPhone either. His new portfolio is also a jQuery javascript site done by RC. It was too slow to be useable on my phone. I wonder if image size is the issue there to. Now his site was optimized for the iPad, not the iPhone, but I want mine to work on both. I will need to do some more testing with smaller images to see.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/04/are-you-ipad-ready-no-flash-allowed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Full Screen Slideshow in Javascript</title>
		<link>http://www.cyberward.net/blog/2010/02/full-screen-slideshow-in-javascript/</link>
		<comments>http://www.cyberward.net/blog/2010/02/full-screen-slideshow-in-javascript/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 11:30:54 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[gallery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[photo gallery]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1760</guid>
		<description><![CDATA[
So I now have a full screen slideshow in Javascript, without using flash, and I wanted to tell you how I did it, and how you can do it too. I will talk a bit about Javascript here, but you will be able to download the jQuery plugin I have without needing to know much, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cyberward.net/photography/"><img class="alignnone size-full wp-image-1777" title="Gallery Slideshow Screen Shot" src="http://www.cyberward.net/blog/wp-content/uploads/screen.jpg" alt="" width="500" height="396" /></a></p>
<p>So I now have a full screen <a href="http://cyberward.net/photography/">slideshow</a> in Javascript, without using flash, and I wanted to tell you how I did it, and how you can do it too. I will talk a bit about Javascript here, but you will be able to download the jQuery plugin I have without needing to know much, if all you want is to implement the gallery.</p>
<p>I got the idea for this plugin after looking at a flash version of Scott Kelby&#8217;s <a href="http://www.scottkelby.com/portfolio/">portfolio</a>. I just wanted to give it a try in Javascript.</p>
<p>I have created a <a href="http://www.cyberward.net/blog/code/bgstretcher2/">permanent page</a> for bgStretcher2, so it is easier to find from the side bar, so look <a href="http://www.cyberward.net/blog/code/bgstretcher2/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/02/full-screen-slideshow-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Upgrading Wordpress and MySql on 1and1</title>
		<link>http://www.cyberward.net/blog/2010/01/upgrading-wordpress-and-mysql-on-1and1/</link>
		<comments>http://www.cyberward.net/blog/2010/01/upgrading-wordpress-and-mysql-on-1and1/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 11:00:43 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Site News]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1751</guid>
		<description><![CDATA[Upgrading Wordpress is trivial now. I wanted to upgrade from 2.8 to 2.9, so I went to my dashboard and pressed the &#8220;Please update now&#8221; link. I got a few warnings, a message to make sure I did a backup first, and then finally a button I could press to do the upgrade.
Only it didn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Upgrading Wordpress is trivial now. I wanted to upgrade from 2.8 to 2.9, so I went to my dashboard and pressed the &#8220;Please update now&#8221; link. I got a few warnings, a message to make sure I did a backup first, and then finally a button I could press to do the upgrade.</p>
<p>Only it didn&#8217;t work. Wordpress 2.9 needs MySql 5.0 or higher. Crap. I had a 4.something MySql version. I went to the 1and1 control panel. No way to upgrade. No way migrate. S.O.L. When I had created my blog and the database for it, which didn&#8217;t seem like all that long ago, the default on 1and1 was to create a 4.x database. Now I was in trouble.</p>
<p>Hey, no problem I thought. I was told to do a <a href="http://faq.1and1.com/applications/mysql_administration/using_11_mysql_administration_center_phpmyadmin/7.html">backup</a> before the upgrade, and I had done that. I used the phpMyAdmin tool to do the backup, I could do a <a href="http://faq.1and1.com/applications/mysql_administration/using_11_mysql_administration_center_phpmyadmin/6.html">restore</a> to a new 5.x table the same way, right? Not exactly. I had a LOT of data in my database. 1and1 will only let you import 2meg. I had 4 times that. I needed to reduce some data.</p>
<p>I know, compression! First I tried to do the data dump with zip compression. Then I found out that the import tool didn&#8217;t support it. Sheesh. Then I tried the gzip compression. For me, this resulted in exactly the same file size. This wasn&#8217;t working at all. I just need to get rid of some data.<span id="more-1751"></span></p>
<p>First I created a new 5.x MySql table using 1and1.</p>
<p>Next, I thought that I would eliminate some cache data that I didn&#8217;t really need. I use a flickr plugin that caches a lot of data. Also, I use a twitter plugin that stores all the tweets I make there. This adds up! So I decided to delete the data. The plugins didn&#8217;t have a delete function, so using phpMyAdmin, I went to the sql tab and entered the following command:</p>
<p><code>TRUNCATE TABLE tablename;</code></p>
<p>Replace tablename with the flickr or twitter, or what ever table you need to remove all rows from. Doing it this way instead of deleteing rows is much faster.</p>
<p>Still too much data. Hmmmm&#8230; how about all the revisions. I never need to go back to the revisions I had before, so lets delete them:</p>
<p><code>DELETE FROM wp_posts WHERE post_type = "revision";</code></p>
<p>That&#8217;s working pretty well. I am down to 3.5meg. Still not near the 2meg limit though.</p>
<p>There is now a time consuming way, and an easy way. The time consuming way is if you are afraid of the command line. You can download a backup file, open it in a text editor (not word), chop it into pieces, and upload these pieces, hoping you don&#8217;t make a mistake. Or, you can do a little command line magic. Just a couple of lines.</p>
<p>First, you need to use ssh to get into your 1and1 account. From a Mac or Linux box, this isn&#8217;t too difficult. It is built right in. From a windows machine, you will need to use a program called <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/">putty</a>. I use a Mac, so I will show you the command line (Terminal) commands to use. First you need to find out the URL, userid, and password to use. If you go to the Administration panel, select &#8220;Web Space &amp; Access&#8221;, and then &#8220;Secure Shell (SSH) Access&#8221;. Here you will find the URL, user and password to use.</p>
<p>ssh userid@sxxxxxxxx.onlinehome.us (or what ever your URL is)</p>
<p>Then enter your password at the prompt.</p>
<p>Now you are in your 1and1 shell account. Now, lets save a backup of our database without the phpMyAdmin tool.</p>
<p>mysqldump -h dbxx.perfora.net -u dboxxxxxxxx -p dbxxxxxxxx &gt; backup.20100101.sql</p>
<p>Give your file a date so it is easy to remember what the backup is from.</p>
<p>The -h part is for host. The -u for user. The -p is prompt for password, then the last part is the database.</p>
<p>That command will take a few seconds depending on how big your database is. Isn&#8217;t is weird that 1and1 will let you have a 100meg database, but your can&#8217;t use any of their tools to import something that big?</p>
<p>Ok, now lets restore that data into a new 5.0 database that you created. Make sure you use the new database for this command.</p>
<p>mysql -h dbxxx.perfora.net -u dboxxxxxxxx -p dbxxxxxxxx &lt; backup.20100101.sql</p>
<p>Again, this will take a couple of seconds, and then you are done. That&#8217;s it.</p>
<p>Ok, I have to confess. I did this the hard way the first time. I thought the text after the -p was my password, instead of the database name, and I couldn&#8217;t get the command to work. This way is much easier though. Trust me.</p>
<p>Now, remember why I was doing all this? I wanted to upgrade my Wordpress install. So next I went and edited my wp-config.php file to the updated database info. I have no idea what happened, but after I edited this file, my Wordpress site became all text, and just dumped out this file to the browser. Oops. So I then deleted the file, and started the install over again. (That&#8217;s what happens if the file is missing) I entered the database info and then got a message about an existing install. Yea, yea, I know, so I exited out, and just went to the site normally, and everything was fine. Phew. Not sure what to tell you, as that was a little bit more stressful than I thought I would be.</p>
<p>Next I logged in, went the the dashboard, gleefully passed the screen telling me to backup, and upgraded to 2.9 in a couple of seconds. Piece of cake.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2010/01/upgrading-wordpress-and-mysql-on-1and1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I hate pair programming</title>
		<link>http://www.cyberward.net/blog/2009/11/i-hate-pair-programming/</link>
		<comments>http://www.cyberward.net/blog/2009/11/i-hate-pair-programming/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 14:22:05 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1624</guid>
		<description><![CDATA[¬†
First, to clarify, I don&#8217;t hate the two lovely girls in this picture, but they can be quite the pair. Second, my mother told me never to say hate, so maybe that was not a good title. How about &#8220;I detest pair programming.&#8221;
Don&#8217;t get me wrong, I don&#8217;t want to be stuck in a cave [...]]]></description>
			<content:encoded><![CDATA[<p><a class="tt-flickr tt-flickr-Medium" title="Hand in Hand" href="http://www.flickr.com/photos/v3ggi32u/2745088032/"><img class="alignnone" src="http://farm4.static.flickr.com/3049/2745088032_c9ec585cfe.jpg" alt="Hand in Hand" width="500" height="393" /></a>¬†</p>
<p>First, to clarify, I don&#8217;t hate the two lovely girls in this picture, but they can be quite the pair. Second, my mother told me never to say hate, so maybe that was not a good title. How about &#8220;I detest pair programming.&#8221;</p>
<p>Don&#8217;t get me wrong, I don&#8217;t want to be stuck in a cave for a couple of months and then crawl out bleary eyed and pronounce that I have created the perfect piece of code. I love collaboration. I go to others for help, input, guidance,¬†or to reciprocate for someone else.</p>
<p>What I¬†don&#8217;t like, is sitting¬†in a cube with two people where one person types and the¬†other looks over their shoulder all day.¬†Maybe I haven&#8217;t had the right person to do this with. Maybe¬†that is why the pair of kids in the picture works. They have their issues, but they get along well, and have similar goals most of the time.</p>
<p>When I am paired with someone, and they have the keyboard, I just feel like one of two things occurs. I am sitting there bored wondering why they just don&#8217;t let me type, or they are whipping through some material I have never worked with, and I am lost when I don&#8217;t get to &#8220;drive&#8221;. When I have the keyboard, I just find it annoying to have someone asking if the code could be done differently every 5 minutes. I don&#8217;t mind getting feedback, and code reviews are great. I have no problem working on a task, checking for ideas, implementing my approach, then getting feedback, and maybe refactoring. I just don&#8217;t like it in real time.</p>
<p>I don&#8217;t see the benefit. Luckily it only looks like we are doing this while several of us are new. It won&#8217;t last forever. Have you ever had to work for an extended time period in a pair? Did it work?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2009/11/i-hate-pair-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Space Needle Reflections</title>
		<link>http://www.cyberward.net/blog/2009/06/space-needle-reflections/</link>
		<comments>http://www.cyberward.net/blog/2009/06/space-needle-reflections/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 12:00:24 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Photo Shoot]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[Seattle]]></category>
		<category><![CDATA[Space Needle]]></category>
		<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1342</guid>
		<description><![CDATA[This post is all about the space needle, and reflections. Annie and I were out walking through the Seattle Center area when I noticed the reflections on the side of the Music Experience building. Annie had the idea to sit on the wall and get her with her reflection.

I also noticed that the reflection of [...]]]></description>
			<content:encoded><![CDATA[<p>This post is all about the space needle, and reflections. Annie and I were out walking through the Seattle Center area when I noticed the reflections on the side of the Music Experience building. Annie had the idea to sit on the wall and get her with her reflection.</p>
<p><a class="tt-flickr tt-flickr-Medium" title="The Annie Experience" href="http://www.flickr.com/photos/v3ggi32u/3649342732/"><img class="alignnone" src="http://farm3.static.flickr.com/2467/3649342732_2110cb981a.jpg" alt="The Annie Experience" width="500" height="335" /></a></p>
<p>I also noticed that the reflection of the Space Needle was coming in really clear, so I posed her to get the best reflection.</p>
<p><a class="tt-flickr tt-flickr-Medium" title="Annie and the Needle" href="http://www.flickr.com/photos/v3ggi32u/3648536953/"><img class="alignnone" src="http://farm4.static.flickr.com/3543/3648536953_fb800e9464.jpg" alt="Annie and the Needle" width="363" height="500" /></a></p>
<p>I had actually been here earlier and was looking for reflection shots, but this was later in the day and the light was a bit better. It was a better angle for the reflection.</p>
<p><span id="more-1342"></span></p>
<p>Then on our way back from Kerry park after dark, (pics to come later) I noticed that the Space Needle when all lit up reflected really well in the building.</p>
<p><a class="tt-flickr tt-flickr-Medium" title="Space Needle Reflected" href="http://www.flickr.com/photos/v3ggi32u/3648537061/"><img class="alignnone" src="http://farm3.static.flickr.com/2465/3648537061_b3d445c480.jpg" alt="Space Needle Reflected" width="373" height="500" /></a></p>
<p>I ended up taking this picture just as the train was coming through too. I planned it that way <img src='http://www.cyberward.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I also was able to squeeze in a shot of the Space Needle and it&#8217;s reflection.</p>
<p><a class="tt-flickr tt-flickr-Medium" title="Space Needle Double" href="http://www.flickr.com/photos/v3ggi32u/3649342838/"><img class="alignnone" src="http://farm3.static.flickr.com/2448/3649342838_87c63a00dc.jpg" alt="Space Needle Double" width="415" height="500" /></a></p>
<p>In a perfect world, I would have had a wider angle lens. I was scrunched down on the ground to take this, but I just don&#8217;t have quite wide enough view to get both the reflection and the needle all in the picture. If I had had the new Nikon 10-24 I bet it would have fit it all in. But then I would have used a smaller aperture, forcing a slower shutter, and I don&#8217;t think I could have hand held it. I did have my tripod with me, but we were rushing home trying to beat the rain and grab that train that just showed up on that previous pictures. We ran and caught it. Best four dollars ever. We had walked enough that day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2009/06/space-needle-reflections/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Intro to Java Observer Pattern</title>
		<link>http://www.cyberward.net/blog/2009/06/intro-to-java-observer-pattern/</link>
		<comments>http://www.cyberward.net/blog/2009/06/intro-to-java-observer-pattern/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 16:27:07 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1312</guid>
		<description><![CDATA[We are well into this whole patterns thing now, picking up lots of new lingo, and becoming better at communicating with other developers. Next on the list: the observer pattern. This is an important pattern to use to help prevent tight coupling and keep code separated into discrete objects. This pattern allows communication between two [...]]]></description>
			<content:encoded><![CDATA[<p>We are well into this whole patterns thing now, picking up lots of new lingo, and becoming better at communicating with other developers. Next on the list: the observer pattern. This is an important pattern to use to help prevent tight coupling and keep code separated into discrete objects. This pattern allows communication between two objects at particular times without each object needing to be too tightly coupled to the other.</p>
<h3>The Observer in the Physical World</h3>
<p>The idea behind the observer is like a subscription. Do you have a newspaper or magazine subscription? Have you signed up for cell phone texts from your favorite band when they have new tour dates? The act of you signing up with the newspaper, the magazine, or the band, is you saying you want a subscription, or that you want to become an observer. When the subject (newspaper, magazine, band) has something new to share (on a schedule or not) they run down their subscriber list, and notify the observers by mail, or text.</p>
<h3>The Observer in the Software World</h3>
<p>On the software side, the idea is the same. Your subject needs to provide a way to take subscriptions, hold a list of observers, and when a particular state changes, notify them. There is usually a couple of methods on the subject that takes in an interface that is used for getting on or off the subscription list. On this interface is also the &#8220;call back&#8221; method. This is the method that the subject will execute when it wants to inform the observers of state changes.<br />
<span id="more-1312"></span></p>
<h3>Do you have a picture?</h3>
<p><a href="http://www.cyberward.net/blog/wp-content/uploads/observercld.jpg"><img class="aligncenter wp-image-1313" title="observer pattern" src="http://www.cyberward.net/blog/wp-content/uploads/observercld.jpg" alt="observer pattern" width="500" height="241" /></a>¬†</p>
<p>Here is a class diagram for our first example. We have two classes and an interface. The interface, NumberObserver is the piece that connects the subject and the observer together. The NumberHolder class has methods to add and remove our observer from an internal list. The NumberDisplayer sets this object and the notify method on NumberHolder. The way it was implemented here was that NumberDisplayer implemented the interface. This is not the only way to do it. NumberDisplayer could create an inner class, or there could be an external class that implements the NumberObserver interface. It doesn&#8217;t matter. All that does matter is that NumberDisplayer passes a NumberObserver object to NumberHolder that implements the notifyNewNumber method. When NumberHolder changes it&#8217;s number, it then notifies all the observers by looping over the list it has of observers and executing the notifyNewNumber method on each one.</p>
<p>The NumberObserver is very basic. Just one method is defined here. The value that will be sent by the subject is the new number.</p>
<pre lang="java" name="NumberObserver.java" colla="+">
public interface NumberObserver {
   public void notifyNewNumber(int pNewValue);
   }
}
</pre>
<p>The NumberHolder doesn&#8217;t have much going on either. Most of the code is just to take care of the observers. Note the add, remove, and update observer methods.</p>
<pre lang="java" name="NumberHolder.java" colla="+">
public class NumberHolder {
   private int number;
   private List<NumberObserver> observerList;

   public NumberHolder() {
      number = 10;
      observerList = new ArrayList<NumberObserver>();
   }

   public void setNumber(int pNumber) {
      System.out.println("Number set to " + pNumber);
      number = pNumber;
      updateObservers();
   }

   public void addObserver(NumberObserver pObserver) {
      System.out.println("Adding observer:" + pObserver);
      observerList.add(pObserver);
   }

   public void removeObserver(NumberObserver pObserver) {
      System.out.println("Removing observer:" + pObserver);
      observerList.remove(pObserver);
   }

   private void updateObservers() {
      for (NumberObserver aObserver : observerList) {
         aObserver.notifyNewNumber(number);
      }
   }
}
</pre>
<p>And finally, the NumberDisplayer class that implements the NumberObserver interface.</p>
<pre lang="java" name="NumberDisplayer.java" colla="+">
public class NumberDisplayer implements NumberObserver {
   private String name;

   public NumberDisplayer(String pName) {
      name = pName;
   }

   public void notifyNewNumber(int pNewValue) {
      System.out.println(name + " is now displaying " + pNewValue);
   }
}
</pre>
<p>NumberHolderTest puts these three classes together. It creates several NumberDisplayer classes and adds and removes objects from the NumberHolder as well as updating the value. Run this to see the observer at work.</p>
<pre lang="Java" name="NumberHolderTest.java" colla="-">
public class NumberHolderTest {
   public static void main(String[] args) {
      NumberHolder aNumberHolder = new NumberHolder();

      // we can set the number at any time. Nothing happens because we don't
      // have any observers registered.
      aNumberHolder.setNumber(10);

      // Each NumberDisplayer will register with the NumberHolder
      NumberDisplayer aObserverAndDisplayerOne = new NumberDisplayer("One");
      aNumberHolder.addObserver(aObserverAndDisplayerOne);

      NumberDisplayer aObserverAndDisplayerTwo = new NumberDisplayer("Two");
      aNumberHolder.addObserver(aObserverAndDisplayerTwo);

      NumberDisplayer aObserverAndDisplayerThree = new NumberDisplayer("Three");
      aNumberHolder.addObserver(aObserverAndDisplayerThree);

      // Each NumberDisplayer should get notified of the change
      aNumberHolder.setNumber(20);

      // Remove two observers
      aNumberHolder.removeObserver(aObserverAndDisplayerOne);
      aNumberHolder.removeObserver(aObserverAndDisplayerTwo);

      // Now only one observer is left
      aNumberHolder.setNumber(30);
   }
}
</pre>
<p>The main idea here is that we can keep the two objects loosely coupled, with the one interface joining them together. The way this is done can vary a bit. In this particular case we have chosen to pass the new number. You could pass a different interface that exposed methods on the subject. You could just have a method that doesn&#8217;t pass anything if just need to know that something happened.</p>
<p>Let&#8217;s look at another example, this time using our Animal classes.</p>
<h3>Defining the problem</h3>
<p>We have a zoo. A zoo contains many cages, each cage contains several animals. The zoo keepers would like to have an up-to-date tally of all the animals in the zoo. Every time animals are added or removed from the cages, they want to know the totals. Let&#8217;s look at this without the observer pattern first.</p>
<h3>No Observer Pattern</h3>
<pre lang="Java" name="Cage1.java" colla="-">
public class Cage1<T extends Animal> {

   // List to hold our animals
   private List<T> animalList;

   public Cage1() {
      animalList = new ArrayList<T>();
   }

   public void insertAnimal(T pAnimal) {
      animalList.add(pAnimal);
      System.out.println("\n" + pAnimal.getName() + " " + pAnimal.getType()	+ " is added to the cage.");
   }

   public void removeAnimal(T pAnimal) {
      animalList.remove(pAnimal);
      System.out.println("\n" + pAnimal.getName() + " " + pAnimal.getType()	+ " is removed from the cage.");
   }

   public void clearCage() {
      animalList.clear();
   }

   public List<T> getAnimals() {
      return animalList;
   }
}
</pre>
<pre lang="Java" name="Zoo1.java" colla="-">
public class Zoo1 {
   private List<Cage1<? extends Animal>> cages;

   public Zoo1() {
      cages = new ArrayList<Cage1<? extends Animal>>();
   }

   public void addCage(Cage1<? extends Animal> pCage) {
      cages.add(pCage);
   }

   public void removeCage(Cage1<? extends Animal> pCage) {
      cages.remove(pCage);
   }

   public void doCageUpdated() {
      Map<String, Integer> aAnimalCounts = getAnimalCounts();
      displayAnimalCounts(aAnimalCounts);
   }

   public Map<String, Integer> getAnimalCounts() {
      Map<String, Integer> aAnimalCounts = new HashMap<String, Integer>();
      for (Cage1<? extends Animal> aCage : cages) {
         List<? extends Animal> aAnimalList = aCage.getAnimals();
         int aCount = aAnimalList.size();
         if (aCount > 0) {
            String aType = aAnimalList.get(0).getType();
            Integer aTypeCount = aAnimalCounts.get(aType);
            if (aTypeCount == null) {
               aTypeCount = new Integer(aCount);
            } else {
               aTypeCount = new Integer(aTypeCount.intValue() + aCount);
            }
            aAnimalCounts.put(aType, aTypeCount);
         }
      }
      return aAnimalCounts;
}

   public void displayAnimalCounts(Map<String, Integer> animalCounts) {

      Set<String> aAnimalSet = animalCounts.keySet();
      if (aAnimalSet == null || aAnimalSet.isEmpty()) {
         System.out.println("\nZoo is currently empty.");
      } else {
         System.out.println("\nCurrent zoo animal counts:");
      }
      for (String aType : aAnimalSet) {
         Integer aTypeCount = animalCounts.get(aType);
         System.out.println("There are " + aTypeCount.intValue() + " of type " + aType);
      }
   }
}
</pre>
<p>See Cage1.java. Pretty simple. Just a simple list, and the methods to support getting data in and out of the list. Now look at Zoo1.java. This class contains the cages in a List. It also has the methods that can look at all the cages and create the totals for each animal type.</p>
<p>There is also a method doCageUpdated that must be called everytime an animal is added to a cage. The way we have this set up so far, there is no way for the zoo to know when an animal is added to a cage. A cage knows where an animal is added, but the cage doesn&#8217;t know about the zoo. It is contained within the zoo, but there is no coupling between the cage, and the zoo. We could insert a reverence for the zoo into the cage when the cages are added to the zoo, but we try to avoid this circular coupling.</p>
<pre lang="Java" name="ZooTest1.java" colla="-">
public class ZooTest1 {
   public static void main(String[] args) {

      Zoo1 aZoo = new Zoo1();

      Cage1<Bird> aBirdCage = new Cage1<Bird>();
      aZoo.addCage(aBirdCage);

      aBirdCage.insertAnimal(new Bird());
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      aBirdCage.insertAnimal(new Bird());
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      Cage1<Mammal> aMammalCage = new Cage1<Mammal>();
      aZoo.addCage(aMammalCage);
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      Mammal aFred = new Mammal("Fred");
      aMammalCage.insertAnimal(aFred);
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      Cage1<Mammal> aDogCage = new Cage1<Mammal>();
      aZoo.addCage(aDogCage);

      aDogCage.insertAnimal(new Mammal("Rover"));
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      aDogCage.insertAnimal(new Mammal("Spot"));
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      aMammalCage.removeAnimal(aFred);
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      aBirdCage.clearCage();
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();

      aDogCage.clearCage();
      // tell zoo to check counts and update display
      aZoo.doCageUpdated();
   }
}
</pre>
<p>What we are forced to do, and must rely on, is that the developer writing the program will call the doCageUpdated() method on the zoo object every time an animal is added to a cage. See ZooTest1.java. This is not good programming practice.</p>
<h3>Implementing the Observer Pattern</h3>
<pre lang="Java" name="Cage2.java" colla="-">
public class Cage2<T extends Animal> {
   // List to hold our animals
   private List<T> animalList;

   // List to hold our observers
   private List<CageObserver> cageObservers;

   public Cage2() {
      animalList = new ArrayList<T>();
      cageObservers = new ArrayList<CageObserver>();
   }

   public void insertAnimal(T pAnimal) {
      animalList.add(pAnimal);
      System.out.println("\n" + pAnimal.getName() + " " + pAnimal.getType()	+ " is added to the cage.");
      notifyObservers();
   }

   public void removeAnimal(T pAnimal) {
      animalList.remove(pAnimal);
      System.out.println("\n" + pAnimal.getName() + " " + pAnimal.getType()	+ " is removed from the cage.");
      notifyObservers();
   }

   public void clearCage() {
      animalList.clear();
      notifyObservers();
   }

   public List<T> getAnimals() {
      return animalList;
   }

   public void addCageObserver(CageObserver pCageObserver) {
      cageObservers.add(pCageObserver);
   }

   public void removeCageObserver(CageObserver pCageObserver) {
      cageObservers.remove(pCageObserver);
   }

   private void notifyObservers() {
      for (CageObserver aCageObserver : cageObservers) {
         aCageObserver.doCageUpdated();
      }
   }
}
</pre>
<pre lang="Java" name="Zoo2.java" colla="-">
public class Zoo2 implements CageObserver {
   private List<Cage2<? extends Animal>> cages;

   public Zoo2() {
      cages = new ArrayList<Cage2<? extends Animal>>();
   }

   public void addCage(Cage2<? extends Animal> pCage) {
      pCage.addCageObserver(this);
      cages.add(pCage);
   }

   public void removeCage(Cage2<? extends Animal> pCage) {
      pCage.removeCageObserver(this);
      cages.remove(pCage);
   }

   public void doCageUpdated() {
      Map<String, Integer> aAnimalCounts = getAnimalCounts();
      displayAnimalCounts(aAnimalCounts);
   }

   public Map<String, Integer> getAnimalCounts() {
      Map<String, Integer> aAnimalCounts = new HashMap<String, Integer>();
      for (Cage2<? extends Animal> aCage : cages) {
         List<? extends Animal> aAnimalList = aCage.getAnimals();
         int aCount = aAnimalList.size();
         if (aCount > 0) {
            String aType = aAnimalList.get(0).getType();
            Integer aTypeCount = aAnimalCounts.get(aType);
            if (aTypeCount == null) {
               aTypeCount = new Integer(aCount);
            } else {
               aTypeCount = new Integer(aTypeCount.intValue() + aCount);
            }
            aAnimalCounts.put(aType, aTypeCount);
         }
      }
      return aAnimalCounts;
   }

   public void displayAnimalCounts(Map<String, Integer> animalCounts) {

      Set<String> aAnimalSet = animalCounts.keySet();
      if (aAnimalSet == null || aAnimalSet.isEmpty()) {
         System.out.println("\nZoo is currently empty.");
      } else {
         System.out.println("\nCurrent zoo animal counts:");
      }
      for (String aType : aAnimalSet) {
         Integer aTypeCount = animalCounts.get(aType);
         System.out.println("There are " + aTypeCount.intValue() + " of type " + aType);
      }
   }
}
</pre>
<p>A better way is to implement the observer pattern. Look at Cage2.java, Zoo2.java, and ZooTest2.java. Looking at Cage2.java you can see that the Cage2 class now has another list, the list of CageObserver&#8217;s. Now, when an animal is added to the cage, the cage informs anyone that is listening that the cage count has changed. In our case, we just send a message that the cage is updated, but in a different design you may have passed the Cage2 object its self, or an interface, or another object that indicated the type of event (add, remove, clear) and the number of animals that it evolved. That is all up to your design. They would all fit the observer pattern.</p>
<pre lang="Java" name="ZooTest2.java" colla="-">
public class ZooTest2 {
   public static void main(String[] args) {

      Zoo2 aZoo = new Zoo2();

      Cage2<Bird> aBirdCage = new Cage2<Bird>();
      aZoo.addCage(aBirdCage);

      aBirdCage.insertAnimal(new Bird());
      aBirdCage.insertAnimal(new Bird());

      Cage2<Mammal> aMammalCage = new Cage2<Mammal>();
      aZoo.addCage(aMammalCage);

      Mammal aFred = new Mammal("Fred");
      aMammalCage.insertAnimal(aFred);

      Cage2<Mammal> aDogCage = new Cage2<Mammal>();
      aZoo.addCage(aDogCage);

      aDogCage.insertAnimal(new Mammal("Rover"));
      aDogCage.insertAnimal(new Mammal("Spot"));

      aMammalCage.removeAnimal(aFred);

      aBirdCage.clearCage();
      aDogCage.clearCage();
   }
}
</pre>
<p>Now look at the ZooTest2 class. Much better. No longer do we have to tell the zoo to display the data, it is done automatically. This may not seem that worthwhile here, but the process for adding cages to the zoo may not be connected with the process of adding animals to anyone cage. Keeping these things separate allows for more flexibility.</p>
<h3>Swinging the Observer Pattern</h3>
<p>We have already used the observer pattern in a previous lesson. Way back when we covered classes we looked at creating a Gui and implemented a WindowListener. We didn&#8217;t discuss it back then, but we were implementing the Observer Pattern. That&#8217;s right, every time we register an action listener of some sort in AWT, or Swing, we are registering an Observer with a Subject. Go back and look at Gui1 to Gui4. These are 4 different ways of creating the Observing class. As an example, here is Gui3.java.</p>
<pre lang="Java" name="Gui3.java" colla="+">
public class Gui3 extends JFrame {
   public Gui3() {
      setTitle("Gui3");
      setSize(300, 300);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.out.println("Closeing Gui3");
            System.exit(0);
         }
      });
   }

   public static void main(String[] args) {
      JFrame aFrame = new Gui3();
      aFrame.setVisible(true);
   }
}
</pre>
<p>So keep this in mind. If you have written a GUI program, it is almost certain that you have implemented the Observer pattern. Swing depends heavily on it. That is how your visual components can react when users press buttons, move focus, and close windows. This way, you don&#8217;t have to create a loop of code that checks for a mouse click, and then determine which button the mouse was over, and then directs the program flow to specific code handling sections. You simple register with each button to be informed when it is clicked. And remember, several objects can register to listen at the same time. Because of this, Java even has a solution you can implement, called the EventListenerList. You can use this list in your classes to implement event listeners. Why reinvent it your self if it is already provided. This class supplies the underlying list, and methods to add and remove EventListeners to and from the List. It doesn&#8217;t supply a method to notify listeners in the list, but it does provide a method (getListeners()) that can take in a class type parameter so that you can ask the list to return just the listeners of a particular type. If you find you need to implement an observer event list, check this class out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2009/06/intro-to-java-observer-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intro to Java Decorator Pattern</title>
		<link>http://www.cyberward.net/blog/2009/06/intro-to-java-decorator-pattern/</link>
		<comments>http://www.cyberward.net/blog/2009/06/intro-to-java-decorator-pattern/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 19:01:36 +0000</pubDate>
		<dc:creator>Chris Ward</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[decorator]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.cyberward.net/blog/?p=1284</guid>
		<description><![CDATA[We have looked at the Factory, the Adapter, and the Singleton patterns. Now we look at the Decorator. The Decorator is similar to the Adapter, but with a subtle difference. With our ReptileAdapter, we wrapped the Reptile class to map the interface methods of Animal to the appropriate methods on Reptile. With the Decorator we [...]]]></description>
			<content:encoded><![CDATA[<p>We have looked at the Factory, the Adapter, and the Singleton patterns. Now we look at the Decorator. The Decorator is similar to the Adapter, but with a subtle difference. With our ReptileAdapter, we wrapped the Reptile class to map the interface methods of Animal to the appropriate methods on Reptile. With the Decorator we also wrap a class, but to add functionality, not to map it or replace the functionality.</p>
<h3>The Decorator in the Physical World</h3>
<p>The idea behind decorating things in the real world is pretty much how it sounds. We wrap an object with new functionality, while keeping the old functionality. How about a camera in a waterproof housing? When closed up, we have added the functionality of waterproofing, but we still provide a way to press the buttons and turn the dials so that the camera can be operated.</p>
<h3>The Decorator in the Software World</h3>
<p>On the software side, the idea is the same. It is usually used when we have one object that we like how it functions, but we want to augment the behavior for a different situation. Done correctly, we can keep inserting one object inside another adding functionality at every step.<br />
<span id="more-1284"></span></p>
<h3>What do we need to create a Decorator?</h3>
<p>We need to create a class (our Decorator) that contains the class we want to decorate. We usually get this class into the Decorator using a constructor. Our Decorator needs to extend or implement the class or interface that contains the functionality we want to Decorate. Then when one of the methods of the decorated class is called, we call it before or after adding our own functionality.</p>
<h3>Ummm, Example?</h3>
<p>Lets use our Animals again. We are going to get a little bit emotional, or at least our animals are. Have you ever met people, who no matter what you ask them, has to tell you their emotional state? We will create an animal decorator that does just that. Every time we ask them to move() or speak() they will still move and speak, but they will also tell us their emotional state.</p>
<p>So, how do we accomplish this? Just as we could have done when we started looking at the Adapter, we could just go into the code for Animal or BaseAnimal and add new code to store a feeling, and put some getters and setters on it. But, our Animal code works, and not every animal likes to tell everyone they meet what they are feeling.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code14'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128414"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p1284code14"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> EmotionDecorator <span style="color: #000000; font-weight: bold;">extends</span> BaseAnimal <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> Animal decoratedAnimal<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> EmotionDecorator<span style="color: #009900;">&#40;</span>Animal pDecoratedAnimal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal <span style="color: #339933;">=</span> pDecoratedAnimal<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> decoratedAnimal.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> move<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      showEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      showEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> showEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(Who by the way, is very &quot;</span> <span style="color: #339933;">+</span> getEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So, we start by extending BaseAnimal. This means that we get all the functionality that is in BaseAnimal, and means that other objects can just treat us like an Animal. (BaseAnimal implements the Animal interface). We have a constructor that takes in an Animal to be decorated. Not every method is implemented by BaseAnimal however, so we provide the pass thru method for getType() and decorate move() and speak().</p>
<p>The getType() method does not add anything, but we still need to provide this pass thru method, because we want the decorated Animal&#8217;s type to be returned if someone asks us.</p>
<p>The move() and speak() methods get decorated. We first call the method on our decorated Animal, then we call showEmotion(). This does the same thing for both, it prints out how the animal is feeling. It gets its emotion from an abstract method. This allows us to push down the hierarchy to some small concrete classes just the specific behavior for each emotion. This does mean a different emotion class for each emotion, but it can be used for all animals. As an exercise, when this is over, try to create a EmotionDecorator that takes in a parameter for the emotion type instead.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code15'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128415"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p1284code15"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HappyDecorator <span style="color: #000000; font-weight: bold;">extends</span> EmotionDecorator <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> HappyDecorator<span style="color: #009900;">&#40;</span>Animal pDecoratedAnimal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>pDecoratedAnimal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getEmotion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;happy&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Nothing to it. <span style="color: #006633;">It</span> just creates the constructor so our decorated animal is set up, and provides the correct emotion.
&nbsp;
<span style="color: #006633;">Lets</span> see an example of how we construct <span style="color: #000000; font-weight: bold;">this</span> decorator now.
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Java&quot;</span><span style="color: #339933;">&gt;</span>public <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   Animal aBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Animal aHappyBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HappyDecorator<span style="color: #009900;">&#40;</span>aBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   aBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aHappyBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here we create a Bird and we create a HappyDecorator that we put the bird into. If we call move() on aBird, we just get the move() method on bird. If we call the move() method on aHappyBird, we get the Bird.move() method called and the decoration.</p>
<p>We don&#8217;t have to create and hold an instance of Bird. We could just add them by doing a new inside the constructor of our decorator. We can even chain our emotions together to get one confused bird.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code16'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1284code16"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   Animal aAngryBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AngryDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Animal aHappyBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HappyDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Animal aConfusedBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AngryDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HappyDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   aAngryBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aHappyBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aConfusedBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And just so you can see that we can decorate any of our Animals:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code17'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128417"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1284code17"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   Animal aAngryBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AngryDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Animal aHappyFish <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HappyDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Fish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Animal aConfusedMammal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AngryDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HappyDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Mammal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   aAngryBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aHappyFish.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aConfusedMammal.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The chaining thing doesn&#8217;t look all that useful in this context, but it can be a great tool. Lets take a quick look at another example, the MovingDecorator. This is almost the same implementation as the EmotionDecorator, but we are only going to decorate the move() method. Lets throw a new twist in there too. Often with a Decorator, there is some sort of setup or initialization to do, so lets allow a way to init this decorator with a value, and have an abstract method to provide the values expected type.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code18'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128418"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p1284code18"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> MovingDecorator <span style="color: #000000; font-weight: bold;">extends</span> BaseAnimal <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> Animal decoratedAnimal<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> movementAmount<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> MovingDecorator<span style="color: #009900;">&#40;</span>Animal pDecoratedAnimal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal <span style="color: #339933;">=</span> pDecoratedAnimal<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> pMovementAmount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      movementAmount <span style="color: #339933;">=</span> pMovementAmount<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> decoratedAnimal.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> move<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringwriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringWriter</span></a> aWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringwriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringWriter</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      aWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Move &quot;</span> <span style="color: #339933;">+</span> getMovement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>movementAmount <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         aWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> movementAmount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         aWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> getMovementUnits<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      aWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>aWriter.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      decoratedAnimal.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMovement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMovementUnits<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We now have a more complicated move method, but the idea is much the same. Decorate the object, then add functionality.</p>
<p>Once again we need to create concrete methods for the movement types. We have MoveForward, MoveLeft, and MoveRight.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code19'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128419"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1284code19"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MoveRightDecorator <span style="color: #000000; font-weight: bold;">extends</span> MovingDecorator <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> MoveRightDecorator<span style="color: #009900;">&#40;</span>Animal pDecoratedAnimal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>pDecoratedAnimal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMovement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;right&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMovementUnits<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;degrees&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>These look almost the same as our Emotion ones, but we also have to implement a movement units method.</p>
<p>For our purposes we didn&#8217;t have to execute the init method, but we could. First is a simple class showing the using of this Decorator.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code20'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128420"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1284code20"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   Animal aMammal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Mammal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aMammal.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here shows chaining the move methods:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code21'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128421"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1284code21"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   Animal aBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MoveLeftDecorator<span style="color: #009900;">&#40;</span>
   <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MoveRightDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Fish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And finally, here we use the init method:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code22'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128422"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1284code22"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   Animal aBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bird<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   MovingDecorator aDecoBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span>aBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveRightDecorator<span style="color: #009900;">&#40;</span>aDecoBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span>aDecoBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveLeftDecorator<span style="color: #009900;">&#40;</span>aDecoBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MoveForwardDecorator<span style="color: #009900;">&#40;</span>aDecoBird<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//What happens when this is uncommented?</span>
   <span style="color: #666666; font-style: italic;">//aDecoBird = new HappyDecorator(aBird);</span>
&nbsp;
   aDecoBird.<span style="color: #006633;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   aDecoBird.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you noticed, I actually created a MovingDecorator instead of an Animal here, and that was simply so that I didn&#8217;t have to keep casting to call the init method.</p>
<h3>How about in the Java SDK?</h3>
<p>One of the best examples is the FileIO section. This is the code that allows you to read and write to the file system. You start with a basic byte or character reader, then wrap with decorators that can read by lines, or buffer for more performance.</p>
<p>You can learn about file IO in this <a href="http://java.sun.com/docs/books/tutorial/essential/io/">sun trail</a>.</p>
<p>If you remember from the Reflection Session, we read data from a properties file. We will access a file the same way here. This is shown so you can see what it looks like with no decoration.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code23'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128423"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1284code23"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IODecoratorTest1 <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span>
&nbsp;
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> aStream <span style="color: #339933;">=</span> IODecoratorTest1.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ioTest.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span> c <span style="color: #339933;">=</span> aStream.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      aStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>IODecoratorTest2 shows how we decorate the input stream. First with InputStreamReader, then with BufferedReader, then finally adding a LineNumberReader.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code24'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128424"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1284code24"><pre class="java" style="font-family:monospace;"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Areader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Reader</span></a> aReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>IODecoratorTest1.<span style="color: #000000; font-weight: bold;">class</span>
.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ioTest.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Areader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Reader</span></a> aReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>
IODecoratorTest1.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ioTest.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Areader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Reader</span></a> aReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinenumberreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LineNumberReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>IODecoratorTest2.<span style="color: #000000; font-weight: bold;">class</span>
.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ioTest.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So, as you can see, we are layering, or stacking the classes. We are decorating the read() methods in each of these classes to add behavior. We can do the same thing. Lets write a Reader decorator ourselves that can count the number of times ‚ÄòI&#8217; is in the file.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code25'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128425"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code" id="p1284code25"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ICountReader <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afilterreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FilterReader</span></a> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Areader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Reader</span></a> inReader<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> ICountReader<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Areader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Reader</span></a> pInReader<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>pInReader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      inReader <span style="color: #339933;">=</span> pInReader<span style="color: #339933;">;</span>
      count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> read<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> aByte <span style="color: #339933;">=</span> inReader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aByte <span style="color: #339933;">==</span> <span style="color: #0000ff;">'I'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         count<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> aByte<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> read<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cbuf, <span style="color: #000066; font-weight: bold;">int</span> off, <span style="color: #000066; font-weight: bold;">int</span> len<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> n <span style="color: #339933;">=</span> inReader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>cbuf, off, len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> off<span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> off <span style="color: #339933;">+</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> cbuf<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">==</span> <span style="color: #0000ff;">'I'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            count<span style="color: #339933;">++;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">return</span> n<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getICount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We are extending FilterReader instead of implementing the Reader interface so we don&#8217;t have quite so many methods to implement. This is just the same as the other decorators we have been looking at. We can insert it into a decorator chain just the same way.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1284code26'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128426"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p1284code26"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IODecoratorTest3 <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span>
      ICountReader aICountReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ICountReader<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>IODecoratorTest1.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ioTest.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinenumberreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LineNumberReader</span></a> aLineReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinenumberreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LineNumberReader</span></a><span style="color: #009900;">&#40;</span>aICountReader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">=</span> aLineReader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      aLineReader.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// We cast because we need a ICountRe to ask for the line</span>
      <span style="color: #666666; font-style: italic;">// number, and it is zero based so we add one</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;# of I's:&quot;</span> <span style="color: #339933;">+</span> aICountReader.<span style="color: #006633;">getICount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;# of lines:&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>aLineReader.<span style="color: #006633;">getLineNumber</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberward.net/blog/2009/06/intro-to-java-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Intro to Java]]></series:name>
	</item>
	</channel>
</rss>
