<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Nick Hodges</title>
    <description>A man's got to know his limitations</description>
    <link>http://www.nickhodges.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 2.7.0.0</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://www.nickhodges.com/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Nick Hodges</dc:creator>
    <dc:title>Nick Hodges</dc:title>
    <geo:lat>0.000000</geo:lat>
    <geo:long>0.000000</geo:long>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/NickHodges" /><feedburner:info uri="nickhodges" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>Flotsam and Jetsam #81</title>
      <description>&lt;ul&gt;   &lt;li&gt;Embarcadero has revealed their plans for AnyDAC – and it is called FireDAC after all.&amp;#160; &lt;a href="http://blog.marcocantu.com/blog/firedac_announced.html"&gt;Marco blogged about it&lt;/a&gt; and has all the links.&amp;#160; Looks like all XE Enterprise developers get it as an upgrade/add-on, and Pro users can buy it for $399.&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://dotnetblogengine.net/"&gt;I upgraded this site to BlogEngine.net 2.7.0.0.&lt;/a&gt;&amp;#160; It all seems to be well, but if you notice any glitches, broken links, missing graphics, or if one of your comments got lost in the shuffle, please let me know. &lt;/li&gt;    &lt;li&gt;I can cross an item off my bucket list – &lt;a href="http://jeff-duntemann.livejournal.com/330052.html"&gt;I was honored by a mention in the Odd Lots column&lt;/a&gt; of the inestimable Jeff Duntemann.&amp;#160; I’ve been a fan for years, and was thrilled to get the mention.&amp;#160; &lt;/li&gt;    &lt;li&gt;One final note:&amp;#160; There’s going to be less content here in the coming months. I’m going to put my patterns series on hold, and pretty much everything else I can spare, in order to work on my book.&amp;#160; I’ll be keeping up the F&amp;amp;J stuff – these are pretty painless – but the heavier content stuff won’t be happening as I work on my book.&amp;#160; Let me know if you want to help via &lt;a href="mailto:codingindelphi@nickhodges.com"&gt;codingindelphi@nickhodges.com&lt;/a&gt; and stay tuned for updates, etc.&amp;#160; And please feel &lt;a href="https://leanpub.com/codingindelphi"&gt;free to let me know what you think over at the LeanPub site&lt;/a&gt;.&amp;#160; &lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/0JxjzpVFXRA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/0JxjzpVFXRA/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-81.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=e5737faa-a527-4899-b72a-9ca2088b115a</guid>
      <pubDate>Fri, 01 Mar 2013 16:36:21 -0400</pubDate>
      <category>Flotsam and Jetsam</category>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=e5737faa-a527-4899-b72a-9ca2088b115a</pingback:target>
      <slash:comments>8</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=e5737faa-a527-4899-b72a-9ca2088b115a</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-81.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=e5737faa-a527-4899-b72a-9ca2088b115a</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=e5737faa-a527-4899-b72a-9ca2088b115a</feedburner:origLink></item>
    <item>
      <title>Want to Help With My Book?</title>
      <description>&lt;p&gt;As you hopefully know, I’m working on a book.&amp;#160; I’m slowly but steadily making progress.&amp;#160; I’m at the point where I need some help.&amp;#160; I’ve gotten the first few chapters in a state that I feel ready to actually let someone else read them.&lt;/p&gt;  &lt;p&gt;So if you are feeling generous, want to feel my undying gratitude, want to be mentioned in the Acknowledgements, and maybe even get a free copy, you can start technical editing!&amp;#160; If you are willing to provide some real and honest feedback on the first three chapters, knowing that things are still in a very early state, please email me at &lt;a href="mailto:codingindelphi@nickhodges.com"&gt;codingindelphi@nickhodges.com&lt;/a&gt;, and I can deliver the book to you in almost any form that you need. (Formatting, etc., is the least of my worries at this point……)&lt;/p&gt;  &lt;p&gt;So, if you are interested, please let me know.&amp;#160; All help will be gratefully received.&amp;#160; &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/qTYE3fTDOBU" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/qTYE3fTDOBU/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Want-to-Help-With-My-Book.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=65cfc71e-e867-4fb9-a757-d9b1062f141d</guid>
      <pubDate>Tue, 26 Feb 2013 22:08:02 -0400</pubDate>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=65cfc71e-e867-4fb9-a757-d9b1062f141d</pingback:target>
      <slash:comments>15</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=65cfc71e-e867-4fb9-a757-d9b1062f141d</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Want-to-Help-With-My-Book.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=65cfc71e-e867-4fb9-a757-d9b1062f141d</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=65cfc71e-e867-4fb9-a757-d9b1062f141d</feedburner:origLink></item>
    <item>
      <title>Delphi and the Factory Pattern: Factory Methods</title>
      <description>&lt;blockquote&gt;
&lt;p&gt;I&amp;rsquo;m currently reading &amp;ldquo;Head First Design Patterns&amp;rdquo;&lt;span&gt;, and am finding&lt;/span&gt; it very useful and educational.&amp;nbsp; One problem, though &amp;ndash; it&amp;rsquo;s all in Java.&amp;nbsp; So I thought that as part of the exercises, I&amp;rsquo;d translate the code to Delphi.&amp;nbsp; And also as part of my learning process, I thought it would be a good idea to post an article about each of the patterns.&amp;nbsp; I also &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;strongly encourage you to buy the book&lt;/a&gt; and read it for yourself.&lt;/p&gt;
&lt;p&gt;Let me be clear &amp;ndash; I&amp;rsquo;m not doing much more than reproducing the demos in the book.&amp;nbsp; My purpose is to make the book more approachable for Delphi developers.&amp;nbsp; &lt;span&gt;The result isn&amp;rsquo;t &lt;/span&gt;always the perfect way to do the pattern since the samples from the book are designed to be as simple as possible to illustrate the point.&amp;nbsp; I&amp;rsquo;m very aware that there are better ways to implement the patterns &lt;span&gt;than&lt;/span&gt; are shown here.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;In the first of three posts about the Factory Pattern, we looked at the &amp;ldquo;Simple Factory&amp;rdquo; pattern and how it can be used to sequester off your calls to create things.&amp;nbsp; &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;HFDP&lt;/a&gt; didn&amp;rsquo;t rank the Simple Factory as a full-fledged pattern, but gave it an &amp;ldquo;Honorable Mention&amp;rdquo;.&amp;nbsp; Either way, it&amp;rsquo;s an effective technique to remove the notion of creating things from your worker classes.&amp;nbsp; Classes should only do one thing, and Factories do the one thing of creating dependencies so your other classes can do their one thing.&lt;/p&gt;
&lt;p&gt;The reason that HFDP doesn&amp;rsquo;t make the simple factory a full fledged pattern is that they believe it isn&amp;rsquo;t robust enough to handle variations of the pattern.&amp;nbsp; It&amp;rsquo;s great if you have one pizza store.&amp;nbsp; However, if you want to open a new kind of pizza store &amp;ndash; say a store in New York that serves New York-style pizza and then one in Chicago which will server Chicago-style pizza &amp;ndash; then you are going to have some seriously ugly case or if statements.&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Branching out to New Pizza Styles&lt;/h3&gt;
&lt;p&gt;Okay, so the next step is to have different kinds of Pizza depending on where the store is (or, I suppose, the kind of clientele you want to attract, but we&amp;rsquo;ll just go with regional differences).&amp;nbsp; &lt;span&gt;First &lt;/span&gt;we&amp;rsquo;ll need a pizza store that can adapt to the different kind of pizzas that the store will need to make.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thus we&amp;rsquo;ll add an abstract&amp;nbsp; method to &lt;span style="font-family: 'Courier New';"&gt;TPizzaStore&lt;/span&gt; that will create the pizza for us in the descendent classes&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:3f556101-0342-4966-8d00-f0e407bb6c41" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;  TPizzaStore = class
  protected
    function CreatePizza(aPizzaName: string): TPizza; virtual; abstract;
  public
    function OrderPizza(aPizzaName: string): TPizza;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt; function is abstract, so descendent stores will have to implement it.&amp;nbsp; That way, each store will get to decide what kind of pizza it will make.&amp;nbsp; The &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method makes sure that all pizzas are handled in the same way, but the actual creation will be delegated to the descendent class via &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;This is where the factory method part comes in &amp;ndash; the &lt;span style="font-family: 'Courier New';"&gt;TPizzaStore&lt;/span&gt; is an abstract class that let&amp;rsquo;s its descendant decide what kind of pizza it will create.&amp;nbsp; The call to &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; becomes a &amp;ldquo;Factory Method&amp;rdquo;, determining which pizza will be created and thus how each pizza will be prepared.&lt;/p&gt;
&lt;p&gt;Next, we&amp;rsquo;ll create a number of different pizzas that will be created by the specific factory methods.&amp;nbsp; We&amp;rsquo;ll create a cheese, pepperoni, clam, and veggie pizza for both the New York and Chicago styles.&amp;nbsp; (Creating at California-style set of pizzas is left as an exercise for the reader).&amp;nbsp;&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:02fc04d7-fa9b-42ab-8aad-0aafa3ecbaa6" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;TNewYorkCheesePizza = class(TPizza)
    procedure Prepare; override;
  end;

  TNewYorkPepperoniPizza = class(TNewYorkCheesePizza)
    procedure Prepare; override;
  end;

  TNewYorkClamPizza = class(TNewYorkCheesePizza)
    procedure Prepare; override;
  end;

  TNewYorkVeggiePizza = class(TNewYorkCheesePizza)
    procedure Prepare; override;
  end;

  TChicagoCheesePizza = class(TPizza)
    procedure Prepare; override;
    procedure Cut; override;
  end;

  TChicagoPepperoniPizza = class(TChicagoCheesePizza)
    procedure Prepare; override;
  end;

  TChicagoClamPizza = class(TChicagoCheesePizza)
    procedure Prepare; override;
  end;

  TChicagoVeggiePizza = class(TChicagoCheesePizza)
    procedure Prepare; override;
  end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can &lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/5ca1685eeb46/Patterns/Factory/uFactoryMethodPizzaStore.pas?at=default"&gt;see the code on BitBucket&lt;/a&gt; for the specific implementations of the Pizza classes.&amp;nbsp; They override some of the methods to provide specific implementations for preparing the pizzas.&amp;nbsp; For instance, Chicago pizzas are cut in squares and use mozzarella cheese.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Once the pizzas are available for creation, the specific pizza stores can be created. Below is the declaration and implementation of the &lt;span style="font-family: 'Courier New';"&gt;TNewYorkPizzaStore&lt;/span&gt;.&amp;nbsp; (The &lt;span style="font-family: 'Courier New';"&gt;TChicagoPizzaStore&lt;/span&gt; looks pretty much identical, except of course that it creates Chicago-style pizzas.)&amp;nbsp; It uses the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method to decide what kind of pizza to create.&amp;nbsp; Inside &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; is the call to &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt; which will get the appropriate pizza type.&amp;nbsp;&amp;nbsp; Thus, it is a perfect example of a factory method which created things that you need.&amp;nbsp;&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7473a5c4-70eb-4901-b129-39fa76437044" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: html;"&gt;type
  TNewYorkPizzaStore = class(TPizzaStore)
  protected
    function CreatePizza(aPizzaName: string): TPizza; override;
  end; 

... 

function TNewYorkPizzaStore.CreatePizza(aPizzaName: string): TPizza;
begin
  if aPizzaName = 'cheese' then
  begin
    Result := TNewYorkCheesePizza.Create('New York Cheese Pizza');
  end else
  begin
    if aPizzaName = 'pepperoni' then
    begin
      Result := TNewYorkPepperoniPizza.Create('New York Pepperoni Pizza');
    end else
    begin
      if aPizzaName = 'clam' then
      begin
        Result := TNewYorkClamPizza.Create('New York Clam Pizza');
      end else
      begin
        if aPizzaName = 'veggie' then
        begin
          Result := TNewYorkVeggiePizza.Create('New York Veggie Pizza');
        end else
        begin
          raise Exception.Create(aPizzaName + ' is an unknown pizza');
        end;
      end;
    end;
  end;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We still have a great big ugly &lt;span style="font-family: 'Courier New';"&gt;if&lt;/span&gt; statement (There doesn&amp;rsquo;t seem to be a way to get around that, eh?) but the subclass is the class that decides what kind of pizza gets made.&amp;nbsp;&amp;nbsp; The &lt;span style="font-family: 'Courier New';"&gt;TPizzaStore&lt;/span&gt; class has no idea what kind of pizza is going to get made when the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method is called, which in turn uses the &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt; method to return the proper kind of pizza.&amp;nbsp; That&amp;rsquo;s the Factory Method pattern to a &amp;lsquo;T&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;So to sum up:&amp;nbsp; we have an abstract pizza store class that defines how pizzas are ordered and created without knowing what kind of pizza will be created.&amp;nbsp; The concrete descendants determine what kind of pizzas get created via a factory method &amp;ndash; in this case, &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we can actually create some pizzas using Factory Methods:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:827be86d-9088-46d7-a8ab-0d48c01f5463" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure MakeMethodPizzas;
var
  ChicagoPizzaStore: uFactoryMethodPizzaStore.TPizzaStore;
  NewYorkPizzaStore: uFactoryMethodPizzaStore.TPizzaStore;
  Pizza: uFactoryMethodPizzaStore.TPizza;
begin
   ChicagoPizzaStore := uFactoryMethodPizzaStore.TChicagoPizzaStore.Create;
   try
     Pizza := ChicagoPizzaStore.OrderPizza('cheese');
     Pizza.Free;
     WriteLn;
     Pizza := ChicagoPizzaStore.OrderPizza('pepperoni');
     Pizza.Free;
     WriteLn;
   finally
     ChicagoPizzaStore.Free;
   end;

   NewYorkPizzaStore := uFactoryMethodPizzaStore.TNewYorkPizzaStore.Create;
   try
     Pizza := NewYorkPizzaStore.OrderPizza('cheese');
     Pizza.Free;
     WriteLn;
     Pizza := NewYorkPizzaStore.OrderPizza('clam');
     Pizza.Free;
     WriteLn;
   finally
     NewYorkPizzaStore.Free;
   end;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The pizza store types are prefaced with their unit identifiers to keep them distinct from the pizza class from the abstract factory demo that we&amp;rsquo;ll see in the next installment of the Factory Pattern demos.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span&gt;T&lt;/span&gt;hat&amp;rsquo;s the Factory Method Pattern &amp;ndash; you create an abstract class and then descend from &lt;span&gt;it &lt;/span&gt;to implement a method for creating the right class.&lt;/p&gt;
&lt;p&gt;Next time, we&amp;rsquo;ll look at how you can create an entirely separate interface for &lt;span&gt;completely abstracting&lt;/span&gt; the notion of creating things.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/049LYc4XZ3I" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/049LYc4XZ3I/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Delphi-and-the-Factory-Pattern-Factory-Methods.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=eef5f392-3beb-458b-86c2-3b0016a19d4f</guid>
      <pubDate>Sun, 24 Feb 2013 20:16:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Patterns</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=eef5f392-3beb-458b-86c2-3b0016a19d4f</pingback:target>
      <slash:comments>13</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=eef5f392-3beb-458b-86c2-3b0016a19d4f</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Delphi-and-the-Factory-Pattern-Factory-Methods.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=eef5f392-3beb-458b-86c2-3b0016a19d4f</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=eef5f392-3beb-458b-86c2-3b0016a19d4f</feedburner:origLink></item>
    <item>
      <title>Fun Code of the Week #7</title>
      <description>&lt;p&gt;It’s all a big circle.&lt;/p&gt;  &lt;p&gt;First, I posted a fun little way to handle the cursor in this &lt;a href="http://www.nickhodges.com/post/Fun-Code-of-the-Week-3.aspx"&gt;Fun Code of the Week&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;The &lt;a href="http://www.nickhodges.com/post/Fun-Code-of-the-Week-3.aspx#id_73a7edad-3835-4500-bd69-f9b1dee83183"&gt;first comment on that post was from my friend Tapper&lt;/a&gt; who said “What, no interface?”&lt;/p&gt;  &lt;p&gt;Then, I &lt;a href="http://www.nickhodges.com/post/Even-Hints-Can-Reveal-Errors.aspx"&gt;post this about hints revealing errors&lt;/a&gt; with cursor handling as part of the demo. &lt;/p&gt;  &lt;p&gt;And from that, we get &lt;a href="http://www.nickhodges.com/post/Even-Hints-Can-Reveal-Errors.aspx#id_4c54dfad-9674-4984-bac5-95bec48ac3bc"&gt;a comment by Leif&lt;/a&gt; that I’m making Fun Code of the Week&lt;/p&gt;  &lt;p&gt;So Tapper is happy, and we all have about three ways to handle the cursor.&amp;#160; &lt;/p&gt;  &lt;p&gt;Wins for everyone!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:0729f06c-cffb-40ad-90c5-947999ad30d5" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;unit CursorHandler;

interface

uses
      Controls
    ;

type
  ICursor = interface(IInterface)
  end;

function AutoCursor(const aCursor: TCursor): ICursor;

implementation

uses
  Forms;

type
  TAutoCursor = class(TInterfacedObject, ICursor)
  private
    FCursor: TCursor;
  public
    constructor Create(const aCursor: TCursor);
    destructor Destroy; override;
  end;

function AutoCursor(const aCursor: TCursor): ICursor;
begin
  Result := TAutoCursor.Create(aCursor);
end;

constructor TAutoCursor.Create(const aCursor: TCursor);
begin
  inherited Create;
  FCursor := Screen.Cursor;
  Screen.Cursor := aCursor;
end;

destructor TAutoCursor.Destroy;
begin
  Screen.Cursor := FCursor;
  inherited;
end;

