<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alexandre Martins &#187; Domain-Driven Design</title>
	<atom:link href="http://blog.m.artins.net/tag/domain-driven-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.m.artins.net</link>
	<description>On Agile Software Development</description>
	<lastBuildDate>Thu, 12 Aug 2010 20:09:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Hamcrest Out Of Test Code!</title>
		<link>http://blog.m.artins.net/hamcrest-out-of-test-code/</link>
		<comments>http://blog.m.artins.net/hamcrest-out-of-test-code/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 12:38:30 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Behaviour-Driven Development]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=168</guid>
		<description><![CDATA[It's been a while since I read some interesting posts showing creative uses of Hamcrest library out of test code. Since then I've been proscrastinating to implement my own version, trying strongly typed java delegates. Thankfully this week I came across a nice API called hamcrest-collections. It uses Hamcrest to implement features such as select, [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a while since I read <a href="http://joe.truemesh.com/blog/000705.html">some</a> <a href="http://jroller.com/ghettoJedi/entry/using_hamcrest_for_iterators">interesting</a> <a href="http://cromwellian.blogspot.com/2006/07/fun-with-generics-and-cglib-functional.html">posts</a> showing creative uses of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> library out of test code. Since then I've been proscrastinating to implement my own version, trying <a href="http://weblogs.java.net/blog/alexwinston/archive/2005/04/strongly_types_1.html">strongly typed java delegates</a>. </p>
<p>Thankfully this week I came across a nice API called <a href="http://code.google.com/p/hamcrest-collections/">hamcrest-collections</a>. It uses Hamcrest to implement features such as <strong><code>select</code></strong>, <strong><code>reject</code></strong>, <strong><code>map</code></strong>, <strong><code>reduce</code></strong> and <strong><code>zip</code></strong> familiar from languages like Ruby and Python. </p>
<h2>Selectors</h2>
<p>Selectors can be used to select or reject items that matches a given Matcher, from any iterable object. It reminds me the <a href="http://martinfowler.com/apsupp/spec.pdf">Specification Pattern</a> from <a href="http://domaindrivendesign.org/">Domain-Driven Design</a>, which is also used for querying objects that satisfies defined specifications.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public static final Person john = new Person("John", 28);
public static final Person nicole = new Person("Nicole", 12);
public static final Person ryan = new Person("Ryan", 23);
public static final Person nathan = new Person("Nathan", 18);

public static final List<Person> list() {
    return Arrays.asList(john, nicole, ryan, nathan);
}</pre>
<p><br/><br />
The code below selects from the list of users defined above, the ones that are under twenty.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_select_only_people_under_twenty_years_old() {
    List<Person> users = Person.list();
    Iterable<Person> underTwentyList = select(users, underAge(20));
    assertThat(underTwentyList, hasItems(nicole, nathan));
    assertThat(underTwentyList, not(hasItems(john, ryan)));
}
</pre>
<p><br/><br />
The code below rejects all the users that are under twenty.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_reject_every_people_under_twenty_years_old() {
    List<Person> users = Person.list();
    Iterable<Person> aboveTwentyList = reject(users, underAge(20));
    assertThat(aboveTwentyList, hasItems(john, ryan));
    assertThat(aboveTwentyList, not(hasItems(nicole, nathan)));
}</pre>
<p><br/></p>
<h2>Map and Reduce</h2>
<p>Map is used to apply a function onto each item in any iterable object, whereas Reduce combines all these elements, applying a Reducer implementation. In our example, we map the timesTwo function, that doubles each element in the list, and then we reduce it by adding up all of them.</p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
@Test
public void should_double_each_number_in_the_list_then_sum_all_of_them() {
    List<Integer> numbers = Arrays.asList(1, 2, 3);
    MultiplyBy timesTwo = new MultiplyBy(2);

    Iterable<Integer> result = map(numbers, timesTwo);
    assertThat(result, hasItems(2, 4, 6));

    Integer sum = reduce(result, new Sum());
    assertThat(sum, equalTo(12));
}</pre>
<p><br/></p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public class MultiplyBy implements Function<Integer, Integer> {
    private Integer factor;

    public MultiplyBy(Integer factor) {
        this.factor = factor;
    }

    public Integer apply(Integer number) {
        return (int)number * factor;
    }
}</pre>
<p><br/></p>
<pre style="border: solid gray 1px; width: 90%; padding: 10px;">
public class Sum implements Reducer<Integer> {
    public Integer apply(Integer first, Integer second) {
        return first + second;
    }
}</pre>
<p><br/></p>
<p>Despite the bias created by some developers, that Hamcrest should not be used anywhere else but test code, specially after JUnit has defined it as its new matcher library, just ignore it and add these features to your runtime library, so that you can let your creativity drive you when developing. Get rid of "for" loops from your life! <img src='http://blog.m.artins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/hamcrest-out-of-test-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Value Objects &amp; RoR</title>
		<link>http://blog.m.artins.net/value-objects-ror/</link>
		<comments>http://blog.m.artins.net/value-objects-ror/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 01:14:28 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=9</guid>
		<description><![CDATA[Currently I'm working on my personal project, aiming to increase my Ruby On Rails skills. It's quite a simple system for my dad's company and one of the features is to register his clients. I started it out writing the Specs and as I'm new to the Ruby world, doubts started to pop inside my [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I'm working on my personal project, aiming to increase my Ruby On Rails skills. It's quite a simple system for my dad's company and one of the features is to register his clients. I started it out writing the <a href="http://rspec.rubyforge.org/">Specs</a> and as I'm new to the Ruby world, doubts started to pop inside my head. I was wondering how to use <a href="http://www.darrenhobbs.com/archives/2007/04/strong_typing_a.html">Value Objects</a> ( Eric Evans definition, please! ) integrated with RoR, something I can do with Java as shown below:</p>
<pre name="code" class="java">
public class Phone() {
    private int code;
    private int number;
    ....
}
</pre>
<pre name="code" class="java">
public class Client() {
    private String name;
    private Phone home;
    private Phone mobile;
    ....
}
</pre>
<p>Chatting with <a href="http://lixo.org">Carlos Villela</a>, he advised me to take a look at <code>composed_of</code> feature, that it would help me get the solution for my problem. The final result is shown below. I'm definitely not used to the Ruby syntax yet, for me it's still a bit confusing.</p>
<pre name="code" class="ruby">
class Phone
  attr_reader :code, :number
  def initialize(code, number)
      @code, @number = code, number
  end

  def ==(other)
      code==other.code &#038;& number==other.number
  end
end
</pre>
<pre name="code" class="ruby">
class Client < ActiveRecord::Base
  composed_of :home, :class_name => "Phone",
        :mapping => [ %w(home_code code), %w(home_number number) ]
  composed_of :mobile, :class_name => "Phone",
        :mapping => [ %w(mob_code code), %w(mob_number number) ]
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/value-objects-ror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
