Question

I am wondering where and when fluent interfaces are a good idea, so I am looking for examples. So far I have found only 3 useful cases, e.g. Ruby's collections, like

unique_words = File.read("words.txt").downcase.split.sort.uniq.length

and Fest (Java) for unit testing:

assertThat(yoda).isInstanceOf(Jedi.class)
    .isEqualTo(foundJedi)
    .isNotEqualTo(foundSith);

and JMock. Do you know of any other good examples that use a fluent interface?

Was it helpful?

Solution

jQuery. :)

OTHER TIPS

RSpec. Example from the home page:

# bowling_spec.rb
require 'bowling'

describe Bowling do
  before(:each) do
    @bowling = Bowling.new
  end

  it "should score 0 for gutter game" do
    20.times { @bowling.hit(0) }
    @bowling.score.should == 0
  end
end
  1. Ninject: http://www.ninject.org
  2. For an example that doesn't come from general-purpose libraries, I built an automated regression suite for a configuration wizard. I created a state machine that fills in values on a wizard page, verifies those values are acceptable, then moves on to the next page. The code for each step in the state machine looks like:

    step.Filler().Fill().Verify().GoForward();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top