end.&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/gJUaF0Xuf3U" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/gJUaF0Xuf3U/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Fun-Code-of-the-Week-7.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=8dcb774b-fa0b-456b-abce-004170e2a371</guid>
      <pubDate>Sun, 24 Feb 2013 09:08:37 -0400</pubDate>
      <category>Fun Code</category>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=8dcb774b-fa0b-456b-abce-004170e2a371</pingback:target>
      <slash:comments>14</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=8dcb774b-fa0b-456b-abce-004170e2a371</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Fun-Code-of-the-Week-7.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=8dcb774b-fa0b-456b-abce-004170e2a371</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=8dcb774b-fa0b-456b-abce-004170e2a371</feedburner:origLink></item>
    <item>
      <title>Even Hints Can Reveal Errors</title>
      <description>&lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;I hope that you all know that when you do a full build on your Delphi project, you should never allow hints and warnings to remain in your code.&amp;#160; Warnings, in particular, are an indication that something is going on that you almost certainly don’t want to be going on.&amp;#160; But what about hints?&amp;#160; Hints can’t really be a problem, can they?&lt;/p&gt;  &lt;p&gt;Well, yeah, they can definitely point to something happening that you don’t want to happen – or in the case below, of something not happening that you &lt;strong&gt;do&lt;/strong&gt; want to happen.&amp;#160; &lt;/p&gt;  &lt;h3&gt;The Situation&lt;/h3&gt;  &lt;p&gt;Let’s say you are maintaining an older Delphi application and you run across some code that looks like this:&lt;/p&gt;  &lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:42c4293e-abd5-419e-8258-4612fff5da4d" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;begin
  Screen.Cursor := crSQLWait;
  // A bunch of database code that might make
  // the user wait
  Screen.Cursor := crDefault;
end;
&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And naturally you think “&lt;em&gt;Oh my, that will never do.&amp;#160; What happens if an exception occurs in that database code! The cursor might be stuck!&amp;#160; That needs a &lt;font face="Courier New"&gt;try…finally&lt;/font&gt; block around it!”.&lt;/em&gt;&amp;#160; And so you do the following:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b690755b-6537-4018-b839-32abf1a5aadb" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;begin
  Screen.Cursor := crSQLWait;
  try
  // A bunch of database code that might make
  // the user wait
  finally
    Screen.Cursor := crDefault;
  end;
end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And that’s better, but something is still nagging at you, and it suddenly hits you – &lt;em&gt;&amp;quot;what if the cursor wasn’t actually &lt;font face="Courier New"&gt;crDefault&lt;/font&gt; to start with?&amp;#160; Shouldn’t you restore the cursor to whatever it was before you started?”&lt;/em&gt;&amp;#160; So you are feeling a bit cocky and you proceed with a bit too much haste and you do this:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f5d0e9f5-d2e9-4006-a807-6a9034c3bce7" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;var
  OldCursor: TCursor;
begin
  OldCursor := Screen.Cursor;
  Screen.Cursor := crSQLWait;
  try
  // A bunch of database code that might make
  // the user wait
  finally
    Screen.Cursor := crDefault;
  end;
end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But like I said, you are feeling a bit cocky, and you think “&lt;em&gt;I’ll be this is all over the place&lt;/em&gt;” and you search for similar code patterns and you are right and you go off and fix this “omission” that is scattered all over your codebase.&amp;#160; Things are great, right?&amp;#160; &lt;/p&gt;

&lt;p&gt;Finally you compile and there is a &amp;lt;&lt;em&gt;shudder&lt;/em&gt;&amp;gt; hint in your code!&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;[dcc32 Hint] Unit5.pas(29): H2077 Value assigned to 'OldCursor' never used&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;and you’re like “&lt;em&gt;Whaaat?&amp;#160; It’s just a silly hint, who cares!&lt;/em&gt;”.&amp;#160; But of course, your conscience is nagging at you, and you remember that Nick told you to not allow &lt;strong&gt;&lt;em&gt;any&lt;/em&gt;&lt;/strong&gt; hints or warnings, not even stupid hints like this one, and you suddenly realize that your code has an &amp;lt;&lt;em&gt;another shudder&lt;/em&gt;&amp;gt; &lt;strong&gt;&lt;em&gt;bug&lt;/em&gt;&lt;/strong&gt; in it and it is not doing at all what you wanted and intended for it to do.&lt;/p&gt;

&lt;p&gt;And of course, you fix the subtle bug of forgetting to actually set the cursor back to its original value:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:db07873f-4276-43f0-be6d-09405a9e8aca" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;var
  OldCursor: TCursor;
begin
  OldCursor := Screen.Cursor;
  Screen.Cursor := crSQLWait;
  try
  // A bunch of database code that might make
  // the user wait
  finally
    Screen.Cursor := OldCursor;  // &amp;lt;--- forgot this part
  end;
end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And then you think “&lt;em&gt;Dang, that Nick dude was right!&amp;#160; Hints really can indicate problems&lt;/em&gt;”, and you never let another hint slide for as long as you live.&lt;/p&gt;

&lt;p&gt;And of course, I should say that I totally made this scenario up, and that I would never, ever, ever do anything like that.&amp;#160; Nope, not me.&amp;#160; Nope.&amp;#160; No way. Neeeever happened.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/iUfddxs-esQ" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/iUfddxs-esQ/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Even-Hints-Can-Reveal-Errors.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=9cfe6ea6-9c78-4aa8-93ab-ae3361157e79</guid>
      <pubDate>Sat, 23 Feb 2013 15:00:45 -0400</pubDate>
      <category>Software Development</category>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=9cfe6ea6-9c78-4aa8-93ab-ae3361157e79</pingback:target>
      <slash:comments>22</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=9cfe6ea6-9c78-4aa8-93ab-ae3361157e79</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Even-Hints-Can-Reveal-Errors.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=9cfe6ea6-9c78-4aa8-93ab-ae3361157e79</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=9cfe6ea6-9c78-4aa8-93ab-ae3361157e79</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #80</title>
      <description>&lt;ul&gt;   &lt;li&gt;Despite all my slaving over a hot keyboard, bringing you a never ending supply of intellectually stimulating and hopelessly useful content, in the entire history of this website, no one has ever donated to me via PayPal.&amp;#160; And there is a reason for that – the stupid donate button doesn’t work.&amp;#160; Well, I fixed it.&amp;#160; Or at least I think I did.&amp;#160; &lt;/li&gt;    &lt;li&gt;And now, you can donate Bitcoins to me as well!&amp;#160; Just click on that little blue button on the right that says “Donate Bitcoins”.&amp;#160; Hey, hope springs eternal! &lt;/li&gt;    &lt;li&gt;I think by now pretty much everyone in the Delphi community knows that Embarcadero has acquired AnyDAC.&amp;#160; I’m pleased by that, and I think you should be, too.&amp;#160; Anyway, it looks like they are going to rename it “&lt;a href="http://docwiki.embarcadero.com/RADStudio/XE3/en/FireDAC"&gt;FireDAC&lt;/a&gt;”.&amp;#160; I like that name.&amp;#160; And according to that link, you’ll be able to download it for XE3.&amp;#160; Just another reason to upgrade to the latest version of &lt;a href="https://store.embarcadero.com/542/cookie?affiliate=24952&amp;amp;expiry=45&amp;amp;redirectto=https%3A%2F%2Fstore.embarcadero.com%2F542%2Fpurl-rbanner"&gt;RAD Studio XE3&lt;/a&gt;. (It appears that this page on the wiki has now been removed.&amp;#160; Hmmm…..)&lt;/li&gt;    &lt;li&gt;Tim Anderson has an &lt;a href="http://www.itwriting.com/blog/7193-xamarin-vs-titanium-vs-firemonkey-should-cross-platform-tools-abstract-the-gui.html"&gt;interesting article about cross-platform development&lt;/a&gt; and the different strategies a framework can take towards producing a user interface for each platform.&amp;#160; I mention it because he discusses &lt;a href="http://www.embarcadero.com/products/firemonkey"&gt;FireMonkey&lt;/a&gt; in it.&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://wiert.me/2013/02/20/someone-should-create-a-table-with-delphi-conditional-defines-over-the-delphi-versionscompiler-platformsbitness/"&gt;Jeroen&lt;/a&gt; pointed out &lt;a href="http://www.muetze1.de/?page_id=547"&gt;this cool table that has all the Delphi compiler defines for each specific version&lt;/a&gt;.&amp;#160; Nice. &lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/8LeUmP6zUCU" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/8LeUmP6zUCU/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-80.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=d8e5b306-97ad-47fc-bcbe-ceddc1c57062</guid>
      <pubDate>Fri, 22 Feb 2013 09:04:16 -0400</pubDate>
      <category>Flotsam and Jetsam</category>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=d8e5b306-97ad-47fc-bcbe-ceddc1c57062</pingback:target>
      <slash:comments>8</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=d8e5b306-97ad-47fc-bcbe-ceddc1c57062</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-80.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=d8e5b306-97ad-47fc-bcbe-ceddc1c57062</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=d8e5b306-97ad-47fc-bcbe-ceddc1c57062</feedburner:origLink></item>
    <item>
      <title>Review of the Mobile Preview Webinar</title>
      <description>&lt;p&gt;I watched the RAD Studio Mobile Preview Webinar today.&amp;nbsp; Anders O, JT Thomas, Marco, and Sarina DuPont did a very nice job presenting the product, including some pretty cool demos.&amp;nbsp; Here are my thoughts in my ever-present bullet form.&amp;nbsp; Everything here is from notes I took during the webinar.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First, the webinar was today, and it hasn&amp;rsquo;t been put up on the web for general consumption yet.&amp;nbsp; They said that would happen soon.&amp;nbsp; I&amp;rsquo;ll make sure to point to it when it is up.&lt;/li&gt;
&lt;li&gt;Overall, this is very, very impressive.&amp;nbsp; I really thought it was cool.&amp;nbsp; It appears that Embarcadero has really made developing for iOS easy and very &amp;ldquo;Delphi-ish&amp;rdquo;.&amp;nbsp; I don&amp;rsquo;t own any Apple products, but I must confess that this tool might make me jump on the bandwagon (though I am most looking forward to the Android side of things &amp;ndash; see below&amp;hellip;.)&lt;/li&gt;
&lt;li&gt;The IDE looked great.&amp;nbsp; The designer works just like the Windows designer you know and love. They showed using FireMonkey in a very nice looking designer with a cool graphic of an iPhone.&amp;nbsp; You can even choose a &amp;ldquo;palette&amp;rdquo; of iPhone 4, iPhone 5, or an iPad and the designer adjusts and looks just like the device.&amp;nbsp; You can rotate the designer to see how things will render at any orientation.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The styling and controls looked very &amp;ldquo;iPhone-ish&amp;rdquo; to me.&amp;nbsp; I&amp;rsquo;m not an expert, but it looks like they&amp;rsquo;ve gone to a large effort to make an iOS-based Fire Monkey app look perfect on an iPhone.&amp;nbsp; I couldn&amp;rsquo;t tell exactly how native controls entered into things, but they definitely mentioned that the calendar control and the list box control were native controls.&amp;nbsp;&amp;nbsp; There were countless styles for the buttons, tabs, and other controls that made everything look like apps on the iPhone/iPad are supposed to look.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;This question got asked about 400 times, and the team was very patient in answering it each time.&amp;nbsp; &lt;strong&gt;&lt;em&gt;You need a Macintosh computer to develop and deploy on iOS. &lt;/em&gt;&lt;/strong&gt;I repeat, &lt;strong&gt;you need a Mac&lt;/strong&gt;.&amp;nbsp; There is no way around this. This isn&amp;rsquo;t a technical requirement &amp;ndash; it&amp;rsquo;s the rules according to Apple.&amp;nbsp; They only allow deployment using a Mac and xCode, and so Embarcadero has to honor that.&amp;nbsp; Further notes in this area:
&lt;ul&gt;
&lt;li&gt;This doesn&amp;rsquo;t work like Kylix did -- the RAD Studio IDE runs in Windows and Windows only.&amp;nbsp; It can run in BootCamp or Parallels on the Mac.&lt;/li&gt;
&lt;li&gt;They said that a MacMini will work fine.&amp;nbsp; I don&amp;rsquo;t know much about OS X, but they said that you&amp;rsquo;ll need at least Lion to run things.&lt;/li&gt;
&lt;li&gt;Debugging happens via a server on the mac that talks to the IDE over TCP/IP.&amp;nbsp; You debug in the IDE (on Windows) just like you normally do.&lt;/li&gt;
&lt;li&gt;Yes, you can develop in Windows on the Mac via a virtual machine, and deploy and run in the same Mac.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;You can&amp;rsquo;t run the simulator on Windows as Apple doesn&amp;rsquo;t have a simulator that runs on Windows.&lt;/li&gt;
&lt;li&gt;You can&amp;rsquo;t run OSX in a virtual machine on Windows because Apple doesn&amp;rsquo;t allow that &amp;ndash; you can only run OSX on Apple hardware.&lt;/li&gt;
&lt;li&gt;Sarina deployed both to the simulator and her actual device (though her device was emulated on the screen by an app designed to do that &amp;ndash; she did hold it up to take a picture and you can see that in the presentation&amp;hellip;&amp;hellip;.)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Database access was interesting and appeared to be very much what we&amp;rsquo;d expect it to be.
&lt;ul&gt;
&lt;li&gt;Marco demonstrated a clientdataset hooking&amp;nbsp; up to a local file.&amp;nbsp; Then he demoed the dbExpress controls talking to both &lt;a href="http://www.sqlite.org/"&gt;SQLite&lt;/a&gt; and &lt;a href="http://edn.embarcadero.com/article/42319"&gt;Interbase ToGo&lt;/a&gt; locally &amp;ndash; that is, to the DB itself residing on the device.&amp;nbsp; Or at least that was how I understood it.&lt;/li&gt;
&lt;li&gt;Having TClientDataset on mobile devices is very cool.&amp;nbsp; I presume this means that the providers will work and allow you to talk to a DataSnap server.&amp;nbsp; That could be very powerful and cool.&amp;nbsp; I can see this being a cool thing for sharing apps between phones and the web.&lt;/li&gt;
&lt;li&gt;No mention of how AnyDAC will enter into things.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A bunch of people asked about Android development, including me.
&lt;ul&gt;
&lt;li&gt;JT said that it would be available later this year.&amp;nbsp; Same with C++ for both iOS and Android.&lt;/li&gt;
&lt;li&gt;He said the goal was to have the same exact FireMonkey code compile and run on iOS and Android.&amp;nbsp; That would be very, very cool.&amp;nbsp; The question that I had was about styling &amp;ndash; but I presume there is a way to dynamically change the styling depending on the OS.&amp;nbsp; I hope so, as I don&amp;rsquo;t want an Android app looking like an iOS app, or vice-versa.&lt;/li&gt;
&lt;li&gt;There were a lot of folks asking about this &amp;ndash; I personally am really looking forward to doing Android development with Delphi.&amp;nbsp; A lot.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The compiler is Embarcadero&amp;rsquo;s &amp;ndash; no more &lt;a href="http://www.freepascal.org/"&gt;FreePascal&lt;/a&gt; involved anywhere in the tool chain.&lt;/li&gt;
&lt;li&gt;Pricing, packaging and all that kind of stuff will come later.&amp;nbsp; You can get on the beta if you are an XE3 customer.&amp;nbsp; Just another reason to buy &lt;a href="https://store.embarcadero.com/542/cookie?affiliate=24952&amp;amp;expiry=45&amp;amp;redirectto=https%3A%2F%2Fstore.embarcadero.com%2F542%2Fpurl-dbanner"&gt;Delphi XE3&lt;/a&gt;.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Did I mention that you need a Mac to develop for iOS?&lt;/li&gt;
&lt;li&gt;Further thoughts:
&lt;ul&gt;
&lt;li&gt;I&amp;rsquo;m anticipating that there will be many Delphi-built apps showing up on Apple&amp;rsquo;s AppStore in the coming year, including some cool DataSnap-based apps.&lt;/li&gt;
&lt;li&gt;The mobile development space has finally come alive, and Delphi&amp;rsquo;s right there on the leading edge.&amp;nbsp; &lt;a href="http://xamarin.com/"&gt;Xamarin has recently announced a cross-platform solution for Android and iOS&lt;/a&gt;, but they don&amp;rsquo;t provide a UI framework.&amp;nbsp; FireMonkey is right there with everything you need.&amp;nbsp; This should be a very compelling and competitive offering from Embarcadero.&amp;nbsp;&amp;nbsp;&amp;nbsp; And they appear to be doing this just at the right time as things really get hopping in the mobile tooling marketplace.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;For once, Delphi will be doing something that Microsoft will never, ever do.&amp;nbsp; Microsoft will never provide tools for developing for iOS and Android.&amp;nbsp; Or at least I can&amp;rsquo;t conceive of them ever doing it.&amp;nbsp; So this is green fields for Delphi.&amp;nbsp; Competitors are broader and smaller &amp;ndash; HTML5 and JavaScript (for which they have an excellent tool in &lt;a href="https://store.embarcadero.com/542/cookie?affiliate=24952&amp;amp;expiry=45&amp;amp;redirectto=https%3A%2F%2Fstore.embarcadero.com%2F542%2Fpurl-HTML5"&gt;HTML5 Builder&lt;/a&gt;) and Xamarin appear to be the main competitors at this point.&amp;nbsp; This is a nice change and a great opportunity for Embarcadero to get away from the head-to-head competition with Microsoft.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Again, I was very impressed overall.&amp;nbsp; The demos were pretty cool, FireMonkey looks really good on the iPhone, and the database access looks like it&amp;rsquo;s exactly what we Delphi developers would expect.&amp;nbsp; This is a cool opportunity for Delphi developers.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/my16CaQwm2s" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/my16CaQwm2s/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Review-of-the-Mobile-Preview-Webinar.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=ba05b76c-e226-483f-bacf-826f38773c55</guid>
      <pubDate>Thu, 21 Feb 2013 17:53:00 -0400</pubDate>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=ba05b76c-e226-483f-bacf-826f38773c55</pingback:target>
      <slash:comments>32</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=ba05b76c-e226-483f-bacf-826f38773c55</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Review-of-the-Mobile-Preview-Webinar.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=ba05b76c-e226-483f-bacf-826f38773c55</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=ba05b76c-e226-483f-bacf-826f38773c55</feedburner:origLink></item>
    <item>
      <title>Delphi and the Factory Pattern: Simple Factory</title>
      <description>&lt;blockquote&gt;
