<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Law of Demeter and the delegate method</title>
	<atom:link href="http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/</link>
	<description>programming &#38; life - ur-ban.com</description>
	<lastBuildDate>Fri, 29 Jan 2010 09:36:38 +0000</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Richard</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1327</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Thu, 19 Nov 2009 11:55:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1327</guid>
		<description>You don&#039;t have to. I should have been a bit clearer, but what I&#039;ve done is renamed withdraw into withdraw_payment, just so it makes a bit more sense in terms of the chain of calls. But withdraw_payment on the Customer object *can* be called withdraw:

So the final call is:

&lt;code&gt;&lt;pre&gt;  @order.withdraw(amount)&lt;/pre&gt;&lt;/code&gt;

In this case it&#039;s now a bit unclear *what* I&#039;m withdrawing.

So Customer becomes:

&lt;code&gt;&lt;pre&gt;  class Customer
    has_many :orders
    has_one :credit_card
    has_one :bank_account
    def withdraw(amount)
      if pay_by_card?
        credit_card.withdraw(amount)
      elsif pay_by_account?
        bank_account.withdraw(amount)
      end
    end
  end&lt;/pre&gt;&lt;/code&gt;

Or if there is only credit cards supported the chain can just be:

&lt;code&gt;&lt;pre&gt;  class Customer
    has_many :orders
    has_one :credit_card
    delegate :withdraw, :to =&gt; :credit_card
  end&lt;/pre&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>You don&#8217;t have to. I should have been a bit clearer, but what I&#8217;ve done is renamed withdraw into withdraw_payment, just so it makes a bit more sense in terms of the chain of calls. But withdraw_payment on the Customer object *can* be called withdraw:</p>
<p>So the final call is:</p>
<p><code>
<pre>  @order.withdraw(amount)</pre>
<p></code></p>
<p>In this case it&#8217;s now a bit unclear *what* I&#8217;m withdrawing.</p>
<p>So Customer becomes:</p>
<p><code>
<pre>  class Customer
    has_many :orders
    has_one :credit_card
    has_one :bank_account
    def withdraw(amount)
      if pay_by_card?
        credit_card.withdraw(amount)
      elsif pay_by_account?
        bank_account.withdraw(amount)
      end
    end
  end</pre>
<p></code></p>
<p>Or if there is only credit cards supported the chain can just be:</p>
<p><code>
<pre>  class Customer
    has_many :orders
    has_one :credit_card
    delegate :withdraw, :to => :credit_card
  end</pre>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1326</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Thu, 19 Nov 2009 11:52:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1326</guid>
		<description>Sorry, my bad. They are other methods on the Customer model which I didn’t provide implementations for. I should have provided them , but it’s a bad example in this case as the decision as to which payment method to use shouldn’t be done with ifs as I’m doing. Going by Open Closed principle, the decision making should be extensible.</description>
		<content:encoded><![CDATA[<p>Sorry, my bad. They are other methods on the Customer model which I didn’t provide implementations for. I should have provided them , but it’s a bad example in this case as the decision as to which payment method to use shouldn’t be done with ifs as I’m doing. Going by Open Closed principle, the decision making should be extensible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Ribakoff</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1286</link>
		<dc:creator>Josh Ribakoff</dc:creator>
		<pubDate>Thu, 12 Nov 2009 14:11:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1286</guid>
		<description>&quot;if pay_by_card&quot;

Where does the pay_by_card come from?</description>
		<content:encoded><![CDATA[<p>&#8220;if pay_by_card&#8221;</p>
<p>Where does the pay_by_card come from?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: david.segonds.org &#187; Blog Archive &#187; Object Oriented Programming: Law of Demeter</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1283</link>
		<dc:creator>david.segonds.org &#187; Blog Archive &#187; Object Oriented Programming: Law of Demeter</dc:creator>
		<pubDate>Wed, 11 Nov 2009 18:22:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1283</guid>
		<description>[...] [ via ur-ban.com] [...]</description>
		<content:encoded><![CDATA[<p>[...] [ via ur-ban.com] [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coding: Pushing the logic back at Mark Needham</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1279</link>
		<dc:creator>Coding: Pushing the logic back at Mark Needham</dc:creator>
		<pubDate>Wed, 11 Nov 2009 10:30:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1279</guid>
		<description>[...] was reading a post on the law of demeter by Richard Hart recently and it reminded me that a lot of the refactorings that we typically do on code bases are [...]</description>
		<content:encoded><![CDATA[<p>[...] was reading a post on the law of demeter by Richard Hart recently and it reminded me that a lot of the refactorings that we typically do on code bases are [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pjammer</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1168</link>
		<dc:creator>pjammer</dc:creator>
		<pubDate>Sun, 25 Oct 2009 18:51:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1168</guid>
		<description>So to use withdraw_payment, you have to also create a withdraw method too in the Customer model right.  not a big deal, but maybe you could add withdraw to your example, just to give the full story and show how the method jumps from one model to the next too.</description>
		<content:encoded><![CDATA[<p>So to use withdraw_payment, you have to also create a withdraw method too in the Customer model right.  not a big deal, but maybe you could add withdraw to your example, just to give the full story and show how the method jumps from one model to the next too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tweets that mention Law of Demeter and the delegate method -- Topsy.com</title>
		<link>http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/comment-page-1/#comment-1167</link>
		<dc:creator>Tweets that mention Law of Demeter and the delegate method -- Topsy.com</dc:creator>
		<pubDate>Sun, 25 Oct 2009 13:09:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.ur-ban.com/blog/?p=1193#comment-1167</guid>
		<description>[...] This post was mentioned on Twitter by Mark Needham, Richard Hart. Richard Hart said: Two blog posts in two days. What&#039;s going on? This time a little on the Law of Demeter http://bit.ly/YmUrd [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was mentioned on Twitter by Mark Needham, Richard Hart. Richard Hart said: Two blog posts in two days. What&#39;s going on? This time a little on the Law of Demeter <a href="http://bit.ly/YmUrd" rel="nofollow">http://bit.ly/YmUrd</a> [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
