<?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: How to think about OO</title>
	<atom:link href="http://misko.hevery.com/2009/07/31/how-to-think-about-oo/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 02 Sep 2010 04:05:01 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Making Zend_Auth more &#8220;Object Oriented&#8221; &#124; Aviblock.com</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-2227</link>
		<dc:creator>Making Zend_Auth more &#8220;Object Oriented&#8221; &#124; Aviblock.com</dc:creator>
		<pubDate>Fri, 30 Oct 2009 01:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-2227</guid>
		<description>[...] controller. I knew there had to be a better way. So after a lot of thought, pondering, reading, and inspiration, this is what I&#8217;ve come [...]</description>
		<content:encoded><![CDATA[<p>[...] controller. I knew there had to be a better way. So after a lot of thought, pondering, reading, and inspiration, this is what I&#8217;ve come [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1899</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Fri, 18 Sep 2009 14:08:55 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1899</guid>
		<description>@John,

The issues in this example is that a static method is fixed at compile time and will always return the same kind of object. This does not help you in testing as you want to return a different kind. For example you want AlwaysAuth class rather than LDAPAuth class. Static method can not make this decision unless there is global state, which is very bad way to solve this problem. So static factory method buys you nothing.

-- Misko</description>
		<content:encoded><![CDATA[<p>@John,</p>
<p>The issues in this example is that a static method is fixed at compile time and will always return the same kind of object. This does not help you in testing as you want to return a different kind. For example you want AlwaysAuth class rather than LDAPAuth class. Static method can not make this decision unless there is global state, which is very bad way to solve this problem. So static factory method buys you nothing.</p>
<p>&#8211; Misko</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Januszczak</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1898</link>
		<dc:creator>John Januszczak</dc:creator>
		<pubDate>Fri, 18 Sep 2009 11:38:47 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1898</guid>
		<description>Mikso, 

Below is an example of what I am talking about. When the factory method is implemented, I often see it implemented as a static method (and often in what might as well be a static class). Look at the examples in this article to see what I was getting at:

http://en.wikipedia.org/wiki/Factory_method_pattern</description>
		<content:encoded><![CDATA[<p>Mikso, </p>
<p>Below is an example of what I am talking about. When the factory method is implemented, I often see it implemented as a static method (and often in what might as well be a static class). Look at the examples in this article to see what I was getting at:</p>
<p><a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Factory_method_pattern</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1844</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Wed, 09 Sep 2009 15:25:18 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1844</guid>
		<description>Hi Misko,
You mentioned that some languages offer extension methods.  The language I work with, C#, indeed does offer this.  However, it should be noted that you can&#039;t mock extension methods, so you&#039;ll need to manually write a test double for dependencies with extension methods (assuming you need to call such methods in your tests).
... of course now that I think about it, perhaps extension methods are best used only with newables, and if an injectable needs to be extended it gets Decorated.</description>
		<content:encoded><![CDATA[<p>Hi Misko,<br />
You mentioned that some languages offer extension methods.  The language I work with, C#, indeed does offer this.  However, it should be noted that you can&#8217;t mock extension methods, so you&#8217;ll need to manually write a test double for dependencies with extension methods (assuming you need to call such methods in your tests).<br />
&#8230; of course now that I think about it, perhaps extension methods are best used only with newables, and if an injectable needs to be extended it gets Decorated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1835</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Mon, 07 Sep 2009 00:08:43 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1835</guid>
		<description>Hey!
Wonderful as usual. For completness could you please further explain something. 
You got rid of the &quot;If&quot;s by using polymorphisim (creating the SuperUser etc). My question is ... How would you use this in code. Could you show (if possible) how you would structure the code that uses each of the user types. Will there stil lbe an &quot;If&quot; or &quot;switch&quot; that returns new SuperUser.. or new User?? 
I hope my question made sense.
Thank you for sharing this wealth of information.</description>
		<content:encoded><![CDATA[<p>Hey!<br />
Wonderful as usual. For completness could you please further explain something.<br />
You got rid of the &#8220;If&#8221;s by using polymorphisim (creating the SuperUser etc). My question is &#8230; How would you use this in code. Could you show (if possible) how you would structure the code that uses each of the user types. Will there stil lbe an &#8220;If&#8221; or &#8220;switch&#8221; that returns new SuperUser.. or new User??<br />
I hope my question made sense.<br />
Thank you for sharing this wealth of information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Przemek</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1630</link>
		<dc:creator>Przemek</dc:creator>
		<pubDate>Wed, 12 Aug 2009 21:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1630</guid>
		<description>Misko, you often stress, that objects should not call factory.get(). In most cases it&#039;s better to ask for collaborators in constructor.

However, what if I really need to create objects as a part of request processing? Sometimes I have not enough data to create them beforehand.

Example: user submits his postal address via a web form. Depending on the address I will display an optimal shipping strategy (each strategy can execute different business process). The most natural way for me to implement it is to call shipping=factory.get(address), and then shipping.display()

That would be however calling factory from my objects, that you don&#039;t advise. How would you approach such problem?</description>
		<content:encoded><![CDATA[<p>Misko, you often stress, that objects should not call factory.get(). In most cases it&#8217;s better to ask for collaborators in constructor.</p>
<p>However, what if I really need to create objects as a part of request processing? Sometimes I have not enough data to create them beforehand.</p>
<p>Example: user submits his postal address via a web form. Depending on the address I will display an optimal shipping strategy (each strategy can execute different business process). The most natural way for me to implement it is to call shipping=factory.get(address), and then shipping.display()</p>
<p>That would be however calling factory from my objects, that you don&#8217;t advise. How would you approach such problem?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1621</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Wed, 12 Aug 2009 17:43:32 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1621</guid>
		<description>@John,

how can they be static? A factory should have a get() method with zero arguments (most of the time). This means that it will always create the same thing (unless there is global state, or same parameters get passed in through the factory constructor.). Are your objects calling factory.get()? (bad) or are they simply asking for the dependencies in the constructor? (good). Look at GUICE and how it solves the factory problem.

-- misko</description>
		<content:encoded><![CDATA[<p>@John,</p>
<p>how can they be static? A factory should have a get() method with zero arguments (most of the time). This means that it will always create the same thing (unless there is global state, or same parameters get passed in through the factory constructor.). Are your objects calling factory.get()? (bad) or are they simply asking for the dependencies in the constructor? (good). Look at GUICE and how it solves the factory problem.</p>
<p>&#8211; misko</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Januszczak</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1619</link>
		<dc:creator>John Januszczak</dc:creator>
		<pubDate>Wed, 12 Aug 2009 17:04:17 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1619</guid>
		<description>Misko, I understand the problem with static methods. I also understand that another OO principle you advocate is separation of object creation from object logic. So you have object factories, and all the &quot;if&quot; statements can be relegated there allowing you to avoid these conditionals in your object logic by using polymorphism (as described in this post in the case of the User classes).

When you do all this, what I end up seeing a lot of times is a static factory method (often inside a dedicated static - or might as well be static - factory class). What&#039;s the proper approach here? Specifically, where does the User factory go in your example so that we don&#039;t end up with a static factory method and/or class?</description>
		<content:encoded><![CDATA[<p>Misko, I understand the problem with static methods. I also understand that another OO principle you advocate is separation of object creation from object logic. So you have object factories, and all the &#8220;if&#8221; statements can be relegated there allowing you to avoid these conditionals in your object logic by using polymorphism (as described in this post in the case of the User classes).</p>
<p>When you do all this, what I end up seeing a lot of times is a static factory method (often inside a dedicated static &#8211; or might as well be static &#8211; factory class). What&#8217;s the proper approach here? Specifically, where does the User factory go in your example so that we don&#8217;t end up with a static factory method and/or class?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Przemek</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1589</link>
		<dc:creator>Przemek</dc:creator>
		<pubDate>Tue, 11 Aug 2009 10:56:09 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1589</guid>
		<description>@Val Karpov
If you want to properly test logoutIfFlyingArtichoke, you often want to mock its collaborators to check if only proper functions were called. That leaves you two options IMO, each with its problems. You either subclass User and throw exceptions (poor solution IMO) or use ISP (and worry about functional approach in Java: not a problem for me). Of course, you can make logout  static, but how are you going to mock it to prevent its side-effects and to check whether it was called?
Finally, I don&#039;t know your case, but I think worrying about unexpected calls to a method from another one is unnecessary in many cases.</description>
		<content:encoded><![CDATA[<p>@Val Karpov<br />
If you want to properly test logoutIfFlyingArtichoke, you often want to mock its collaborators to check if only proper functions were called. That leaves you two options IMO, each with its problems. You either subclass User and throw exceptions (poor solution IMO) or use ISP (and worry about functional approach in Java: not a problem for me). Of course, you can make logout  static, but how are you going to mock it to prevent its side-effects and to check whether it was called?<br />
Finally, I don&#8217;t know your case, but I think worrying about unexpected calls to a method from another one is unnecessary in many cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Val Karpov</title>
		<link>http://misko.hevery.com/2009/07/31/how-to-think-about-oo/comment-page-1/#comment-1580</link>
		<dc:creator>Val Karpov</dc:creator>
		<pubDate>Mon, 10 Aug 2009 17:23:28 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=533#comment-1580</guid>
		<description>@Przemek
That can quickly get really bad really quickly - you can end up with an interface for every function defined in your class, public class User implements Login, Logout, etc. which basically leaves you trying to hack Java into being a functional language.</description>
		<content:encoded><![CDATA[<p>@Przemek<br />
That can quickly get really bad really quickly &#8211; you can end up with an interface for every function defined in your class, public class User implements Login, Logout, etc. which basically leaves you trying to hack Java into being a functional language.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