&lt;p&gt;I&amp;rsquo;m currently reading &amp;ldquo;Head First Design Patterns&amp;rdquo;&lt;span&gt;, and am finding&lt;/span&gt; it very useful and educational.&amp;nbsp; One problem, though &amp;ndash; it&amp;rsquo;s all in Java.&amp;nbsp; So I thought that as part of the exercises, I&amp;rsquo;d translate the code to Delphi.&amp;nbsp; And also as part of my learning process, I thought it would be a good idea to post an article about each of the patterns.&amp;nbsp; I also &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;strongly encourage you to buy the book&lt;/a&gt; and read it for yourself.&lt;/p&gt;
&lt;p&gt;Let me be clear &amp;ndash; I&amp;rsquo;m not doing much more than reproducing the demos in the book.&amp;nbsp; My purpose is to make the book more approachable for Delphi developers.&amp;nbsp; &lt;span&gt;The result isn&amp;rsquo;t &lt;/span&gt;always the perfect way to do the pattern since the samples from the book are designed to be as simple as possible to illustrate the point.&amp;nbsp; I&amp;rsquo;m very aware that there are better ways to implement the patterns &lt;span&gt;than&lt;/span&gt; are shown here.&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The chapter on the Factory Pattern in &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;HFDP&lt;/a&gt; is pretty long, and divided into three main sections.&amp;nbsp; I&amp;rsquo;ll do a post on each section: &lt;span&gt; General&lt;/span&gt; discussion and the Simple Factory, the Factory Method Pattern, and the Abstract Factory.&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;General Discussion&lt;/h3&gt;
&lt;p&gt;Regular readers of this blog will know that &lt;a href="http://www.nickhodges.com/page/Dependency-Injection-Series.aspx"&gt;I&amp;rsquo;m a huge proponent of Dependency Injection&lt;/a&gt;.&amp;nbsp; I&amp;rsquo;ve gone so far as to say that Dependency Injection should just be a way of life for a developer.&amp;nbsp; If you aren&amp;rsquo;t following the principles of Dependency Injection, you aren&amp;rsquo;t writing good, clean code.&lt;/p&gt;
&lt;p&gt;One of the basics of Dependency Injection is the notion that the creation of objects is a &amp;ldquo;&lt;a href="https://docs.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/edit"&gt;single responsibility&lt;/a&gt;&amp;rdquo; and that your class shouldn&amp;rsquo;t take on that responsibility because it, too, should only have a single responsibility.&amp;nbsp; If a class is taking care of its responsibility &lt;strong&gt;&lt;em&gt;and&lt;/em&gt;&amp;nbsp;&lt;/strong&gt;creating the things that it needs, it is doing too much. The notion of creating things is a big responsibility, and not something that should be taken lightly.&amp;nbsp; Creating things should be done by classes whose specific job it is to create things.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And one of the basic tenets of development &lt;span&gt;is &lt;span&gt;t&lt;/span&gt;o code&lt;/span&gt; against abstractions and not concrete implementations.&amp;nbsp; Well, every time you call &lt;span style="font-family: 'Courier New';"&gt;Create &lt;/span&gt;means you are coding against a concrete implementation.&amp;nbsp; That should be avoided as much as possible, right?&amp;nbsp; If you press all your &lt;span style="font-family: 'Courier New';"&gt;Create&lt;/span&gt; calls back to a factory, then you can minimize the number of calls to &lt;span style="font-family: 'Courier New';"&gt;Create&lt;/span&gt; and keep them well sequestered away.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thus we have the Factory Pattern.&amp;nbsp; The job of the factory pattern is to remove the worry and concern of creating things and make it happen pretty much automatically.&amp;nbsp; A while back I even wrote a blog post entitled &amp;ldquo;&lt;a href="http://www.nickhodges.com/post/Life-is-Too-Short-To-Call-Create.aspx"&gt;Life is Too Short to Call Create&lt;/a&gt;&amp;rdquo;.&amp;nbsp; In other words, the main job of the Factory Pattern is to hide via encapsulation the process of creating something.&amp;nbsp; Factories are the main places where your calls to &lt;span style="font-family: 'Courier New';"&gt;Create&lt;/span&gt; should happen.&lt;/p&gt;
&lt;h3&gt;The Simple Factory&lt;/h3&gt;
&lt;p&gt;HFDP uses the example of a pizza store to show how a factory might be used to create pizzas.&amp;nbsp; So following along with their example, we can declare a pizza class and some specific types of pizzas:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f3e36a78-cb09-4ec8-9b1d-0c824818e4f9" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;type

  TSimplePizza = class(TObject)
    procedure Prepare;
    procedure Bake;
    procedure Cut;
    procedure Box;
  end;

  TCheesePizza = class(TSimplePizza );&lt;br /&gt;  TPepperoniPizza = class(TSimplePizza );&lt;br /&gt;  TVeggiePizza = class(TSimplePizza );&lt;/pre&gt;
&lt;pre class="brush: delphi;"&gt;
...

{ TPizza }

procedure TSimplePizza .Bake;&lt;br /&gt;begin
  WriteLn('Bake the pizza');
end;

procedure TSimplePizza .Box;&lt;br /&gt;begin
  WriteLn('Put the pizza in a box');
end;

procedure TSimplePizza .Cut;&lt;br /&gt;begin
  WriteLn('Cut the pizza');
end;

procedure TSimplePizza .Prepare;&lt;br /&gt;begin
  WriteLn('Prepare the Pizza');
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There&amp;rsquo;s nothing special here, just some classes that represent pizzas.&amp;nbsp; The pizzas know how to be prepared.&amp;nbsp; Now, of course, we have to have a place to make our pizzas, so we declare a pizza store that knows how to take a pizza order:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:37cdbbd0-ce49-4c34-a8da-38e52645c007" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt; TSimplePizzaStore = class
    function OrderPizza(aPizzaType: string): TSimplePizza;
  end;

...

function TPizzaStore.OrderPizza(aPizzaType: string): TSimplePizza;
begin
  if aPizzaType = 'cheese' then
  begin
    Result := TCheesePizza.Create;
  end else
  begin
    if aPizzaType = 'pepperoni' then
    begin
      Result := TPepperoniPizza.Create;
    end else
    begin
      if aPizzaType = 'veggie' then
      begin
        Result := TVeggiePizza.Create;
      end else
      begin
        raise Exception.Create('I don''t know what kind of pizza that is: ' + aPizzaType);
      end;
    end;
  end;

  Result.Prepare;
  Result.Bake;
  Result.Cut;
  Result.Box;
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now that works great.&amp;nbsp; But what happens when you want to add a pizza?&amp;nbsp; You have to change the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method.&amp;nbsp; But what if you want to do something else to the pizzas?&amp;nbsp; Say you want to use the pizza class to program your point of sale, and provide pricing and descriptions?&amp;nbsp; You&amp;rsquo;d have to do another one of those big ugly if statements there as well, resulting in two places to change the code for a new pizza.&amp;nbsp; And of course, if you came up with a delivery scheme, that might be a third place.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code for creating the pizzas is something that seems likely to change and be duplicated, and so that screams out &amp;ldquo;Encapsulate me!&amp;rdquo;.&amp;nbsp; This is a good example of a class trying to do two things:&amp;nbsp; order a pizza and create a pizza.&amp;nbsp; It&amp;rsquo;s not doing one thing like all good classes that follow the Single Responsibility Principle should.&amp;nbsp; When a class tries to do two things, it has two reasons to change, and when a class has more than one reason to change, it becomes less useful, more coupled, and more complicated to change.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So instead of doing the pizza creation right in the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method, let&amp;rsquo;s create a class whose sole job&lt;span&gt; is&lt;/span&gt; to create the correct pizza on demand.&amp;nbsp; This class will be a &amp;ldquo;simple factory&amp;rdquo; class, and it will take the pizza creation code out of the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method and put it into its own method:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:e647b1ce-d814-472d-95f9-77c67b3cb0f3" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: c#;"&gt;type
  TSimplePizzaFactory = class
    function CreatePizza(aPizzaType: string): TSimplePizza;
  end;
...

{ TSimplePizzaFactory }

function TSimplePizzaFactory.CreatePizza(aPizzaType: string): TSimplePizza;
begin
  if aPizzaType = 'cheese' then
  begin
    Result := TCheesePizza.Create;
  end else
  begin
    if aPizzaType = 'pepperoni' then
    begin
      Result := TPepperoniPizza.Create;
    end else
    begin
      if aPizzaType = 'veggie' then
      begin
        Result := TVeggiePizza.Create;
      end else
      begin
        raise Exception.Create('I don''t know what kind of pizza that is: ' + aPizzaType);
      end;
    end;
  end;
end;

&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaFactory&lt;/span&gt; is pretty straight-forward:&amp;nbsp; it creates the kind of pizza you ask it for.&amp;nbsp; Simple.&lt;/p&gt;
&lt;p&gt;Now that we have a class that has the sole responsibility for creating pizzas, we can simplify the &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaStore&lt;/span&gt; class, passing it a &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaFactory&lt;/span&gt;:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1a29beca-9f74-4dd5-8cdd-99709e783f3d" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;  TSimplePizzaStore = class
  private
    FFactory: TSimplePizzaFactory;
  public
    constructor Create(aPizzaFactory: TSimplePizzaFactory);
    destructor Destroy; override;
    function OrderPizza(aPizzaType: string): TSimplePizza;
  end;

...

{ TSimplePizzaStore }

constructor TSimplePizzaStore.Create(aPizzaFactory: TSimplePizzaFactory);
begin
  inherited Create;
  FFactory := aPizzaFactory;
end;

destructor TSimplePizzaStore.Destroy;
begin
  FFactory.Free;
  inherited;
end;

function TSimplePizzaStore.OrderPizza(aPizzaType: string): TSimplePizza;
begin
  Result := FFactory.CreatePizza(aPizzaType);

  Result.Prepare;
  Result.Bake;
  Result.Cut;
  Result.Box;
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The new pizza store takes the pizza factory as a parameter on its constructor and stores it.&amp;nbsp; Then the &lt;span style="font-family: 'Courier New';"&gt;OrderPizza&lt;/span&gt; method uses that factory to create the pizza instead of doing the creation itself.&amp;nbsp; That unpleasant &lt;span style="font-family: 'Courier New';"&gt;if &lt;/span&gt;statement is neatly hidden away in the &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaFactory&lt;/span&gt; class where it can be re-used by anyone that needs it.&amp;nbsp; The pizza store just knows that the factory will create the right pizza for it.&lt;/p&gt;
&lt;p&gt;So tying it all together, the following code will create a pepperoni pizza:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2c4c7d66-b7b8-46f1-b857-0ec3421fb81e" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure MakeSimplePizza;
var
  SimpleStore: TSimplePizzaStore;
begin
  SimpleStore := TSimplePizzaStore.Create(TSimplePizzaFactory.Create);;
  try
    SimpleStore.OrderPizza('pepperoni');
  finally
    SimpleStore.Free
  end;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;h3&gt;Things to Note&lt;/h3&gt;
&lt;p&gt;Okay, here are some things to note about all of this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sure, the code for creating pizzas didn&amp;rsquo;t really change &amp;ndash; we still have that big ugly if statement.&amp;nbsp; But it is nestled away in a single class &amp;ndash; isolated, decoupled, and ready for use anywhere.&amp;nbsp; It is also a single place to add new pizza types.&lt;/li&gt;
&lt;li&gt;The code above simply calls &lt;span style="font-family: 'Courier New';"&gt;Create&lt;/span&gt; on the factory as a direct parameter in the constructor for the pizza store.&amp;nbsp; The pizza store then owns, and thus frees, the factory.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Strictly speaking, this use of a simple factory isn&amp;rsquo;t a pattern.&amp;nbsp; Or at least that is what the folks in HFDP argue.&amp;nbsp; I&amp;rsquo;m not sure that I agree with them.&amp;nbsp; This is a pretty simple yet effective pattern in my mind.&lt;/li&gt;
&lt;li&gt;Now, remember, I&amp;rsquo;m just implementing the basics of the code in the book.&amp;nbsp; There are some clear improvements that could be made to this code, and indeed the next &lt;span&gt;two&lt;/span&gt; blog articles will cover much of those improvements, but I&amp;rsquo;ll mention a couple here:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;First, both &lt;span style="font-family: 'Courier New';"&gt;TSimplePizza&lt;/span&gt; and &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaStore&lt;/span&gt; (and even &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaFactory&lt;/span&gt;) could implement interfaces, enabling us to code against interfaces.&lt;/li&gt;
&lt;li&gt;The &lt;span style="font-family: 'Courier New';"&gt;CreatePizza&lt;/span&gt; method on &lt;span style="font-family: 'Courier New';"&gt;TSimplePizzaFactory&lt;/span&gt; could have been declared as a class static method, making it so that you don&amp;rsquo;t even have to create an instance of the factory.&lt;/li&gt;
&lt;li&gt;Passing in the pizza type as a string isn&amp;rsquo;t perfect either &amp;ndash; it should be an enumerated type&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;The code for this project &lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/842eb3fc74d74aa1f24167f957e4c07f787a6bd7/Patterns/Factory/uSimpleFactory.pas?at=default"&gt;can be found on BitBucket&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;So that&amp;rsquo;s the first look at a simple factory &amp;ndash; a class &lt;span&gt;whose &lt;/span&gt;job&lt;span&gt; is&lt;/span&gt; to create things for you instead of you creating them yourself.&amp;nbsp; That&amp;rsquo;s the heart of what Dependency Injection is all about, and it&amp;rsquo;s a critical part of making sure that your code is &lt;span&gt;a&lt;/span&gt;s loosely coupled as possible.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/FaN94rtmA8o" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/FaN94rtmA8o/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Delphi-and-the-Factory-Pattern-Simple-Factory.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=c047d5eb-2c92-4c5d-bec6-7bb9a740ec01</guid>
      <pubDate>Sun, 17 Feb 2013 07:54:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Patterns</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=c047d5eb-2c92-4c5d-bec6-7bb9a740ec01</pingback:target>
      <slash:comments>13</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=c047d5eb-2c92-4c5d-bec6-7bb9a740ec01</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Delphi-and-the-Factory-Pattern-Simple-Factory.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=c047d5eb-2c92-4c5d-bec6-7bb9a740ec01</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=c047d5eb-2c92-4c5d-bec6-7bb9a740ec01</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #79</title>
      <description>&lt;ul&gt;   &lt;li&gt;Today is Delphi’s 18th Birthday.&amp;#160; I'll always wear it as a badge of honor that I was there at the launch at SD ‘95 in the Moscone Center.&amp;#160; Quite a privilege.&amp;#160; Thanks to everyone who has made Delphi such a great tool to use.&lt;/li&gt;    &lt;li&gt;I am signed up for the &lt;a href="http://forms.embarcadero.com/forms/AMUSCA1302RADMobilePreviewWebinar2-21"&gt;RAD Studio Mobile Preview Webinar&lt;/a&gt;.&amp;#160; Are you going to attend?&lt;/li&gt;    &lt;li&gt;Colin Johnsun reports that the JVCL has migrated over to use GitHub.&amp;#160; I consider this good news.&amp;#160; While I prefer Mercurial, Git is a Distributed Version Control System, and DVCS’s are vastly superior to server based systems like SVN.&amp;#160; First off, they are designed to branch and merge with ease, meaning that coordinating development and contributing changes is way, way easier (the process of “pull requests” that Colin talks about).&amp;#160; If you aren’t using a DVCS, you don’t know what you are missing.&lt;/li&gt;    &lt;li&gt;In case anyone cares, my &lt;a href="http://nickhodges.com/twitter/"&gt;entire tweet archive is now online&lt;/a&gt; here on my site.&amp;#160; &lt;a href="https://twitter.com/NickHodges/statuses/861848936?tw_i=861848936&amp;amp;tw_e=permalink&amp;amp;tw_p=archive"&gt;Here’s the tweet that started it all&lt;/a&gt;.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/4sOliQnQb_U" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/4sOliQnQb_U/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-79.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=d7468db3-5fa7-4649-bf3d-02b473b20019</guid>
      <pubDate>Thu, 14 Feb 2013 15:30:56 -0400</pubDate>
      <category>Delphi</category>
      <category>Flotsam and Jetsam</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=d7468db3-5fa7-4649-bf3d-02b473b20019</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=d7468db3-5fa7-4649-bf3d-02b473b20019</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-79.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=d7468db3-5fa7-4649-bf3d-02b473b20019</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=d7468db3-5fa7-4649-bf3d-02b473b20019</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #78</title>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.nickhodges.com/post/Flotsam-and-Jetsam-77.aspx"&gt;In my last Flotsam and Jetsam&lt;/a&gt;, I mentioned how I thought the if statement needed some consideration as a potential indication that you need to break out your code into classes.&amp;nbsp; Well, apparently, I&amp;rsquo;m not the first to think this. In fact, &lt;a href="https://twitter.com/NickHodges/status/290922858421571584"&gt;I complained about the dreaded if&amp;hellip;else&amp;hellip;if&amp;hellip;else &amp;ldquo;pattern&amp;rdquo; in a tweet a couple of weeks ago&lt;/a&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="http://www.jamestharpe.com"&gt;James Tharpe&lt;/a&gt; pointed to a few links, including this great talk by &lt;a href="http://misko.hevery.com/"&gt;Misko Hevery&lt;/a&gt; about &lt;a href="http://www.youtube.com/watch?v=4F72VULWFvc"&gt;how and why to get rid of if statements&lt;/a&gt;. (I can&amp;rsquo;t recommend that video enough.) Also, commenter J&amp;oslash;rn E. Angeltveit kindly pointed out this site:&amp;nbsp; &lt;a href="http://www.antiifcampaign.com/"&gt;The Anti-if Campaign&lt;/a&gt;.&amp;nbsp; Wonderful!&lt;/li&gt;
