How to Make a ConfigurationBuilder to Talk to the Twitter Server with Your Android App

By Barry Burd

If you want to make a Twitter app that works with Android devices, you need your app to talk to the Twitter server. You can use a ConfigurationBuilder. In the fake code below, the chaining of set method calls, one after another, is called the builder pattern.

Twitter twitter;
// … Some code goes here
ConfigurationBuilder builder =
new ConfigurationBuilder();
builder
.setOAuthConsumerKey(“01qedaqsdtdemrVJIkU1dg”)
.setOAuthConsumerSecret(“TudeMgXgh37Ivq173SNWnRIhI”)
.setOAuthAccessToken(“1385541-ueSEFeFgJ8vUpfy6LBv6”)
.setOAuthAccessTokenSecret(“G2FXeXYLSHI7XlVdMsS2e”);
TwitterFactory factory =
new TwitterFactory(builder.build());
twitter = factory.getInstance();

Here’s the basic idea. A configuration builder has lots of properties, and you can imagine several different ways of setting those properties. For example, you could have one enormous constructor:

// This is not correct Twitter4J code:
ConfigurationBuilder builder = new ConfigurationBuilder(
“01qedaqsdtdemrVJIkU1dg”, “TudeMgXgh37Ivq173SNWnRIhI”,
“ 1385541-ueSEFeFgJ8vUpfy6LBv6”, “G2FXeXYLSHI7XlVdMsS2e”);

This approach is really cumbersome because you must remember which string belongs in which position. In fact, it gets worse. A configuration builder has 46 different properties, and you may want to set more than four of these properties. However, a constructor with 46 parameters would be really awful.

Another possibility is to create a blank-slate configuration builder and then set each of its properties with separate method calls.

// This is not correct Twitter4J code:
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(“01qedaqsdtdemrVJIkU1dg”);
builder.setOAuthConsumerSecret
(“TudeMgXgh37Ivq173SNWnRIhI”);
builder.setOAuthAccessToken
(“ 1385541-ueSEFeFgJ8vUpfy6LBv6”);
builder.setOAuthAccessTokenSecret
(“G2FXeXYLSHI7XlVdMsS2e”);

This is less awkward than having a giant constructor, but there’s a better way. In the Twitter4J API, the ConfigurationBuilder class has 46 set methods. Each method applies to an existing ConfigurationBuilder instance. And each method returns, as its result, a new ConfigurationBuilder instance. So, the statement

ConfigurationBuilder builder =
new ConfigurationBuilder();

creates a blank-slate configuration builder. The next piece of code

builder
.setOAuthConsumerKey(“01qedaqsdtdemrVJIkU1dg”)

applies to the blank-slate instance. But the value of this piece of code is an instance with a particular OAuth consumer key. To this enhanced instance you apply

.setOAuthConsumerSecret(“TudeMgXgh37Ivq173SNWnRIhI”)

The combined code’s value is an even better instance — one with a particular OAuth consumer key and an OAuth consumer secret. And so on. Each application of a set method takes an existing instance and yields an instance with more and better properties. This elegant way of adding properties to an object is the builder pattern. That’s the idea behind the code here.