<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Polymorphism by Closure Injection?</title>
	<atom:link href="http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/</link>
	<description>Observations of everyday Java phenomena</description>
	<lastBuildDate>Fri, 18 Dec 2009 05:32:07 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: WarpedJavaGuy</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-340</link>
		<dc:creator>WarpedJavaGuy</dc:creator>
		<pubDate>Wed, 10 Sep 2008 10:47:57 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-340</guid>
		<description>Hamlet,

You make an interesting point about the strategy pattern being used to create functional programming abstractions.  Would you say that discrete units of abstraction like SAM interfaces actually encourage a more functional style of programming as opposed to abstract classes which may not be as discrete?  If so, and if this is the preferred, better, and more standard approach, would having closures in Java be a natural step forwards for the language?

There are those who want closures and those who don&#039;t, but most of us I think agree that interfaces are good.  In terms of SAM interfaces, there is not much difference between them and closures.  Both define a single unit of abstraction.  But interfaces are considered object oriented whereas closures are not.  Surely closures would make for a more pleasant programming experience when used in this way.</description>
		<content:encoded><![CDATA[<p>Hamlet,</p>
<p>You make an interesting point about the strategy pattern being used to create functional programming abstractions.  Would you say that discrete units of abstraction like SAM interfaces actually encourage a more functional style of programming as opposed to abstract classes which may not be as discrete?  If so, and if this is the preferred, better, and more standard approach, would having closures in Java be a natural step forwards for the language?</p>
<p>There are those who want closures and those who don&#8217;t, but most of us I think agree that interfaces are good.  In terms of SAM interfaces, there is not much difference between them and closures.  Both define a single unit of abstraction.  But interfaces are considered object oriented whereas closures are not.  Surely closures would make for a more pleasant programming experience when used in this way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: WarpedJavaGuy</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-339</link>
		<dc:creator>WarpedJavaGuy</dc:creator>
		<pubDate>Mon, 08 Sep 2008 10:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-339</guid>
		<description>Howard,

In response to 1:
I actually use the predicate on Legs as well as Routes.  Therefore the use of generics here is justified.  Sample snippet: 
&lt;code&gt;
new Predicate&lt;Leg&gt;(
 &#160;{Leg leg =&gt; leg.startsAt(origin)});
&lt;/code&gt;

In response to 2:
My code does not perform a search on a list of existing routes, but rather on a dynamic iteration of routes that are generated on the fly.  The predicate is used to yield eligible routes in real time as they are generated.  Having said that, the pattern you describe can still be applied here.</description>
		<content:encoded><![CDATA[<p>Howard,</p>
<p>In response to 1:<br />
I actually use the predicate on Legs as well as Routes.  Therefore the use of generics here is justified.  Sample snippet:<br />
<code><br />
new Predicate&lt;Leg&gt;(<br />
 &nbsp;{Leg leg =&gt; leg.startsAt(origin)});<br />
</code></p>
<p>In response to 2:<br />
My code does not perform a search on a list of existing routes, but rather on a dynamic iteration of routes that are generated on the fly.  The predicate is used to yield eligible routes in real time as they are generated.  Having said that, the pattern you describe can still be applied here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-338</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Mon, 08 Sep 2008 01:14:50 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-338</guid>
		<description>I agree with Hamlet D&#039;Arcy - it is the sort of thing I do with the Strategy pattern often.

I would also say that if I wrote the code I would possibly do it differently, I say possibly because I haven&#039;t seen the whole example (which is fair enough this is a blog after all). Two things look out of place in the fragment presented are:

1. The use of generics - there is only Route shown, so why not code to Route directly or extend generic interfaces to encode Route - see below

2. eligible and yield have a lot of overlap in their functionality - in normal optimisation and searching algorithms (operations research) you would typically have:

interface Penalty { double cost( T t ); }

/** Route Penalty is the time taken for a given Route */
interface RoutePenalty extends Penalty {}


interface Constraint { boolean isOK( T t ); }

/** Route Constraint is if a given Route is feasible and if it is within user constraints like maximum number of stops */
interface RouteConstraint extends Constraint {}

The search algorithm is usually generic and accepts a Penalty (the cost of the solution, e.g. time of Route) and a List of Constraints (all of which have to be satisfied if the route is OK), so I would be tempted to code the example using the standard searching conventions. The call to optimise would typically be:

/** List of Routes sorted by their penalty */
interface SortedRoutes extends SortedList {}

final Optimiser optimiser = new Optimiser();
optimiser.setPenalty( routePenalty );
optimiser.setConstraints( routeConstraints );
optimiser.setMethod( OptimiserType.GENETIC );
final SortedRoutes possibleRoutes = optimiser.optimise( routes );

The reason for introducing RoutePenalty, RouteConstraint, SortedRoutes, etc. is that they give you somewhere to comment what the penalty, constraints, method of sorting, etc. is for a Route, they also reads a little better in the code.

The above is pseudo code - but hopefully gives an idea of how standard searching algorithms are used.</description>
		<content:encoded><![CDATA[<p>I agree with Hamlet D&#8217;Arcy &#8211; it is the sort of thing I do with the Strategy pattern often.</p>
<p>I would also say that if I wrote the code I would possibly do it differently, I say possibly because I haven&#8217;t seen the whole example (which is fair enough this is a blog after all). Two things look out of place in the fragment presented are:</p>
<p>1. The use of generics &#8211; there is only Route shown, so why not code to Route directly or extend generic interfaces to encode Route &#8211; see below</p>
<p>2. eligible and yield have a lot of overlap in their functionality &#8211; in normal optimisation and searching algorithms (operations research) you would typically have:</p>
<p>interface Penalty { double cost( T t ); }</p>
<p>/** Route Penalty is the time taken for a given Route */<br />
interface RoutePenalty extends Penalty {}</p>
<p>interface Constraint { boolean isOK( T t ); }</p>
<p>/** Route Constraint is if a given Route is feasible and if it is within user constraints like maximum number of stops */<br />
interface RouteConstraint extends Constraint {}</p>
<p>The search algorithm is usually generic and accepts a Penalty (the cost of the solution, e.g. time of Route) and a List of Constraints (all of which have to be satisfied if the route is OK), so I would be tempted to code the example using the standard searching conventions. The call to optimise would typically be:</p>
<p>/** List of Routes sorted by their penalty */<br />
interface SortedRoutes extends SortedList {}</p>
<p>final Optimiser optimiser = new Optimiser();<br />
optimiser.setPenalty( routePenalty );<br />
optimiser.setConstraints( routeConstraints );<br />
optimiser.setMethod( OptimiserType.GENETIC );<br />
final SortedRoutes possibleRoutes = optimiser.optimise( routes );</p>
<p>The reason for introducing RoutePenalty, RouteConstraint, SortedRoutes, etc. is that they give you somewhere to comment what the penalty, constraints, method of sorting, etc. is for a Route, they also reads a little better in the code.</p>
<p>The above is pseudo code &#8211; but hopefully gives an idea of how standard searching algorithms are used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hamlet D'Arcy</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-337</link>
		<dc:creator>Hamlet D'Arcy</dc:creator>
		<pubDate>Sun, 07 Sep 2008 22:52:35 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-337</guid>
		<description>I think I&#039;d call these functional programming abstractions and functional composition techniques &quot;abstraction through closure injection&quot; and not &quot;polymorphism through closure injection&quot;. It seems the point of the Strategy Pattern isn&#039;t to achieve polymorphism but to achieve an abstract solution framework that can be applied to many different problems. Functional composition, which is kinda what the strategy pattern is about, strives for the same goal.

&lt;blockquote&gt;
WarpedJavaGuy - &lt;strong&gt;September 8, 2008&lt;/strong&gt;

Hamlet, my original non-closure version used polymorphism.  Hence the reasoning behind the title.
&lt;/blockquote&gt;

</description>
		<content:encoded><![CDATA[<p>I think I&#8217;d call these functional programming abstractions and functional composition techniques &#8220;abstraction through closure injection&#8221; and not &#8220;polymorphism through closure injection&#8221;. It seems the point of the Strategy Pattern isn&#8217;t to achieve polymorphism but to achieve an abstract solution framework that can be applied to many different problems. Functional composition, which is kinda what the strategy pattern is about, strives for the same goal.</p>
<blockquote><p>
WarpedJavaGuy &#8211; <strong>September 8, 2008</strong></p>
<p>Hamlet, my original non-closure version used polymorphism.  Hence the reasoning behind the title.
</p></blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: WarpedJavaGuy</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-333</link>
		<dc:creator>WarpedJavaGuy</dc:creator>
		<pubDate>Fri, 05 Sep 2008 10:55:15 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-333</guid>
		<description>It is interesting that you do that Howard, because what you have done is refactored my closure version to use SAM interfaces.  The question is would you have originally coded it by injecting interfaces or would have coded it using an abstract class and anonymous inner subclasses?  To achieve polymorphism, would you have originally chosen deferred execution over late binding?</description>
		<content:encoded><![CDATA[<p>It is interesting that you do that Howard, because what you have done is refactored my closure version to use SAM interfaces.  The question is would you have originally coded it by injecting interfaces or would have coded it using an abstract class and anonymous inner subclasses?  To achieve polymorphism, would you have originally chosen deferred execution over late binding?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comment-332</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Fri, 05 Sep 2008 07:59:52 +0000</pubDate>
		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33#comment-332</guid>
		<description>Surely inner classes are simpler and clearer because you can give meaningful names:
   
   public interface IsRouteOK { boolean test(Route route); }

   public class RouteTests {  
      private IsRouteOK eligible;  
      private IsRouteOK yield;  
      private boolean isEligible;  
  
     public RouteTests(IsRouteOK yield) {  
       this(yield, yield);  
     }  
    
    public RouteTests(IsRouteOK eligible, IsRouteOK yield) {  
      this.eligible = eligible;  
      this.yield = yield;  
    }  
    
    public boolean eligible(Route route) {  
      isEligible = eligible.test(route);  
     return isEligible;  
   }  
   
   public boolean yield(Route test) {  
     return (isEligible &amp;&amp; eligible == yield) ? true : yield.test(route);  
   }  
    
    /* Pre-defined predicates not shown here */  
    
  }</description>
		<content:encoded><![CDATA[<p>Surely inner classes are simpler and clearer because you can give meaningful names:</p>
<p>   public interface IsRouteOK { boolean test(Route route); }</p>
<p>   public class RouteTests {<br />
      private IsRouteOK eligible;<br />
      private IsRouteOK yield;<br />
      private boolean isEligible;  </p>
<p>     public RouteTests(IsRouteOK yield) {<br />
       this(yield, yield);<br />
     }  </p>
<p>    public RouteTests(IsRouteOK eligible, IsRouteOK yield) {<br />
      this.eligible = eligible;<br />
      this.yield = yield;<br />
    }  </p>
<p>    public boolean eligible(Route route) {<br />
      isEligible = eligible.test(route);<br />
     return isEligible;<br />
   }  </p>
<p>   public boolean yield(Route test) {<br />
     return (isEligible &amp;&amp; eligible == yield) ? true : yield.test(route);<br />
   }  </p>
<p>    /* Pre-defined predicates not shown here */  </p>
<p>  }</p>
]]></content:encoded>
	</item>
</channel>
</rss>