&lt;li&gt;Are you in Norway?&amp;nbsp; &lt;a href="http://www.meetup.com/delphi-173/events/103478012/"&gt;There&amp;rsquo;s a Delphi Meetup for you&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;RAD Studio &lt;a href="http://www.drdobbs.com/joltawards/jolt-awards-coding-tools/240147989?pgno=2"&gt;is a finalist for the Jolt Cola Awards Coding Tools section&lt;/a&gt;.&amp;nbsp; Nice.&lt;/li&gt;
&lt;li&gt;The folks over at Smart Mobile Studio &amp;ndash; the tool that compiles Delphi code in to Javascript &amp;ndash; have migrated &lt;a href="http://smartmobilestudio.com/2013/02/08/design-patterns-observer/"&gt;my Observer Pattern code into their Delphi dialect&lt;/a&gt;.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nick&amp;rsquo;s Book Update&lt;/strong&gt;:&amp;nbsp; &lt;a href="http://www.nickhodges.com/post/I%E2%80%99m-Writing-a-Book.aspx"&gt;I am writing a book&lt;/a&gt;.&amp;nbsp; &lt;a href="https://leanpub.com/codingindelphi"&gt;I am working on it.&lt;/a&gt;I have made progress. Not as much as I&amp;rsquo;d like, but I&amp;rsquo;m not standing still.&amp;nbsp; I am thinking about how to market it and sell it.&amp;nbsp; I purchased Guy Kawasaki&amp;rsquo;s latest book "&lt;a href="http://www.amazon.com/gp/product/0988523108/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0988523108&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;APE: Author, Publisher, Entrepreneur-How to Publish a Book&lt;/a&gt;"&amp;nbsp;&lt;img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0988523108" alt="" width="1" height="1" border="0" /&gt;and am reading it.&amp;nbsp; (I can&amp;rsquo;t recommend it enough &amp;ndash; it&amp;rsquo;s exactly what a self-publishing author like me needs to read.)&amp;nbsp; And so I&amp;rsquo;m getting to the stage where I need to start reviewing, editing, copy editing, and really getting rolling on producing a polished, final book.&amp;nbsp; I&amp;rsquo;m tossing around a few ideas:
&lt;ul&gt;
&lt;li&gt;Sell it &amp;ldquo;early&amp;rdquo; for a lesser price to those willing to read a crappy rough draft level of it and provide feedback.&lt;/li&gt;
&lt;li&gt;Create a project on &lt;a href="http://www.kickstarter.com"&gt;Kickstarter&lt;/a&gt;/IndieGoGo/Unbound/Pubslush to &amp;ldquo;sell&amp;rdquo; the book and get money to help fund the project&lt;/li&gt;
&lt;li&gt;Just keep working on it with a small cadre of volunteer tech editors, and possibly hire professional help to copy-edit the book.&lt;/li&gt;
&lt;/ul&gt;
In any event, I am getting it done. Thanks to all of you who have shown interest.&amp;nbsp; Your feedback, thoughts, and assistance are greatly appreciated.&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/ZjD6wUglu50" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/ZjD6wUglu50/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-78.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=f677c79d-06c3-4d8e-a69b-9cf50ecb1841</guid>
      <pubDate>Sat, 09 Feb 2013 10:39:00 -0400</pubDate>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=f677c79d-06c3-4d8e-a69b-9cf50ecb1841</pingback:target>
      <slash:comments>10</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=f677c79d-06c3-4d8e-a69b-9cf50ecb1841</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-78.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=f677c79d-06c3-4d8e-a69b-9cf50ecb1841</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=f677c79d-06c3-4d8e-a69b-9cf50ecb1841</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #77</title>
      <description>&lt;ul&gt;
&lt;li&gt;I was thinking about &lt;span style="font-family: 'Courier New';"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; statements the other day, and came up with this potential Rule of Thumb: &amp;ldquo;&lt;em&gt;Every time you write an &lt;strong&gt;&lt;span style="font-family: 'Courier New';"&gt;if&lt;/span&gt;&lt;/strong&gt; statement, you should stop and consider whether you should be creating a new class instead.&amp;rdquo;&lt;/em&gt;&amp;nbsp; Thoughts? (Now, I&amp;rsquo;ve been around long enough to see how this might go.&amp;nbsp; Before commenting, note that I &lt;strong&gt;did not say&lt;em&gt; &lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&amp;ldquo;Every time you use an &lt;span style="font-family: 'Courier New';"&gt;if&lt;/span&gt; statement, you should create a new class&amp;rdquo;&lt;/em&gt;.&amp;nbsp; I merely said that it is a point at which you should &lt;strong&gt;&lt;em&gt;consider&lt;/em&gt;&lt;/strong&gt; it&amp;hellip;)&lt;/li&gt;
&lt;li&gt;I signed the &lt;a href="http://bit.ly/Xk8GxX"&gt;Manifesto for Software Craftsmanship&lt;/a&gt;. I think you should, too.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.embarcadero.com/davidi"&gt;David I&lt;/a&gt; is quoted extensively in &lt;a href="http://www.mendix.com/think-tank/the-history-of-visual-development-environments-imagine-theres-no-ides-its-difficult-if-you-try/"&gt;this interesting article about the history of the IDE&lt;/a&gt;.&amp;nbsp; There&amp;rsquo;s a cool graphic of an old Visual Developer magazine with about three Delphi articles mentioned, including the cover article about the VCL. Cool.&lt;/li&gt;
&lt;li&gt;Embarcadero has made an interesting move &amp;ndash; &lt;a href="http://www.embarcadero.com/press-releases/embarcadero-technologies-acquires-high-performance-enterprise-database-connectivity-technology-for-developers"&gt;they have bought AnyDAC from DA-Soft&lt;/a&gt;.&amp;nbsp; They&amp;rsquo;ve also hired Dmitry Arefiev to continue maintaining and developing the framework.&amp;nbsp;&amp;nbsp; Marco has more details on his blog.&amp;nbsp; &lt;a href="http://forms.embarcadero.com/forms/AnyDAC"&gt;You can even sign up to get more information as news breaks&lt;/a&gt; (I guess&amp;hellip;). This is actually a huge move &amp;ndash; there&amp;rsquo;s quite a bit of overlap between AnyDAC and dbExpress, so it will be interesting to see how this plays out.&amp;nbsp; I myself don&amp;rsquo;t know anything about AnyDAC, but you can get &lt;a href="http://www.da-soft.com/anydac/download.html"&gt;a trial version from their website&lt;/a&gt;. I for one will be curious to see how this all plays out.&amp;nbsp; Database access is obviously a core function of Delphi, and this appears to be a radical shift in that area.&amp;nbsp; I presume details on SKU&amp;rsquo;s, pricing, etc. will be forthcoming.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Thought I&amp;rsquo;d post here three tweets I made recently:
&lt;ul&gt;
&lt;li&gt;"&lt;a href="https://twitter.com/NickHodges/status/299186042429382659"&gt;If you assign a stringlist to a combobox items property using ':=', you still have to free the stringlist.&lt;/a&gt;&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;&lt;a href="https://twitter.com/NickHodges/status/298898132442431488"&gt;The *instant* you create something, *immediately* write the code to free it. Do nothing until that happens. *Nothing*&lt;/a&gt;.&amp;rdquo;&lt;/li&gt;
&lt;li&gt;And my favorite:&amp;nbsp; &amp;ldquo;&lt;a href="https://twitter.com/NickHodges/status/298913105784754177"&gt;Ahhhhhhhhhhhhhhhhhhhhhhhhhhhrrrrrrrrrrrrrggggggggggggghhhhhhhhh!!!!!!!!!!! @leebehrens: @BarryStaes I just use FreeAndNil() ;)&lt;/a&gt;&amp;nbsp; &amp;ldquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/6vslF_AyEx4" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/6vslF_AyEx4/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-77.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=fec42dca-5406-4c49-96af-4a8cd010ed16</guid>
      <pubDate>Wed, 06 Feb 2013 16:43:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Flotsam and Jetsam</category>
      <category>Tweet Expansion</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=fec42dca-5406-4c49-96af-4a8cd010ed16</pingback:target>
      <slash:comments>18</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=fec42dca-5406-4c49-96af-4a8cd010ed16</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-77.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=fec42dca-5406-4c49-96af-4a8cd010ed16</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=fec42dca-5406-4c49-96af-4a8cd010ed16</feedburner:origLink></item>
    <item>
      <title>Delphi and the Decorator Pattern</title>
      <description>&lt;blockquote&gt;
&lt;p&gt;I&amp;rsquo;m currently reading &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;Head First Design Patterns&lt;/a&gt;, and am finding it very useful and educational.&amp;nbsp; One problem, though &amp;ndash; it&amp;rsquo;s all in Java.&amp;nbsp; So I thought that as part of the exercises, I&amp;rsquo;d translate the code to Delphi.&amp;nbsp; And also as part of my learning process, I thought it would be a good idea to post an article about each of the patterns.&amp;nbsp; I also encourage you to &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;buy the book and read it for yourself.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let me be clear &amp;ndash; I&amp;rsquo;m not doing any more than reproducing the demos in the book.&amp;nbsp; I&amp;rsquo;m very aware that there are better ways to implement the patterns than are shown here.&amp;nbsp; The idea is to learn the pattern, not optimize the code.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Continuing on through HFDP, the next pattern we&amp;rsquo;ll cover is the Decorator Pattern.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One of the themes of the book is to make your code as flexible as possible at runtime, and to design your code to prefer runtime flexibility over compile time flexibility.&amp;nbsp; Anytime you determine something at compile-time, you lock that feature in to your code.&amp;nbsp; It can&amp;rsquo;t be changed at runtime.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Inheritance is a pillar of OOP, but it is a compile-time construct.&amp;nbsp; Inheritance is powerful, but inflexible at runtime.&amp;nbsp; You have the classes you have, and they can&amp;rsquo;t be altered or changed once you set them in the stone that is compiled code. If you are trying to come up with differing combinations to decorate each class, you can end up having a huge number of descendent classes which can become difficult to manage and maintain.&amp;nbsp; Changes to one of the features means any number of descendants might need to change as well.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The decorator pattern is a way of circumventing these limitations, and providing the power of inheritance while at the same time providing run-time flexibility.&amp;nbsp; It actually uses some simple inheritance to &amp;ldquo;wrap up&amp;rdquo; a given class and provide additional functionality without having to change the original class.&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Here is the formal definition from Wikipedia:&amp;nbsp; &lt;strong&gt;The Decorator Pattern attaches additional responsibilities to an object dynamically.&amp;nbsp; Decorators provide a flexible alternative to sub-classing for extending functionality.&amp;nbsp; &lt;/strong&gt;&lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;You can read more about the pattern on Wikipedia&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It&amp;rsquo;s actually pretty straight-forward.&amp;nbsp; Here&amp;rsquo;s what you do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First, you start with the notion of a class that needs to be decorated.&amp;nbsp; It becomes the &amp;ldquo;base&amp;rdquo; class.&amp;nbsp; The example in HFDP is that of a coffee drink.&amp;nbsp; They coffee itself &amp;ndash; a beverage &amp;ndash; is the base class which can be any number of different coffee types. Each will be decorated with various flavorings, etc., such as milk, mocha, and cream whip.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Then you create a single descendant that will be the base decorator class.&amp;nbsp; That class will take as a property an instance of the original base class.&amp;nbsp; The base decorator should have a constructor that takes the original base class as a parameter.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Next, you create concrete instances of decorators that override the necessary methods and properties of the base decorator class.&lt;/li&gt;
&lt;li&gt;From there, you can start with the base class, and then chain together as many decorators as you want.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Okay, let&amp;rsquo;s take a look at that in code.&amp;nbsp; Here is the base class, &lt;span style="font-family: 'Courier New';"&gt;TBeverage&lt;/span&gt;:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:702ea236-e2cc-4849-a306-02a4c4c9bdfc" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;type
  TBeverage = class
  private
    FDescription: string;
  protected
    function GetDescription: string;  virtual;
    procedure SetDescription(const Value: string);
  public
    function Cost: Double; virtual; abstract;
    property Description: string read GetDescription write SetDescription;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The base class is abstract.&amp;nbsp; It has two properties, with the &lt;span style="font-family: 'Courier New';"&gt;Cost&lt;/span&gt; property being read-only and having an abstract, virtual getter.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New';"&gt;TBeverage&lt;/span&gt; is the base class for the beginning of the wrapping process.&amp;nbsp; We create descendants that will form the base class described above.&amp;nbsp; These classes override the &lt;span style="font-family: 'Courier New';"&gt;Cost&lt;/span&gt; method, providing their own prices.&amp;nbsp; They will be the starting point for decorating.&amp;nbsp; In our case, we&amp;rsquo;ll create specific base coffee types that can be wrapped:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:946c2ad0-ae64-4b3c-8985-e4bf088305f9" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt; TEspresso = class(TBeverage)
    constructor Create;
    function Cost: Double; override;
  end;

  THouseBlend = class(TBeverage)
    constructor Create;
    function Cost: Double; override;
  end;

  TDarkRoast = class(TBeverage)
    constructor Create;
    function Cost: Double; override;
  end;

  TDecaf = class(TBeverage)
    constructor Create;
    function Cost: Double; override;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The interesting part comes in the base decorator class:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7b9bacab-cd92-4675-ad8a-0398a2b7035b" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;  TBeverageDecorator = class(TBeverage)
  private
    FBeverage: TBeverage;
  public
    constructor Create(aBeverage: TBeverage);
    destructor Destroy; override;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New';"&gt;TBeverageDecorator&lt;/span&gt; does three things.&amp;nbsp; One, it descends from &lt;span style="font-family: 'Courier New';"&gt;TBeverage. &lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Two&lt;/span&gt;, it gets another instance of &lt;span style="font-family: 'Courier New';"&gt;TBeverage&lt;/span&gt; from its constructor in order to &amp;ldquo;wrap&amp;rdquo; itself around the starting base class.&amp;nbsp; And three, the destructor ensures that the internally stored instance gets freed properly when that time comes.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Since &lt;span style="font-family: 'Courier New';"&gt;TBeverageDecorator&lt;/span&gt; is itself a &lt;span style="font-family: 'Courier New';"&gt;TBeverage&lt;/span&gt;, you can continue wrapping the previous result in a new one, decorating the decorator as it were.&amp;nbsp; In addition, it will have the exact same interface as the base beverage class, so it can act like a beverage because, well, it is one.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As for classes that do the decorating, they will descend from &lt;span style="font-family: 'Courier New';"&gt;TBeverageDecorator&lt;/span&gt;:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2013da3c-ff3a-4274-9b4f-d30ee6f1aca0" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;  TMocha = class(TBeverageDecorator)
  protected
    function GetDescription: string; override;
  public
    function Cost: Double; override;
  end;

  TSoy = class(TBeverageDecorator)
  protected
    function GetDescription: string; override;
  public
    function Cost: Double; override;
  end;

  TWhip = class(TBeverageDecorator)
  protected
    function GetDescription: string; override;
  public
    function Cost: Double; override;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Each of these classes does two things.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, they override the getter for the &lt;span style="font-family: 'Courier New';"&gt;Description&lt;/span&gt; property.&amp;nbsp; They do it in an interesting way.&amp;nbsp; Since they are decorating the base Beverage, they assume that they are &amp;ldquo;additive&amp;rdquo;, that is that they will be adding on to the base description.&amp;nbsp; The &lt;span style="font-family: 'Courier New';"&gt;TMocha.GetDescription&lt;/span&gt; method looks like this:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a042905b-db40-45c6-a037-3f41935d400a" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;function TMocha.GetDescription: string;
begin
  Result := FBeverage.GetDescription + ', Mocha';
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This method grabs the description from the class that it is wrapping and adds on a comma and its own description.&lt;/p&gt;
&lt;p&gt;Second, the &lt;span style="font-family: 'Courier New';"&gt;Cost&lt;/span&gt; method is overridden to add the cost to the price of the class that it is decorating.&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d35fdef7-2a78-42b2-92b9-7464b99a7bb5" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;function TMocha.Cost: Double;
begin
  Result := 0.20 + FBeverage.Cost;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So, now we have a bunch of types of coffee &amp;ndash; the base classes --and a bunch of flavors to wrap around those classes and themselves.&amp;nbsp; Let&amp;rsquo;s put it all together:&lt;/p&gt;
&lt;p&gt;First, we&amp;rsquo;ll create a routine to write out the coffee type to the console:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b03d4a25-b53f-4d30-b6af-16a304a45d11" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure OutputBeverage(aBeverage: TBeverage);
begin
  WriteLn('A ', aBeverage.Description, ' costs ', '$', Format('%2f', [aBeverage.Cost]));
  WriteLn;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then, we&amp;rsquo;ll create an Espresso and wrap it with Double Mocha and Whip:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9423c1a0-e102-44d7-a2b1-ff732ca39445" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure OrderCoffee;
var
  Beverage: TBeverage;
begin
  Beverage := TEspresso.Create;
  try
    Beverage := TMocha.Create(Beverage);
    Beverage := TMocha.Create(Beverage);
    Beverage := TWhip.Create(Beverage);
    OutputBeverage(Beverage);
  finally
    Beverage.Free;
  end;
end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will build the description and calculate the correct price, and result in:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img src="http://www.nickhodges.com/image.axd?picture=2013%2f2%2fdecorator.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Another way that you can create a coffee is by simply nesting each decorator in the constructor of the previous one.&amp;nbsp; This method actually provides a coding representation of how each decorator wraps up the previous one:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2ade2977-f140-413c-a9ad-b4da47da9d59" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;  // Alternate way to call the same thing as above....
  Beverage3 := TWhip.Create(TMocha.Create(TMocha.Create(TDarkRoast.Create)));
  try
    OutputBeverage(Beverage3);
  finally
    Beverage3.Free;
  end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/917b8f352751e410fde5dfddd4f3c1987bf26dad/Patterns/Decorator?at=default"&gt;The complete implementation of our little coffee shop application can be found on BitBucket&lt;/a&gt;.&amp;nbsp; That code should be a faithful representation of the final project in the book.&amp;nbsp; And I have mentioned that I strongly recommend that you buy &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;the book, right?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So, to summarize:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The whole thing starts with base class that will be decorated.&lt;/li&gt;
&lt;li&gt;The decorator classes both descend from and maintains a reference to that base class. This means that they will have the same interface, yet the reference will allow composition instead of inheritance.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;One or more decorators can be wrapped around the base class and each other to produce a single entity.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The Decorator can augment the behavior of the class it is wrapping.&amp;nbsp; We see this with the &lt;span style="font-family: 'Courier New';"&gt;Cost&lt;/span&gt; and &lt;span style="font-family: 'Courier New';"&gt;Description&lt;/span&gt; properties.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;You can dynamically decorate objects at anytime, enabling you to create any combination of decorators without having to create a descendent class for each.&lt;/li&gt;
&lt;li&gt;Decorators allow you to extend behavior of existing classes without needing to modify existing code.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are some problems here &amp;ndash; you can end up with a lot of little classes to manage; that might bother some people.&amp;nbsp; Use of decorators can lead to complex code and implementations &amp;ndash; use them with care.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, the basic idea is to simplify and add flexibility to your code over a complex inheritance model. In addition, it makes it easier to build a single instance at run-time of complicated combinations of features or items.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Next up:&amp;nbsp; The Factory Pattern&lt;/em&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/eWfsmEyIUOo" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/eWfsmEyIUOo/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Delphi-and-the-Decorator-Pattern.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=5bbe3fd8-b0be-4cc2-bd0d-4667db16bbfa</guid>
      <pubDate>Mon, 04 Feb 2013 09:23:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Patterns</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=5bbe3fd8-b0be-4cc2-bd0d-4667db16bbfa</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=5bbe3fd8-b0be-4cc2-bd0d-4667db16bbfa</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Delphi-and-the-Decorator-Pattern.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=5bbe3fd8-b0be-4cc2-bd0d-4667db16bbfa</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=5bbe3fd8-b0be-4cc2-bd0d-4667db16bbfa</feedburner:origLink></item>
    <item>
      <title>Delphi and the Observer Pattern</title>
      <description>&lt;blockquote&gt;
&lt;p&gt;I&amp;rsquo;m currently reading &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;Head First Design Patterns&lt;/a&gt;, and am finding it very useful and educational.&amp;nbsp; One problem, though &amp;ndash; it&amp;rsquo;s all in Java.&amp;nbsp; So I thought that as part of the exercises, I&amp;rsquo;d translate the code to Delphi.&amp;nbsp; And also as part of my learning process, I thought it would be a good idea to post an article about each of the patterns.&amp;nbsp; I also encourage you to &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124&amp;amp;linkCode=as2&amp;amp;tag=nickhodgeshomepa"&gt;buy the book and read it for yourself.&lt;/a&gt;&lt;img style="margin: 0px; border-style: none !important;" src="http://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0596007124" alt="" width="1" height="1" border="0" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The first real pattern that the book shows is &lt;a href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;the Observer Pattern&lt;/a&gt;.&amp;nbsp; You should use the observer pattern when you have one object that notifies other objects when events occur.&amp;nbsp;&amp;nbsp;&amp;nbsp; Or more succinctly, use it when you have an object that needs to notify other objects about stuff that happens.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;HFDP uses the example of a simple weather station that gathers data about the weather.&amp;nbsp; Each time the weather station takes readings, it needs to update the views of that weather. The weather station reports on Temperature, Humidity, and Pressure.&amp;nbsp; There are three views that the application provides &amp;ndash; current conditions, statistics about the weather, a simple forecast.&lt;/p&gt;
&lt;p&gt;The idea here is that the weather station is the &lt;em&gt;&lt;strong&gt;subject&lt;/strong&gt;&lt;/em&gt; &amp;ndash; it is the object being observed and doing the notifying.&amp;nbsp; The different displays for the information are the &lt;em&gt;&lt;strong&gt;observers&lt;/strong&gt;&lt;/em&gt; &amp;ndash; they watch what happens on the weather station and update themselves based on the notifications that they receive from the weather station.&amp;nbsp; Another way to look at it is that the weather station is a publisher of information, and the displays are subscribers.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The formal definition given for the Observer Pattern goes like this: The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;But when it comes time to implement this, the temptation is to simply embed the displays inside of a method of the weather station.&amp;nbsp; You might create classes for each view, instantiate them in the weather stations constructor, and then simply update the views in a method called &lt;span style="font-family: 'Courier New';"&gt;MeasurementsChanged&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;But of course if you do this, you are breaking some of the base design rules.&amp;nbsp; First, you&amp;rsquo;d be coding against an implementation instead of against an interface.&amp;nbsp; Second, you&amp;rsquo;ve made it really hard to add a new data display should that become necessary.&amp;nbsp; Displays are hard-coded into the weather station and can&amp;rsquo;t be added or removed at runtime.&amp;nbsp; If we do want to add more displays, we&amp;rsquo;d need to modify the weather station itself.&amp;nbsp; Or, put more succinctly, the weather station and its displays are tightly coupled to each other.&amp;nbsp; And if you know one thing about me, I abhor tight coupling.&amp;nbsp; And you should too.&lt;/p&gt;
&lt;p&gt;In any event, there is a better way &amp;ndash; the Observer Pattern.&amp;nbsp; As noted above, observer pattern consists of a &lt;em&gt;Subject&lt;/em&gt; that is monitored by &lt;em&gt;Observers&lt;/em&gt;.&amp;nbsp;&amp;nbsp; We&amp;rsquo;ll implement a very simple version of the observer pattern. The first thing we will do of course, is declare two interfaces:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:bf69c1c6-6bef-4bd3-a051-5de6641105db" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;type
  IObserver = interface
  ['{69B40B25-B2C8-4F11-B442-39B7DC26FE80}']
    procedure Update(aTemperature: integer; aHumidity: integer; aPressure: Double);
  end;

  ISubject = interface
  ['{A9240295-B0C2-441D-BD43-932AF735832A}']
    procedure RegisterObserver(aObserver: IObserver);
    procedure RemoveObserver(aObserver: IObserver);
    procedure NotifyObservers;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The first interface is &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt;, which the observing classes will implement.&amp;nbsp; In it they&amp;rsquo;ll be updated with all the weather information that the weather station has for us.&amp;nbsp; The second is &lt;span style="font-family: 'Courier New';"&gt;ISubject&lt;/span&gt;, which will be implemented by the weather station.&amp;nbsp; It takes three methods, two for handling the connecting and disconnecting of &lt;span style="font-family: 'Courier New';"&gt;IObservers&lt;/span&gt;, and the third for doing the actual notification to the observers.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The first implementation we&amp;rsquo;ll look at is the Weather Station.&amp;nbsp; Here is its implementation declaration.&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:fa931a32-a85b-4d24-9bca-78bacbd67381" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;type
  TWeatherData = class(TInterfacedObject, ISubject)
  private
    FTemperature: integer;
    FHumidity: integer;
    FPressure: double;
    FObserverList: TList&amp;lt;IObserver&amp;gt;;
    function GetTemperature: integer;
    function GetHumidity: integer;
    function GetPressure: double;
  public
    constructor Create;
    destructor Destroy; override;
    procedure SetWeatherInformation(aTemperature: integer; aHumidity: integer; aPressure: double);
    procedure RegisterObserver(aObserver: IObserver);
    procedure RemoveObserver(aObserver: IObserver);
    procedure NotifyObservers;
    procedure MeasurementsChanged;
    property Temperature: integer read GetTemperature;
    property Humidity: integer read GetHumidity;
    property Pressure: double read GetPressure;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The plumbing for managing the weather implementation is all what you&amp;rsquo;d expect.&amp;nbsp; The interesting part, of course, is the implementation of &lt;span style="font-family: 'Courier New';"&gt;ISubject&lt;/span&gt;.&amp;nbsp; Internally, it uses a &lt;span style="font-family: 'Courier New';"&gt;TList&amp;lt;IObserver&amp;gt;&lt;/span&gt; to manage all the observers that get registered with it.&amp;nbsp; The &lt;span style="font-family: 'Courier New';"&gt;RegisterObserver&lt;/span&gt; and &lt;span style="font-family: 'Courier New';"&gt;RemoveObserver&lt;/span&gt; methods simply insert and remove, respectively, instances of classes that implement the &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; interface.&lt;/p&gt;
&lt;p&gt;The real action occurs in the &lt;span style="font-family: 'Courier New';"&gt;NotifyObservers&lt;/span&gt; method.&amp;nbsp; We&amp;rsquo;ll get to that in a second.&amp;nbsp; First, though, let&amp;rsquo;s take a look at the observers.&amp;nbsp; Since all the displays are very similar, this is a good time to use good old-fashioned inheritance to declare a base class that implements the needed functionality, and then have descendent classes that do the work of providing the specified displays.&amp;nbsp; Here, then, is the interface for &lt;span style="font-family: 'Courier New';"&gt;TWeatherDataDisplay&lt;/span&gt;:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7e72a418-b2de-426e-94dc-3088cb2403c3" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;TWeatherDataDisplay = class(TInterfacedObject, IObserver, IDisplay)
  private
    FSubject: TWeatherData;
    FTemperature: integer;
    FHumidity: integer;
    FPressure: Double;
  public
    constructor Create(aWeatherData: TWeatherData);
    destructor Destroy; override;
    procedure Update(aTemperature: integer; aHumidity: integer; aPressure: Double); virtual;
    procedure Display; virtual; abstract;
  end;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="font-family: 'Courier New';"&gt;TWeatherDataDisplay&lt;/span&gt; has fields to keep track of the current weather information.&amp;nbsp; It&amp;rsquo;s descendants can do with it as they please.&amp;nbsp; It also implements the &lt;span style="font-family: 'Courier New';"&gt;IDisplay&lt;/span&gt; interface so that it can report out what it has to say.&amp;nbsp; The &lt;span style="font-family: 'Courier New';"&gt;Update&lt;/span&gt; method will allow the Subject &amp;ndash; in this case the weather station &amp;ndash; to update the displays.&amp;nbsp; &lt;span style="font-family: 'Courier New';"&gt;Update&lt;/span&gt; is a virtual method, by the way, so that descendants can do different things with the incoming information.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Okay, so let&amp;rsquo;s look under the hood.&amp;nbsp; The weather station can accept and remove &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; implementers at runtime.&amp;nbsp;&amp;nbsp; implements the &lt;span style="font-family: 'Courier New';"&gt;NotifyObservers&lt;/span&gt; method as part of the &lt;span style="font-family: 'Courier New';"&gt;ISubject&lt;/span&gt; interface.&amp;nbsp; It gets called whenever the weather information changes.&amp;nbsp; It is implemented as follows: &lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:bbbae3ae-39a6-445d-8700-eb8b6fb260f0" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure TWeatherData.NotifyObservers;
var
  Observer: IObserver;
begin
  for Observer in FObserverList do
  begin
    Observer.Update(Temperature, Humidity, Pressure);
  end;
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This is pretty simple &amp;ndash; it merely enumerates over each item in the observer list and calls the &lt;span style="font-family: 'Courier New';"&gt;Update&lt;/span&gt; method, passing along the new weather data.&amp;nbsp; The base class stores the information, and its descendants process it.&lt;/p&gt;
&lt;p&gt;The real &amp;ldquo;work&amp;rdquo; gets done when the weather station updates the temperature data and calls &lt;span style="font-family: 'Courier New';"&gt;NotifyObservers&lt;/span&gt;:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:48a8b929-96bf-46b6-ba18-003dff6e4b1a" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure TWeatherData.SetWeatherInformation(aTemperature, aHumidity: integer; aPressure: double);
begin
  FTemperature := aTemperature;
  FHumidity := aHumidity;
  FPressure := aPressure;
  MeasurementsChanged;
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;The entire implementation of our little weather station can be found as &lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/118f1392a9b839d3549030d409386dfcbf4b7427/Patterns/Observer?at=default"&gt;part of my demo code on BitBucket.org.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So all of this comes together in a method to create a weather station and add observers:&lt;/p&gt;
&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a26909c8-2d67-4759-8d75-e084fd72b5c8" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;pre class="brush: delphi;"&gt;procedure DoWeatherStation;
var
  WeatherStation: TWeatherStation;
  CurrentDisplay: IDisplay;
  ForecastDisplay: IDisplay;
  StatsDisplay: IDisplay;
begin
  WeatherStation := TWeatherStation.Create;
  try
    CurrentDisplay := TCurrentConditionsDisplay.Create(WeatherData);
    ForecastDisplay := TForecastDisplay.Create(WeatherData);
    StatsDisplay := TStatisticsDisplay.Create(WeatherData);;
    WeatherStation.SetWeatherInformation(70, 55, 28.90);
    WeatherStation.SetWeatherInformation(68, 59, 28.96);
    WeatherStation.SetWeatherInformation(35, 66, 27.40);
    WeatherStation.SetWeatherInformation(55, 55, 27.40);

  finally
    WeatherStation.Free;
  end;
end;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here are some interesting things to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This code doesn&amp;rsquo;t write anything to the console.&amp;nbsp; All of that is done by the display objects that get registered as observers.&amp;nbsp; Each observer is registered, and the code pretty much forgets about them.&amp;nbsp; To the weather station, they are merely &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; interfaces, and the only thing you can do to an &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; is call it&amp;rsquo;s &lt;span style="font-family: 'Courier New';"&gt;Update&lt;/span&gt; method.&amp;nbsp; From the Weather Station&amp;rsquo;s perspective, there&amp;rsquo;s nothing else to it.&lt;/li&gt;
&lt;li&gt;Four calls to &lt;span style="font-family: 'Courier New';"&gt;SetWeatherInformation&lt;/span&gt; result in for reports from all of the updates to the temperature information.&lt;/li&gt;
&lt;li&gt;Once you have a reference to a &lt;span style="font-family: 'Courier New';"&gt;TWeatherStation&lt;/span&gt;, you can add or remove displays at runtime.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New';"&gt;TWeatherStation&lt;/span&gt; doesn&amp;rsquo;t know anything about Weather displays &amp;ndash; it only knows about &lt;span style="font-family: 'Courier New';"&gt;IObservers&lt;/span&gt;.&amp;nbsp; This means that you could have observers that do other things besides being Weather Displays.&amp;nbsp; You could simply store the weather data, or anything else at all.&amp;nbsp; For instance, you could create a &lt;span style="font-family: 'Courier New';"&gt;TWriteWeatherInfoToDatabase&lt;/span&gt; class that implements the &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; interface.&amp;nbsp; The Weather Station itself neither knows nor cares.&amp;nbsp; &lt;span style="font-family: 'Courier New';"&gt;IObservers&lt;/span&gt; are under no obligation to do anything specific with the data passed to them.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;We can change the observers anytime we want without altering the Subject.&amp;nbsp; We can add new observers, too.&amp;nbsp; Basically, observers and subjects are very loosely coupled, and either can be changed, altered, updated, and added to without having to worry about changing the other.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So that should be a quick rundown on how the observer pattern works.&amp;nbsp; The obvious weakness here is that our &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; interface is very specific to weather data.&amp;nbsp; You can create your own interface for your specific implementation.&amp;nbsp; The pattern remains the same no matter how the &lt;span style="font-family: 'Courier New';"&gt;IObserver&lt;/span&gt; interface is designed.&amp;nbsp; This is obviously a good place for generics to enter the picture, and in fact, the Delphi Spring framework implements a generic &lt;span style="font-family: 'Courier New';"&gt;IObservable&amp;lt;T&amp;gt;&lt;/span&gt; interface and implementing class.&lt;/p&gt;
&lt;p&gt;So in summation, the Observer pattern ensures that publishing classes (subjects) can communicate updates to their subscribing (observer) classes with very loose and flexible coupling.&amp;nbsp; Observers can be updated and removed at runtime.&amp;nbsp; Adding observers requires no change to subjects.&amp;nbsp; Subjects don&amp;rsquo;t know much at all about what the observers are up to.&amp;nbsp; It&amp;rsquo;s all very easy and elegant and loosely coupled &amp;ndash; just like you want it.&lt;/p&gt;
&lt;p&gt;Next up &amp;ndash; the Decorator Pattern.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/qfjtw2YCKoA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/qfjtw2YCKoA/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Delphi-and-the-Observer-Pattern.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=a89a690d-d12c-4a92-9b41-ec8f4a03e0de</guid>
      <pubDate>Sun, 27 Jan 2013 20:46:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Patterns</category>
      <category>Software Development</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=a89a690d-d12c-4a92-9b41-ec8f4a03e0de</pingback:target>
      <slash:comments>23</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=a89a690d-d12c-4a92-9b41-ec8f4a03e0de</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Delphi-and-the-Observer-Pattern.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=a89a690d-d12c-4a92-9b41-ec8f4a03e0de</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=a89a690d-d12c-4a92-9b41-ec8f4a03e0de</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #76</title>
      <description>&lt;ul&gt;   &lt;li&gt;There are a lot of cool things that an IDE could do, but it’s getting to the point where my one top feature for the IDE is the automatic addition of interface methods.&amp;#160; If I declare a class that implements a given interface, the IDE should automatically fill those in for me.&amp;#160; That would be really sweet.&amp;#160; This feature is in third place &lt;a href="http://delphi.uservoice.com/forums/4434-code-editor-ide/suggestions/170258-better-class-completion"&gt;in the IDE section of Delphi’s User Voice page&lt;/a&gt;. Feel free to vote for it. &lt;/li&gt;    &lt;li&gt;The fine folks at &lt;a href="http://www.tmssoftware.com/site/"&gt;TMS Software&lt;/a&gt; are organizing a &lt;a href="http://www.be-delphi.com/cms/"&gt;Delphi/TMS day in the Benelux region&lt;/a&gt; – if you are around you should go.&amp;#160; They&amp;#160; have early bird registration until 01 March 2013. &lt;/li&gt;    &lt;li&gt;Embarcadero has announced the coming of &lt;a href="http://www.embarcadero.com/products/delphi/ios-development"&gt;Delphi for iOS&lt;/a&gt;.&amp;#160; I’m not an iOS guy – I’m waiting for the Android stuff.&amp;#160; I don’t have an Apple device at all, and you need one to develop on.&amp;#160; (There’s no iOS emulator or simulator for Windows – everything has to run on Apple hardware/software…) so it’s not of any use to me at the present.&amp;#160; However, I’ll be keeping a close eye on this, as I’ll be developing for Android as soon as I can with Delphi, and I suspect I’ll eventually be wanting the results of that to run on iOS as well.&amp;#160; After all, that is one of the promises of FireMonkey – to have a single codebase that runs on Windows, iOS, and Android.&amp;#160; In any event, no one can say that Delphi isn’t alive and kicking.&amp;#160; The new offering includes a Delphi compiler for ARM chips, which is pretty exciting if you’ve been wanting Delphi to branch out.&amp;#160; And the best part is you can get early access to the beta &lt;a href="https://store.embarcadero.com/542/cookie?affiliate=24952&amp;amp;expiry=45&amp;amp;redirectto=https%3A%2F%2Fstore.embarcadero.com%2F542%2Fpurl-rbanner"&gt;if you buy XE3&lt;/a&gt;.&lt;/li&gt;    &lt;li&gt;Looks like David I has &lt;a href="http://blogs.embarcadero.com/davidi/2013/01/25/42389"&gt;something up his sleeve this year on Delphi’s birthday&lt;/a&gt;.&amp;#160; &lt;/li&gt;    &lt;li&gt;Delphi Developer Days is back this year – with two dates in Europe and just one in the US (Chicago).&amp;#160; &lt;a href="http://caryjensen.blogspot.com/2013/01/delphi-developer-days-2013-cities-and.html?utm_source=feedburner&amp;amp;utm_medium=feed&amp;amp;utm_campaign=Feed%3A+CaryJensenLetsGetTechnical+%28Cary+Jensen++%22Let%27s+Get+Technical%22%29"&gt;Cary has all the details&lt;/a&gt;.&amp;#160; I’ve been to a couple of the events, and they are really fun and well worth the time.&amp;#160; If you can make it, I strongly recommend the event.&amp;#160; Dr. Bob is certainly a more than able replacement for Marco.&amp;#160; With no Delphi Live in the future, this is really the only US conference where Delphi folks can gather.&amp;#160; I’d like to see them add an East Coast date in the US.&amp;#160; Otherwise, I’ll try to get to the Chicago event.&amp;#160; &lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/VSTd8wOR3sY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/VSTd8wOR3sY/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-76.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=ee59b19b-5855-4f40-ac31-cd44fb819dbb</guid>
      <pubDate>Sat, 26 Jan 2013 13:11:55 -0400</pubDate>
      <category>Delphi</category>
      <category>Flotsam and Jetsam</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=ee59b19b-5855-4f40-ac31-cd44fb819dbb</pingback:target>
      <slash:comments>7</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=ee59b19b-5855-4f40-ac31-cd44fb819dbb</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-76.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=ee59b19b-5855-4f40-ac31-cd44fb819dbb</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=ee59b19b-5855-4f40-ac31-cd44fb819dbb</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #75</title>
      <description>&lt;ul&gt;   &lt;li&gt;Now here’s a blog after my own heart:&amp;#160; &lt;a href="http://www.loosecouplings.com"&gt;LooseCouplings.com&lt;/a&gt;&amp;#160; Sadly, it hasn’t been updated in a while, and there are only a few articles, but what is there is really good.&amp;#160; There are four or five articles about Dependency Injection (particularly about how it doesn’t necessarily mean using a DI Container…..) and we’d all do well to read and heed. Thanks to Stefan Glienke for pointing me to it. &lt;/li&gt;    &lt;li&gt;RAD Studio won the “&lt;a href="http://www.developerweek.com/index/awardwinners"&gt;Top Innovator: Developer Tools&lt;/a&gt;” at the &lt;a href="http://www.developerweek.com/"&gt;Developer Week Conference&lt;/a&gt;.&amp;#160; Nice thing to see. &lt;/li&gt;    &lt;li&gt;I wanted to strengthen my SQL skills, so I decided to give &lt;a href="http://www.amazon.com/gp/product/0596526849/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596526849"&gt;Head First SQL&lt;/a&gt;&lt;img style="border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px" border="0" alt="" src="http://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0596526849" width="1" height="1" /&gt; a try.&amp;#160; It was my first foray into the world of the &lt;a href="http://www.amazon.com/s/?_encoding=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=ur2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;field-keywords=head%20first&amp;amp;url=search-alias%3Dstripbooks" target="_blank"&gt;Head First Series&lt;/a&gt;&lt;img style="border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px" border="0" alt="" src="https://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=ur2&amp;amp;o=1" width="1" height="1" /&gt;, and I was pleasantly surprised. I wasn’t quite sure what to expect, but what I got was a great review/tutorial on SQL.&amp;#160; The examples were simple but to the point, the style a touch irreverent but enjoyable, and I actually did, and learned from, the exercises.&amp;#160; Now I’m working on &lt;a href="http://www.amazon.com/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0596007124"&gt;Head First Design Patterns&lt;/a&gt;&lt;img style="border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px" border="0" alt="" src="http://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0596007124" width="1" height="1" /&gt; which so far is the same: excellent, interesting, and attention-holding. I do believe that I'll seek out Head First titles in the future if I want to learn something new. &lt;/li&gt;    &lt;li&gt;I’d like to put in another plug for the &lt;a href="https://plus.google.com/communities/103113685381486591754"&gt;Google Plus Delphi Developers Community&lt;/a&gt;.&amp;#160; We’ve been having a lot of fun over there – or at least I have, maybe the rest of the folks are sick of me.&amp;#160; &lt;img class="wlEmoticon wlEmoticon-winkingsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Winking smile" src="http://www.nickhodges.com/image.axd?picture=wlEmoticon-winkingsmile_41.png" /&gt;&amp;#160; In any event, please stop by and participate.&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Shameless Plug of the Week&lt;/strong&gt;:&amp;#160; &lt;a href="https://refer.sprint.com/a1IlBE"&gt;Sign up for Sprint cell phone service&lt;/a&gt;.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/FJRoqATmBOY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/FJRoqATmBOY/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-75.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=01a9fc9a-f6ea-451d-aba7-3cfa6070b440</guid>
      <pubDate>Sun, 20 Jan 2013 12:44:05 -0400</pubDate>
      <category>Delphi</category>
      <category>Flotsam and Jetsam</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=01a9fc9a-f6ea-451d-aba7-3cfa6070b440</pingback:target>
      <slash:comments>11</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=01a9fc9a-f6ea-451d-aba7-3cfa6070b440</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-75.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=01a9fc9a-f6ea-451d-aba7-3cfa6070b440</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=01a9fc9a-f6ea-451d-aba7-3cfa6070b440</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #74</title>
      <description>&lt;ul&gt;
&lt;li&gt;A while back I wrote a &amp;ldquo;How Not To Code&amp;rdquo; about &lt;a href="http://www.nickhodges.com/post/How-Not-To-Code-2-Don%E2%80%99t-Use-Boolean-Method-Parameters.aspx"&gt;not using Booleans as method parameters&lt;/a&gt;.&amp;nbsp; This line of real-world code that I ran across today -- &lt;span style="font-family: 'Courier New';"&gt;myMonster.Run(cSourceStateLicenseWebNew, '1', '', False, False);&lt;/span&gt; -- is a perfect example of why this shouldn&amp;rsquo;t be done.&amp;nbsp; Who the heck knows what those last two parameters mean?&lt;/li&gt;
&lt;li&gt;Does anyone know if that is real Chinese they speak in &lt;a href="http://www.amazon.com/s/?_encoding=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=ur2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;field-keywords=firefly&amp;amp;url=search-alias%3Daps" target="_blank"&gt;Firefly&lt;/a&gt;&lt;img style="margin: 0px; border-style: none !important;" src="https://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=ur2&amp;amp;o=1" alt="" width="1" height="1" border="0" /&gt;?&lt;/li&gt;
&lt;li&gt;I don&amp;rsquo;t know if this is good or bad, but apparently I&amp;rsquo;ve visited &lt;a href="http://stackoverflow.com/users/2044/nick-hodges"&gt;StackOverflow on 1001 distinct days&lt;/a&gt;.&amp;nbsp; Yikes. (Maybe more by the time you read this&amp;hellip;.)&lt;/li&gt;
&lt;li&gt;&lt;a href="http://francois-piette.blogspot.be/2013/01/using-custom-attribute-for-data.html"&gt;This is a really nice article on attributes&lt;/a&gt; by Francois Piette. I&amp;rsquo;m bummed because he used the example that I was going to use in &lt;a href="https://leanpub.com/codingindelphi"&gt;my book&lt;/a&gt;.&amp;nbsp; Anyone got any better ideas?&lt;/li&gt;
&lt;li&gt;Okay, I did a little more research on &lt;a href="http://www.amazon.com/s/?_encoding=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=ur2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;field-keywords=arduino&amp;amp;url=search-alias%3Daps" target="_blank"&gt;Arduino&lt;/a&gt;&lt;img style="margin: 0px; border-style: none !important;" src="https://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=ur2&amp;amp;o=1" alt="" width="1" height="1" border="0" /&gt; vs. &lt;a href="http://www.amazon.com/s/?_encoding=UTF8&amp;amp;tag=nickhodgeshomepa&amp;amp;linkCode=ur2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;rh=n%3A172282%2Ck%3Araspberry%20pi&amp;amp;field-keywords=raspberry%20pi&amp;amp;url=search-alias%3Delectronics&amp;amp;sprefix=rasp%2Caps%2C137" target="_blank"&gt;Raspberry Pi&lt;/a&gt;&lt;img style="margin: 0px; border-style: none !important;" src="https://www.assoc-amazon.com/e/ir?t=nickhodgeshomepa&amp;amp;l=ur2&amp;amp;o=1" alt="" width="1" height="1" border="0" /&gt;, and I think I&amp;rsquo;m going to start with the Arduino. It seems more &amp;ldquo;science project-y&amp;rdquo; whereas the Raspberry Pi seems more like a complete computer solution.&amp;nbsp; Both sound great, but I&amp;rsquo;m more interested in the &amp;ldquo;controlling something&amp;rdquo; aspect rather than the &amp;ldquo;use all that small but powerful computing power&amp;rdquo;.&amp;nbsp; I think the Arduino would provide more stuff for me to do with my kids in terms of &amp;ldquo;making it all come alive&amp;rdquo;.&amp;nbsp; That&amp;rsquo;s kind of what I&amp;rsquo;m interested in, really &amp;ndash; the &amp;ldquo;Mr. Science&amp;rdquo; kind of stuff.&amp;nbsp; Thanks to you all for the feedback.&lt;/li&gt;
&lt;li&gt;I never talk much about &lt;a href="http://www.lazarus.freepascal.org/"&gt;Lazarus/FreePascal&lt;/a&gt; because I don&amp;rsquo;t use it and I&amp;rsquo;m not much interested in it, but it is starting to move more to the center of my radar as apparently you can use it to program a Raspberry Pi.&amp;nbsp; In fact, &lt;a href="http://jeff-duntemann.livejournal.com/"&gt;our friend Jeff Duntemann&lt;/a&gt; has set up a Raspberry Pi as a &amp;ldquo;regular&amp;rdquo; computer, and &lt;a href="http://jeff-duntemann.livejournal.com/324669.html"&gt;is using Lazarus to write code for it.&lt;/a&gt;&amp;nbsp; Not only that, he&amp;rsquo;s working on &lt;a href="http://jeff-duntemann.livejournal.com/324669.html"&gt;rewriting his classic &amp;ldquo;Borland Pascal from Square One&amp;rdquo;&lt;/a&gt; for Free Pascal, the current version of which you &lt;a href="http://www.copperwood.com/pub/FreePascalSquareOneVol1.pdf"&gt;can download as a PDF&lt;/a&gt;.&amp;nbsp; He also reports that he&amp;rsquo;s doing a book on Lazarus.&amp;nbsp; It&amp;rsquo;s always good to see activity in the world of Pascal and Delphi Programming. Now if we could just get Delphi for Arduino, I&amp;rsquo;d be in heaven.&amp;nbsp; &lt;img class="wlEmoticon wlEmoticon-winkingsmile" style="border-style: none;" src="http://www.nickhodges.com/image.axd?picture=wlEmoticon-winkingsmile_40.png" alt="Winking smile" /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://globalguerrillas.typepad.com/globalguerrillas/2013/01/dronenet-the-next-big-thing.html?utm_source=feedburner&amp;amp;utm_medium=feed&amp;amp;utm_campaign=Feed%3A+typepad%2FrzYD+%28Global+Guerrillas%29"&gt;I for one welcome our new drone overlords.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/GUeC60YfoJg" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/GUeC60YfoJg/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-74.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=344e6c96-8535-479a-973c-7524cce3ac1e</guid>
      <pubDate>Sun, 13 Jan 2013 21:56:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Flotsam and Jetsam</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=344e6c96-8535-479a-973c-7524cce3ac1e</pingback:target>
      <slash:comments>12</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=344e6c96-8535-479a-973c-7524cce3ac1e</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-74.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=344e6c96-8535-479a-973c-7524cce3ac1e</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=344e6c96-8535-479a-973c-7524cce3ac1e</feedburner:origLink></item>
    <item>
      <title>Enumerating and the for…in Loop</title>
      <description>&lt;p&gt;One of those fun little issues in our business is trying to figure out the difference between iterating and enumerating.&amp;#160; &lt;a href="http://stackoverflow.com/a/716250/2044"&gt;This StackOverflow answer&lt;/a&gt; does a pretty good job of explaining it.&amp;#160; Iterating is doing something – usually a series of steps – over and over again.&amp;#160; Enumerating is going over each item in a given collection.&amp;#160; A subtle difference, sure.&amp;#160; One usually iterates when enumerating.&amp;#160; &lt;/p&gt;  &lt;p&gt;Okay, so enumerating is going over each item in a collection.&amp;#160; &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; can enumerate over the items in a collection &lt;a href="http://www.nickhodges.com/post/Loop-Less-with-IEnumerable-and-Spring-for-Delphi.aspx"&gt;as we saw in my last article&lt;/a&gt;.&amp;#160; But you can enumerate over other things as well.&amp;#160; For instance, in Delphi, you can use the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; statement to enumerate over the characters in a string.&amp;#160; &lt;/p&gt;  &lt;p&gt;But this makes me wonder:&amp;#160; how does something become, well, enumeratable?&amp;#160; How do you make something so that you can use with the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; statement?&amp;#160; &lt;/p&gt;  &lt;p&gt;Well, it’s pretty easy.&amp;#160; The rules for Delphi are simple:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To create a class or record to enumerate, it must provide a method called &lt;font face="Courier New"&gt;GetEnumerator()&lt;/font&gt; which in turn needs to be a function that returns a class, an interface, or a record that is an enumerator. &lt;/li&gt;    &lt;li&gt;To be returned as an enumerator, that class, record, or implemented interface needs to have the following:      &lt;ul&gt;       &lt;li&gt;A method called &lt;font face="Courier New"&gt;MoveNext&lt;/font&gt; that returns a &lt;font face="Courier New"&gt;Boolean&lt;/font&gt; indicating whether the end as been reached or not &lt;/li&gt;        &lt;li&gt;A read-only property called &lt;font face="Courier New"&gt;Current&lt;/font&gt; that indicates the item that is currently being “looked at” as the enumeration occurs.&amp;#160; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So, at this point, a simple demo would be helpful, eh?&lt;/p&gt;  &lt;p&gt;You can already enumerate the characters in a string, but it’s a simple example, and so the following example implements this easy to understand enumeration on a string:&lt;/p&gt;  &lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:e9ab3433-caca-4faa-8b40-0295134d075f" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;TForInDemoClassEnumerator = class
  private
    FString: string;
    FIndex: integer;
  protected
    function GetCurrent: Char; virtual;
  public
    constructor Create(aString: string);
    function MoveNext: Boolean;
    property Current: Char read GetCurrent;
  end;

TForInDemo = class
  private
    FTheString: string;
    procedure SetTheString(const Value: string);
  public
    constructor Create(aString: string);
    function GetEnumerator: TForInDemoClassEnumerator;
    property TheString: string read FTheString write SetTheString;
  end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This code declares two classes.&amp;#160; The first class is the enumerator – the class that does the work of&amp;#160; moving over each item, and which will be returned by the call to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt;.&amp;#160; The second is the class that will be enumerated.&amp;#160; Notice, too, that &lt;font face="Courier New"&gt;TForInDemoClassEnumerator&lt;/font&gt; has the read-only property &lt;font face="Courier New"&gt;Current&lt;/font&gt; with the reader &lt;font face="Courier New"&gt;GetCurrent&lt;/font&gt; and &lt;font face="Courier New"&gt;MoveNext&lt;/font&gt; methods.&amp;#160; The &lt;font face="Courier New"&gt;TForInDemo&lt;/font&gt; class has a call to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; that returns an instance of &lt;font face="Courier New"&gt;TForInDemoClassEnumerator&lt;/font&gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;First let’s look at &lt;font face="Courier New"&gt;TForInDemoClassEnumerator&lt;/font&gt;.&amp;#160; You create it by passing the constructor the item to be enumerated – in this case a &lt;font face="Courier New"&gt;string&lt;/font&gt;.&amp;#160; Hence, the constructor:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:e437d2e8-733d-4695-80d4-a16de1e306fd" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;constructor Create(aString: string);&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here is the implementation of the &lt;font face="Courier New"&gt;MoveNext&lt;/font&gt; method:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d6febe1c-eca7-4180-b21d-41d796c6bc1a" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;function TForInDemoClassEnumerator.MoveNext: Boolean;
begin
  Result := FIndex &amp;lt; FString.Length;
  if Result then
  begin
    Inc(FIndex);
  end;
end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code does two things.&amp;#160; First, it sets the result. The function returns &lt;font face="Courier New"&gt;True&lt;/font&gt; if the enumerator is able to move to the next item, and &lt;font face="Courier New"&gt;False&lt;/font&gt; if it has reached the end of the items to be enumerated.&amp;#160; In this case, it merely checks to see the index is before the length of the string.&amp;#160;&amp;#160; If the end has not yet been reached, it just increases the &lt;font face="Courier New"&gt;FIndex&lt;/font&gt; field.&amp;#160; So it’s job here is merely to move the index along, and report if the end has been reached.&lt;/p&gt;

&lt;p&gt;The actual item itself is returned by the &lt;font face="Courier New"&gt;GetCurrent&lt;/font&gt; method, which is self-explanatory:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:0558f9da-6551-4c89-b254-9c1957ca34d1" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;function TForInDemoClassEnumerator.GetCurrent: Char;
begin
  Result := FString[FIndex];
end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And that is it for the enumerator.&amp;#160; It’s pretty simple.&amp;#160; But we’ll soon see that you can do some cool things with these enumerators because you have complete control over how they return the data that they are enumerating over.&lt;/p&gt;

&lt;p&gt;The next step is to see how the actual class to be enumerated provides its enumerator to the compiler.&amp;#160; When the compiler builds the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop construct, it looks at the in part of the construct and thinks “Hey, I need an enumerator here, so I’ll call &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt;”.&amp;#160; If the item in question indeed has a method called &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt;, it calls it, and all is well.&amp;#160; If not, the compiler raises an error because the type in the &lt;font face="Courier New"&gt;in&lt;/font&gt; part of the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop isn’t enumeratable.&amp;#160; &lt;/p&gt;

&lt;p&gt;But of course, our &lt;font face="Courier New"&gt;TForInDemo&lt;/font&gt; class has such a method, and it is quite simple:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:5fc68a1c-a6bb-43f8-9a96-752e0554beff" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;function TForInDemo.GetEnumerator: TForInDemoClassEnumerator;
begin
  Result := TForInDemoClassEnumerator.Create(FTheString);
end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It merely creates and returns an instance of our enumerator, passing it the string value it is storing for the purpose of enumerating.&amp;#160;&amp;#160; Pretty easy and straight forward, really.&lt;/p&gt;

&lt;p&gt;Now that we’ve created all these classes, you can run the following code:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:53d0752e-2f0a-4873-9978-ac83c96cfe0f" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;procedure DoStuff;
var
  ForInDemo: TForInDemo;
  C: Char;
begin
  ForInDemo := TForInDemo.Create('HelloWorld');;
  try
    for C in ForInDemo do
    begin
      Write(C, ',');
    end;
    WriteLn;

  finally
    ForInDemo.Free;
  end;
end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h3&gt;IEnumerator&amp;lt;T&amp;gt; Interface&lt;/h3&gt;

&lt;p&gt;Now the above discussion talks a lot about specific methods that must be included as part of an enumerator, and as part of a class that wants to be enumerated.&amp;#160; And of course, that should immediately make you think “Interfaces!”.&amp;#160; And sure enough, there are some nice interfaces that fall out of this. (You knew I’d work interfaces into things here eventually, didn’t you.&amp;#160; Yes, you did.)&amp;#160; &lt;/p&gt;

&lt;p&gt;Consider this interface:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d80ee84e-ad54-4dec-a7a9-ab258c5e784e" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;type
  IEnumerator&amp;lt;T&amp;gt; = interface
  ['{DD445F01-975D-405E-BCC1-09D3E78CB0FF}']
    function GetCurrent: T;
    function MoveNext: Boolean;
    property Current: T read GetCurrent;
  end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That should look awfully familiar.&amp;#160; It’s the exact two methods and one property needed to implement an enumerator – hence the name.&amp;#160; &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I’ve declared this &lt;font face="Courier New"&gt;IEnumerator&amp;lt;T&amp;gt;&lt;/font&gt; interface myself, but the Delphi RTL includes a similar one in the System.pas unit. And at its base, &lt;font face="Courier New"&gt;IEnumerable&amp;lt;T&amp;gt; &lt;/font&gt;is all about implementing the &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; method.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It leverages generics because the type of what is being enumerated doesn’t matter as far as the interface is concerned.&amp;#160; And remember when I said that a call to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; could return an interface?&amp;#160; Well it can, and the compiler will happily use an &lt;font face="Courier New"&gt;IEnumerator&amp;lt;T&amp;gt;&lt;/font&gt; to implement the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop.&lt;/p&gt;

&lt;p&gt;Thus, your enumerators can implement this interface and your calls to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; can return this interface, and you can add flexibility to how they are implemented.&amp;#160; Here’s an example:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d4f71597-50ea-4586-ab25-26e8e96872ee" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;  TStringEnumerator = class(TInterfacedObject, IEnumerator&amp;lt;Char&amp;gt;)
  private
    FIndex: integer;
    FString: string;
    function GetCurrent: Char;
  public
    constructor Create(aString: string);
    function MoveNext: Boolean;
    property Current: Char read GetCurrent;
  end;

  TInterfaceEnumeratorDemo = class
  private
    FTheString: string;
    procedure SetTheString(const Value: string);
  public
    constructor Create(aString: string);
    function GetEnumerator: IEnumerator&amp;lt;Char&amp;gt;;
    property TheString: string read FTheString write SetTheString;
  end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice that the call to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; returns an &lt;font face="Courier New"&gt;IEnumerator&amp;lt;Char&amp;gt;&lt;/font&gt; which is actually the individual type being iterated. In this case, the generic type needs to be the same type as the variable being returned in the &lt;font face="Courier New"&gt;for&lt;/font&gt; part of the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop.&amp;#160;&amp;#160; &lt;/p&gt;

&lt;p&gt;This enables you to do the following:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:5eaca517-0a10-46b4-afd7-fdc09183188c" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;procedure DoInterfaceStuff;
var
  InterfacedEnumerator: TInterfaceEnumeratorDemo;
  c: Char;
begin
  InterfacedEnumerator := TInterfaceEnumeratorDemo.Create('GoodbyeWorld');
  try
    for c in InterfacedEnumerator do
    begin
      Write(C, ',');
    end;
    WriteLn;
  finally
    InterfacedEnumerator.Free;
  end;
end;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;&amp;#160;&lt;/h3&gt;

&lt;h3&gt;Specialized Enumerators&lt;/h3&gt;

&lt;p&gt;The implementation of an enumerator is, as I mentioned, really simple.&amp;#160; But what if you wanted to get a little creative in the &lt;font face="Courier New"&gt;GetCurrent&lt;/font&gt; method?&amp;#160; After all, at that point you have complete control over what the enumerator returns.&amp;#160; In our simple case with characters and strings, what if we decided to, say, always return the upper case version of the character?&amp;#160; Or if we were iterating over integers, return the squares of the numbers in the collection?&amp;#160; That would be super easy, right? Well, yes, it would.&lt;/p&gt;

&lt;p&gt;Consider this code:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b9382e1f-3088-4e51-b1b2-dfc1a859b7ec" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;  TForInDemoClassUpperCaseEnumerator = class(TForInDemoClassEnumerator)
  protected
    function GetCurrent: Char; override;
  end;
...
function TForInDemoClassUpperCaseEnumerator.GetCurrent: Char;
begin
  Result := UpCase(inherited GetCurrent);
end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This class descends from &lt;font face="Courier New"&gt;TForInDemoClassEnumerator&lt;/font&gt; and overrides the existing enumerator for our demo class and returns the upper case of the character in question.&amp;#160; &lt;/p&gt;

&lt;p&gt;If we wanted, we could return this class from our &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; call, but that would be sort of playing a trick on the user of our code. How about if we provide two different enumerators, and then make each available for enumeration in a &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop.&amp;#160; Surely that is possible, right?&amp;#160; Of course it is.&amp;#160; &lt;/p&gt;

&lt;p&gt;First, we’ll look at the end result and then work our way backwards to see how it was implemented, because you have to play a little trick to expose more than one enumerator for a class:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d81063d4-44df-46fa-b037-69e05456b4fd" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;procedure DoMoreStuff;
var
  C: Char;
  ForInExtraDemo: TForInDemoExtraIterators;
begin

  ForInExtraDemo := TForInDemoExtraIterators.Create('Greetings');
  try
    for C in ForInExtraDemo.AsUpperCase do
    begin
      Write(C, ',');
    end;
    WriteLn;
end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This little routine will display the &lt;font style="background-color: #ffffff"&gt;string “Greetings” as “G,R,E,E,T,I,N,G,S” in the console.&amp;#160; But note in the &lt;font face="Courier New"&gt;for…in&lt;/font&gt; loop that the actual class is not passed to the &lt;font face="Courier New"&gt;in&lt;/font&gt; clause, but a method that returns a “proxy” class that has the desired enumerator attached to i&lt;/font&gt;t instead.&amp;#160; This is implemented as follows:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:fbc6cd57-da7a-4c6e-9ea5-e8daa22c4f94" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;TUpperCaseEnumeratorProxy = class
  private
    FOwner: TForInDemo;
  public
    constructor Create(aOwner: TForInDemo);
    function GetEnumerator: TForInDemoClassUpperCaseEnumerator;
  end;
..
constructor TUpperCaseEnumeratorProxy.Create(aOwner: TForInDemo);
begin
  inherited Create;
  FOwner := aOwner
end;

function TUpperCaseEnumeratorProxy.GetEnumerator: TForInDemoClassUpperCaseEnumerator;
begin
  Result := TForInDemoClassUpperCaseEnumerator.Create(FOwner.TheString);
end&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is just another class that can be enumerated – it has a call to &lt;font face="Courier New"&gt;GetEnumerator&lt;/font&gt; – and thus be returned by a method call on our “real” enumerating class.&amp;#160; It returns an instance of an enumerator called &lt;font face="Courier New"&gt;TForInDemoClassUpperCaseEnumerator&lt;/font&gt; which we looked at above.&amp;#160; So, if you want to iterate over the upper case version of the strings, you call it as we did in the &lt;font face="Courier New"&gt;DoMoreStuff&lt;/font&gt; method above.&amp;#160; The trick here is that instead of iterating over the class itself, you iterate over the proxy class using &lt;font face="Courier New"&gt;ForInExtraDemo.AsUpperCase&lt;/font&gt;.&amp;#160; It’s a neat little trick, eh?&lt;/p&gt;

&lt;p&gt;Thus, the enumerating class becomes:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:801f2b5b-846d-442b-afb8-c26c34064153" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;  TForInDemoExtraIterators = class(TForInDemo)
  private
    FUpper: TUpperCaseEnumeratorProxy;
  public
    constructor Create(aString: string);
    property AsUpperCase: TUpperCaseEnumeratorProxy read FUpper;
  end;
...
constructor TForInDemoExtraIterators.Create(aString: string);
begin
  inherited Create(aString);
  FUpper := TUpperCaseEnumeratorProxy.Create(Self);
end;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;So, that should give you a little insight into what happens with for…in loops and&amp;#160; how you can create classes that participate automatically.&amp;#160; I’m working towards never using the old &lt;font face="Courier New"&gt;for I := 0 to Count –1 do;&lt;/font&gt; construct, and making my classes enumeratable. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;The code for this article with further examples &lt;/strong&gt;&lt;/em&gt;&lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/981f58009e1d67488690f091b5b7d026d218de75/Denmark/5%20-%20IEnumerable/Code?at=default"&gt;&lt;em&gt;&lt;strong&gt;can be found with my demo code on BitBucket&lt;/strong&gt;&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/IBuOfK2mcaU" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/IBuOfK2mcaU/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Enumerating-and-the-for…in-Loop.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=8b3a1675-cdfc-4a80-91b8-a9e1f592726d</guid>
      <pubDate>Sat, 12 Jan 2013 15:05:19 -0400</pubDate>
      <category>Delphi</category>
      <category>Software Development</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=8b3a1675-cdfc-4a80-91b8-a9e1f592726d</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=8b3a1675-cdfc-4a80-91b8-a9e1f592726d</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Enumerating-and-the-for…in-Loop.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=8b3a1675-cdfc-4a80-91b8-a9e1f592726d</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=8b3a1675-cdfc-4a80-91b8-a9e1f592726d</feedburner:origLink></item>
    <item>
      <title>Stuff I’ve Been Reading And Watching</title>
      <description>&lt;OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_9c828f4e-ab60-4870-a6d1-f5f2cb664f87"  WIDTH="600px" HEIGHT="200px"&gt; &lt;PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?rt=tf_cw&amp;amp;ServiceVersion=20070822&amp;amp;MarketPlace=US&amp;amp;ID=V20070822%2FUS%2Fnickhodgeshomepa%2F8010%2F9c828f4e-ab60-4870-a6d1-f5f2cb664f87&amp;amp;Operation=GetDisplayTemplate"&gt;&lt;PARAM NAME="quality" VALUE="high"&gt;&lt;PARAM NAME="bgcolor" VALUE="#FFFFFF"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;&lt;embed src="http://ws.amazon.com/widgets/q?rt=tf_cw&amp;amp;ServiceVersion=20070822&amp;amp;MarketPlace=US&amp;amp;ID=V20070822%2FUS%2Fnickhodgeshomepa%2F8010%2F9c828f4e-ab60-4870-a6d1-f5f2cb664f87&amp;amp;Operation=GetDisplayTemplate" id="Player_9c828f4e-ab60-4870-a6d1-f5f2cb664f87" quality="high" bgcolor="#ffffff" name="Player_9c828f4e-ab60-4870-a6d1-f5f2cb664f87" allowscriptaccess="always" type="application/x-shockwave-flash" align="middle" height="200px" width="600px"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;noscript&gt;&lt;a href="http://ws.amazon.com/widgets/q?rt=tf_cw&amp;amp;ServiceVersion=20070822&amp;amp;MarketPlace=US&amp;amp;ID=V20070822%2FUS%2Fnickhodgeshomepa%2F8010%2F9c828f4e-ab60-4870-a6d1-f5f2cb664f87&amp;amp;Operation=NoScript"&gt;Amazon.com Widgets&lt;/a&gt;&lt;/noscript&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/80cmkyOS2WA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/80cmkyOS2WA/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Stuff-Ive-Been-Reading-And-Watching.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=cfa4c092-cf44-4640-b197-6f0f86e16c26</guid>
      <pubDate>Sat, 12 Jan 2013 10:50:20 -0400</pubDate>
      <category>Delphi</category>
      <category>General</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=cfa4c092-cf44-4640-b197-6f0f86e16c26</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=cfa4c092-cf44-4640-b197-6f0f86e16c26</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Stuff-Ive-Been-Reading-And-Watching.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=cfa4c092-cf44-4640-b197-6f0f86e16c26</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=cfa4c092-cf44-4640-b197-6f0f86e16c26</feedburner:origLink></item>
    <item>
      <title>Flotsam and Jetsam #73</title>
      <description>&lt;ul&gt;   &lt;li&gt;Do you think &lt;a href="http://www4.clustrmaps.com/counter/maps.php?url=http://www.spring4d.org"&gt;it is safe to say that this map&lt;/a&gt; is a pretty good illustration and approximation of world-wide Delphi use?&amp;#160; It’s a map of visitors to &lt;a href="http://www.spring4d.org"&gt;http://www.spring4d.org&lt;/a&gt;.&amp;#160; There are even Delphi users in Iceland! The information you get from &lt;a href="http://www.google.com/trends/explore#q=embarcadero%20delphi"&gt;this Google Trends search&lt;/a&gt; also reveals some interesting geographical information.&amp;#160; It seems that there are a lot of Delphi developers in Russia.&amp;#160; (These types of searches are complicated by the fact that &lt;a href="http://delphi.com/"&gt;Delphi is also a major auto parts company&lt;/a&gt;…)&lt;/li&gt;    &lt;li&gt;The new year means an updated look at the TIOBE Index!&amp;#160; And guess &lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html"&gt;what language is #1 for 2012&lt;/a&gt;?&amp;#160; C.&amp;#160; Not C++, C.&amp;#160; It’s moved past Java as the most popular language.&amp;#160; That’s kind of a surprise to me, anyway.&amp;#160; Delphi continues to hold it’s spot in the “A” language at #15.&amp;#160; They seemed to have combined “Delphi” and “Object Pascal”, but put “Pascal” in its own category. This makes no sense to me, but oh well.&amp;#160; If you combine the numbers all up, “Delphi/Object Pascal/Pascal” ends up at number 11 ahead of Ruby. Completely respectable if you ask me. Another surprising thing to me on the index is how low Javascript ranks.&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;New Delphi Blog of the Week&lt;/strong&gt; &lt;a href="http://francois-piette.blogspot.com/"&gt;comes from Francois Piette&lt;/a&gt;.&amp;#160; &lt;/li&gt;    &lt;li&gt;The &lt;a href="https://plus.google.com/communities/103113685381486591754"&gt;Delphi Developers Community on Google Plus&lt;/a&gt; is quite active – if you aren’t a member, you should join up and contribute.&amp;#160; Maybe you’ll be the 1000th member!&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;New Delphi Components of the Week&lt;/strong&gt;:&amp;#160;&amp;#160; &lt;a href="http://www.easy-ip.net/tchrometabs.html"&gt;Chrome Tabs&lt;/a&gt; – It’s &lt;a href="http://code.google.com/p/delphi-chrome-tabs/"&gt;open source&lt;/a&gt;, too.&amp;#160; Pretty nice.&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Question of the Day: &lt;/strong&gt;Arduino or Raspberry Pi?&amp;#160; Why?&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/0bZbz8f-ldA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/0bZbz8f-ldA/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Flotsam-and-Jetsam-73.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=23430101-885e-4496-81b2-dbfd3c3aff0e</guid>
      <pubDate>Mon, 07 Jan 2013 20:51:19 -0400</pubDate>
      <category>Flotsam and Jetsam</category>
      <category>Delphi</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=23430101-885e-4496-81b2-dbfd3c3aff0e</pingback:target>
      <slash:comments>7</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=23430101-885e-4496-81b2-dbfd3c3aff0e</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Flotsam-and-Jetsam-73.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=23430101-885e-4496-81b2-dbfd3c3aff0e</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=23430101-885e-4496-81b2-dbfd3c3aff0e</feedburner:origLink></item>
    <item>
      <title>Loop Less with IEnumerable and Spring for Delphi</title>
      <description>&lt;p&gt;I rave about the &lt;a href="http://code.google.com/p/delphi-spring-framework/"&gt;Delphi Spring Framework&lt;/a&gt;.&amp;#160; But I think &lt;a href="http://www.nickhodges.com/page/Dependency-Injection-Series.aspx"&gt;my attention to the Dependency Injection Framework&lt;/a&gt; – which is only part of it -- has made you guys think that the two are synonymous.&amp;#160; Not true – Spring4D is more than just a Dependency Injection container.&amp;#160; In fact, there is a wealth of other stuff to be found in &lt;a href="http://www.nickhodges.com/page/Dependency-Injection-Series.aspx"&gt;Spring4D&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stackoverflow.com/questions/14139830/how-to-get-the-maximum-value-in-a-generic-tlistinteger"&gt;Inspired by this StackOverflow question&lt;/a&gt; and a thread in the newsgroups, I thought that I’d write an article about a very powerful but not so well known (apparently) feature of Spring4D – &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; and the accompanying container interfaces and classes.&lt;/p&gt;  &lt;p&gt;So the StackOverflow question basically asks this: &lt;em&gt;How do I find out the maximum value of an integer in &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TList&amp;lt;integer&amp;gt;&lt;/span&gt;&lt;span style="font-family: verdana"&gt;?&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Well, the first thing you might do is use a &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;for&lt;/span&gt; loop to check each item, and save each one as it gets larger.&amp;#160; Or if you were really tricky, you’d use a &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;for…in&lt;/span&gt; statement to find it.&amp;#160; That’s the “straight-up” way to do it.&lt;/p&gt;  &lt;p&gt;But that isn’t the real answer to the StackOverflow question.&amp;#160; &lt;a href="http://stackoverflow.com/a/14179213/2044"&gt;The real answer is this:&lt;/a&gt;&lt;/p&gt;    &lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:8d9cd337-0f1c-479f-a392-1b6423debb51" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;program ListEnumerableDemo;

{$APPTYPE CONSOLE}

{$R *.res}

uses 
    System.SysUtils 
  , Spring.Collections;

var 
  List: IList&amp;lt;Integer&amp;gt;; 
  Enumerable: IEnumerable&amp;lt;integer&amp;gt;;

begin 
  try 
    List := TCollections.CreateList&amp;lt;integer&amp;gt;; 
    List.AddRange([1,6,2,9,54,3,2,7,9,1]);

    Enumerable := List; 
    WriteLn(Enumerable.Max); 
    ReadLn; 
  except 
    on E: Exception do 
      Writeln(E.ClassName, ': ', E.Message); 
  end; 
end. 
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Now that looks quite a bit different than the code you were thinking of, I’ll bet.&amp;#160;&amp;#160; There doesn’t even appear to be any looping going on at all.&amp;#160; There is in the background, but it’s all been abstracted away for you.&amp;#160; You want the maximum value to be found in the list?&amp;#160; Just ask for it.&amp;#160; Cool.&lt;/p&gt;

&lt;p&gt;The key part above, of course, is the use of two things.&amp;#160; First, the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IList&amp;lt;T&amp;gt;&lt;/span&gt; – an interface to a generic list – and secondly, the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt;.&amp;#160; Both of these types are provided by the Delphi Spring Framework.&amp;#160; Both are defined in the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.pas&lt;/span&gt; unit.&amp;#160; That unit contains a complete group of collection interfaces – &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IList&amp;lt;T&amp;gt;, IStack&amp;lt;T&amp;gt; ICollection&amp;lt;T&amp;gt;&lt;/span&gt;, etc. – as well as the definition of &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;.&lt;/span&gt;&amp;#160;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I’d recommend that you consider using &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.pas&lt;/span&gt; instead of the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Generics.Collections.pas&lt;/span&gt; unit which comes with Delphi.&amp;#160; First, it exposes all of the collection types as interfaces, and you should know by now that you should be coding against interfaces and not implementations.&amp;#160; Secondly, each of the types supports the IEnumerable&amp;lt;T&amp;gt; interface, meaning you have the power of this great interface on all of your collections.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’ll tackle those two things in order.&lt;/p&gt;

&lt;h4&gt;Spring Collections&lt;/h4&gt;

&lt;p&gt;The &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.pas&lt;/span&gt; unit is all you need in your uses clause to avail yourself of all that the Spring collections library provides.&amp;#160; The unit includes the following interfaces along with a class to provide easy access to implementations of these interfaces:&lt;/p&gt;

&lt;table id="tfhover" class="tftable" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;strong&gt;Interface&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;ICollection&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A Collection is a group of items in no particular order.&amp;#160; You can't insert items into a specific location in a collection, only add or remove them.&amp;#160; A collection can’t be sorted because it has no order.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IList&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A list is a group of items in a particular order.&amp;#160; Items can be sorted and inserted in particular locations.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A dictionary is a data structure that allows you to “look up” items.&amp;#160; Each entry takes a key and a value, and values can be looked up by asking for it via the item’s key.&amp;#160; This is a useful data structure, as any type can be uses as the index, and any type can be stored for lookup.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IStack&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A stack is a First In, Last Out data structure. Think of a spring-loaded stack of plates at the cafeteria – you “push” plates onto the stack, and then “pop” them off in reverse order.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IQueue&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A queue is a First In, First Out data structure.&amp;#160; Think of an ordinary line that you wait in, or a tube in which items are inserted at one end and taken out on the other.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="239"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;ISet&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/td&gt;

      &lt;td valign="top" width="477"&gt;A set is a data structure involving membership.&amp;#160; Items are either in the set or out.&amp;#160; A set’s methods allow you to determine the intersection, union, or overlap with any other container. &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;All of these types should be familiar to you – if not, you can certainly read up on them via Google.&amp;#160; The important part is that they are all generic (though there are non-generic types with store &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TValue&lt;/span&gt; as well) and they all implement &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;The unit also provides a class called &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TCollections&lt;/span&gt; which has nothing but class methods that return instances of the types listed above.&amp;#160; These methods act as proxies for the constructors of the implementing types.&amp;#160; You can create your own instances if you like, but for the most part, &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TCollections&lt;/span&gt; will do the job of getting an implementation of whatever container interface you want.&amp;#160; For instance, if you want to get a hold of a dictionary, you can do the following:&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9188d0a2-5733-44b6-b9af-f46cfe9f9d20" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;program ListEnumerableDemo;

{$APPTYPE CONSOLE}

{$R *.res}

uses 
    System.SysUtils 
  , Spring.Collections;

var 
  List: IList&amp;lt;Integer&amp;gt;; 
  Enumerable: IEnumerable&amp;lt;integer&amp;gt;;

begin 
  try 
    List := TCollections.CreateList&amp;lt;integer&amp;gt;; 
    List.AddRange([1,6,2,9,54,3,2,7,9,1]);

    Enumerable := List; 
    WriteLn(Enumerable.Max); 
    ReadLn; 
  except 
    on E: Exception do 
      Writeln(E.ClassName, ': ', E.Message); 
  end; 
end. 
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The example above uses &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TCollections.CreateList&amp;lt;integer&amp;gt;&lt;/span&gt; to get an implementation of the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IList&amp;lt;T&amp;gt;&lt;/span&gt; interface.&lt;/p&gt;

&lt;p&gt;The implementations are all in units named &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.Stack.pas&lt;/span&gt; and the like, but the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.pas&lt;/span&gt; unit exposes almost everything you need, and so as a rule, you should only need it in your uses clauses to take advantage of these powerful containers.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h4&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/h4&gt;

&lt;p&gt;Those of you who also use .Net are familiar with &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&lt;/span&gt; already, but perhaps you didn't know that the same power was available to you in your Delphi code.&amp;#160; For those of you not familiar, you are in for a treat.&lt;/p&gt;

&lt;p&gt;The &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; interface is implemented by all of the classes discussed in the previous section, and so any of them can be accessed as an &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;.&lt;/span&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Here is the declaration of &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;:&lt;/span&gt;&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7bd711d4-45f6-45f5-a384-13e4baff42cd" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;IEnumerable&amp;lt;T&amp;gt; = interface(IEnumerable)
    function GetEnumerator: IEnumerator&amp;lt;T&amp;gt;;
    function AsObject: TObject;
    function TryGetFirst(out value: T): Boolean;
    function TryGetLast(out value: T): Boolean;
    function First: T; overload;
    function First(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function FirstOrDefault: T; overload;
    function FirstOrDefault(const defaultValue: T): T; overload;
    function FirstOrDefault(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function Last: T; overload;
    function Last(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function LastOrDefault: T; overload;
    function LastOrDefault(const defaultValue: T): T; overload;
    function LastOrDefault(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function Single: T; overload;
    function Single(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function SingleOrDefault: T; overload;
    function SingleOrDefault(const predicate: TPredicate&amp;lt;T&amp;gt;): T; overload;
    function ElementAt(index: Integer): T;
    function ElementAtOrDefault(index: Integer): T;
    function All(const predicate: TPredicate&amp;lt;T&amp;gt;): Boolean;
    function Any(const predicate: TPredicate&amp;lt;T&amp;gt;): Boolean;
    function Contains(const item: T): Boolean; overload;
    function Contains(const item: T; const comparer: IEqualityComparer&amp;lt;T&amp;gt;): Boolean; overload;
    function Min: T;
    function Max: T;
    function Where(const predicate: TPredicate&amp;lt;T&amp;gt;): IEnumerable&amp;lt;T&amp;gt;;
    function Skip(count: Integer): IEnumerable&amp;lt;T&amp;gt;;
    function SkipWhile(const predicate: TPredicate&amp;lt;T&amp;gt;): IEnumerable&amp;lt;T&amp;gt;; overload;
    function SkipWhile(const predicate: TFunc&amp;lt;T, Integer, Boolean&amp;gt;): IEnumerable&amp;lt;T&amp;gt;; overload;
    function Take(count: Integer): IEnumerable&amp;lt;T&amp;gt;;
    function TakeWhile(const predicate: TPredicate&amp;lt;T&amp;gt;): IEnumerable&amp;lt;T&amp;gt;; overload;
    function TakeWhile(const predicate: TFunc&amp;lt;T, Integer, Boolean&amp;gt;): IEnumerable&amp;lt;T&amp;gt;; overload;
    function Concat(const collection: IEnumerable&amp;lt;T&amp;gt;): IEnumerable&amp;lt;T&amp;gt;;
    function Reversed: IEnumerable&amp;lt;T&amp;gt;;
    procedure ForEach(const action: TAction&amp;lt;T&amp;gt;); overload;
    procedure ForEach(const action: TActionProc&amp;lt;T&amp;gt;); overload;
    procedure ForEach(const action: TActionMethod&amp;lt;T&amp;gt;); overload;
    function EqualsTo(const collection: IEnumerable&amp;lt;T&amp;gt;): Boolean; overload;
    function EqualsTo(const collection: IEnumerable&amp;lt;T&amp;gt;; const comparer: IEqualityComparer&amp;lt;T&amp;gt;): Boolean; overload;
    function ToArray: TArray&amp;lt;T&amp;gt;;
    function ToList: IList&amp;lt;T&amp;gt;;
    function ToSet: ISet&amp;lt;T&amp;gt;;
    function GetCount: Integer;
    function GetIsEmpty: Boolean;
    property Count: Integer read GetCount;
    property IsEmpty: Boolean read GetIsEmpty;
  end;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That’s a lot of cool stuff, right?&amp;#160; Many of the methods here are pretty clear -- it's pretty obvious what the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Min&lt;/span&gt; and &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Max&lt;/span&gt; methods do. But some of the others are a bit trickier.&amp;#160; You can get all of the items.&amp;#160; You can get the First and &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Last&lt;/span&gt; items.&amp;#160; You can get the first x number of items.&amp;#160; You can get the items back out as a &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;List&lt;/span&gt;, an &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Array&lt;/span&gt;, or a &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Set&lt;/span&gt;.&amp;#160; And most powerfully, you can get any particular group of the items out using a predicate.&lt;/p&gt;

&lt;h4&gt;Predicates&lt;/h4&gt;

&lt;p&gt;What is a predicate?&amp;#160; A predicate is an anonymous function (or any function, really, but in our case it is an anonymous function) that takes a single &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;const&lt;/span&gt; parameter of the type in question and returns True or False. &lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b0d3e9b5-59f3-447b-a20f-f233e5f53823" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt; TPredicate&amp;lt;T&amp;gt; = reference to function(const value: T): Boolean;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It is used with some of the methods of &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; to answer this simple question:&amp;#160; In or out?&amp;#160; If the predicate is true, then the individual item is “in”, or included.&amp;#160; If it is “out”, then the predicate will return False.&amp;#160; So if you want to get all the items in a list strings that contain the letter ‘z’, then you can do the following:

  &lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:3da037bc-ad77-4378-9f0e-df91a1a736cd" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;function ContainsLetterZ: IEnumerable&amp;lt;string&amp;gt;;
var
  List: IList&amp;lt;string&amp;gt;;
begin
  List := TCollections.CreateList&amp;lt;string&amp;gt;;
  List.AddRange(['zoo', 'park', 'city', 'town', 'museum', 'jazz festival']);

  Result := List.Where(function(const aString: string): Boolean
                     begin
                       Result := Pos('z', aString) &amp;gt; 0;
                     end);
end;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;The above uses the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Where&lt;/span&gt; method to determine items that should be returned as part of a new &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;string&amp;gt;.&lt;/span&gt;&amp;#160; The above is saying “return to me an enumerable item where all the strings in the list have ‘z’ in them.” &lt;/p&gt;

&lt;p&gt;You can do similar things with the &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TakeWhile&lt;/span&gt; method, which returns items from the start of the list as long as the predicate is True, and stops once the predicate is False.&amp;#160;&amp;#160; You can determine if a given container has or doesn’t have a given element.&amp;#160; You can &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Skip&lt;/span&gt; over a given number of elements and take the rest.&amp;#160; You can use a predicate to &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;SkipWhile&lt;/span&gt; a certain thing is true, and then return the rest once the predicate returns True&amp;#160; Basically, once you have a reference to a collection or any &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; instance, you can get out of it pretty much anything you want.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Something to consider:&amp;#160; If you have a nicely composed class that includes a private IList&amp;lt;T&amp;gt; which gets exposed through proxy methods, then you might want to expose access to the items via a property of type IEnumerable&amp;lt;T&amp;gt; instead of exposing the actual list itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And the real power gets unleashed with the very powerful &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;ForEach&lt;/span&gt; methods.&amp;#160; Folks have always pined for the lovely &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;ForEach&lt;/span&gt; since the days of Borland Pascal’s old &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TCollection&lt;/span&gt; object, and now it’s back in full force, leveraging the power of anonymous methods.&amp;#160; Thus, you can have a collection and do what you please with the items.&amp;#160; Here is a simple example of just outputting them to the console window, but you can have your &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;TAction&amp;lt;T&amp;gt;&lt;/span&gt; do anything at all that you like for each (sorry) element in the container.&lt;/p&gt;

&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2886a616-44a0-4962-869d-6862bddc061b" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre class="brush: delphi;"&gt;procedure SimpleForEachDemo;
var
  List: IList&amp;lt;integer&amp;gt;;
  Action: TAction&amp;lt;integer&amp;gt;;
  i: Integer;
begin
  Action := procedure(const aInt: integer) begin Writeln(Format('This number is: %d', [aInt])); end;

  List := TCollections.CreateList&amp;lt;integer&amp;gt;;
  for i := 1 to 10 do
  begin
    List.Add(Random(100));
  end;

  List.ForEach(Action);
end;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, basically you have a lot of untapped power there in &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;Spring.Collections.pas&lt;/span&gt;, eh?&amp;#160; The use of &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; and predicates ought to transform your code and change the way you look for things in lists and collections.&amp;#160; If you aren’t using these powerful tools yet, I strongly recommend that you add them to your tool chest.&lt;/p&gt;

&lt;p&gt;I’ve written a pretty thorough &lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/981f58009e1d67488690f091b5b7d026d218de75/Denmark/5%20-%20IEnumerable/Code?at=default"&gt;example application for &lt;span style="font-family: &amp;#39;Courier New&amp;#39;"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt; and put it up on BitBucket&lt;/a&gt;.&amp;#160; I also have some &lt;a href="https://bitbucket.org/NickHodges/nickdemocode/src/981f58009e1d/Denmark/6%20-%20Predicates/Code?at=default"&gt;pretty simple examples showing predicates at work&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Remember, the Delphi Spring Framework is more than a Dependency Injection container.&amp;#160; And there’s more to it than the container classes as well.&amp;#160; I strongly recommend that you start treating Spring4D like part of the Delphi Runtime Library.&amp;#160; Heck, I wish Embarcadero would do just that. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/NickHodges/~4/frflPaZB4ro" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/NickHodges/~3/frflPaZB4ro/post.aspx</link>
      <author>nick@nickhodges.com</author>
      <comments>http://www.nickhodges.com/post/Loop-Less-with-IEnumerable-and-Spring-for-Delphi.aspx#comment</comments>
      <guid isPermaLink="false">http://www.nickhodges.com/post.aspx?id=5a5bd758-83b1-47e1-8426-e327a28ac257</guid>
      <pubDate>Sun, 06 Jan 2013 19:57:00 -0400</pubDate>
      <category>Delphi</category>
      <category>Software Development</category>
      <dc:publisher>nick</dc:publisher>
      <pingback:server>http://www.nickhodges.com/pingback.axd</pingback:server>
      <pingback:target>http://www.nickhodges.com/post.aspx?id=5a5bd758-83b1-47e1-8426-e327a28ac257</pingback:target>
      <slash:comments>17</slash:comments>
      <trackback:ping>http://www.nickhodges.com/trackback.axd?id=5a5bd758-83b1-47e1-8426-e327a28ac257</trackback:ping>
      <wfw:comment>http://www.nickhodges.com/post/Loop-Less-with-IEnumerable-and-Spring-for-Delphi.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.nickhodges.com/syndication.axd?post=5a5bd758-83b1-47e1-8426-e327a28ac257</wfw:commentRss>
    <feedburner:origLink>http://www.nickhodges.com/post.aspx?id=5a5bd758-83b1-47e1-8426-e327a28ac257</feedburner:origLink></item>
  </channel>
</rss>
