0

There is a coding style that I've seen a lot in Android Programming when using Google Services libraries where they use dots to call methods after initializing an instance of a class.

For example, lets say I have a Person class:

public class Person {
    private String firstName;
    private String lastName;    
    private String occupation;
    private String primarySkill;
    private String secondarySkill;

/* ******************
 *   CONSTRUCTOR
 * ******************/
    public Person(String firstName, String lastName) {
        setFirstName(firstName);
        setLastName(lastName);
    }

/* ******************
 *     MUTATORS
 * ******************/
    // firstName Setter:
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    // lastName Setter:
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    // occupation Adder:
    public void addOccupation(String occupation) {
        this.occupation = occupation;
    }

    // primarySkill Adder:
    public void addPrimarySkill(String primarySkill) {
        this.primarySkill = primarySkill;
    }

    // secondarySkill Adder:
    public void addSecondarySkill(String secondarySkill) {
        this.secondarySkill = secondarySkill;
    }   
}

Now, when I create an instance of this class, I want to be able to do the following:

Person bob = new Person("Bob", "Anderson")
            .addOccupation("Student")
            .addPrimarySkill("Java")
            .addSecondarySkill("SQL");

However this gives me syntax errors. How can I build a class that can allow me to do this?

Lenny
  • 33
  • 1
  • 3
  • 1
    Your methods don't return anything, make them return an instance of the class. – Maroun Jun 28 '16 at 14:11
  • This is not magical, if the dot thing works, it is because each of the method calls, returns the object itself. Your methods don't return the object. – Arnaud Jun 28 '16 at 14:12
  • 2
    This is also called **fluent API** or more or less the **builder pattern**. – Joop Eggen Jun 28 '16 at 14:15

3 Answers3

8

What you're looking for is called Method Chaining. Modify your setters to return this:

// occupation Adder:
public Person addOccupation(String occupation) {
    this.occupation = occupation;
    return this;
}
CPerkins
  • 8,968
  • 3
  • 34
  • 47
1

You should have "setter" methods returning the type of the object. To build on your addXyx methods:

public Person setLastName(String lastName) {
    this.lastName = lastName;
    return this;
}

public Person addOccupation(String occupation) {
    this.occupation = occupation;
    return this;
}

public Person addPrimarySkill(String primarySkill) {
    this.primarySkill = primarySkill;
    return this;
}

public Person addSecondarySkill(String secondarySkill) {
    this.secondarySkill = secondarySkill;
    return this;
} 

With that, you could chain your setter invocations:

Person bob = new Person("Bob", "Anderson")
        .addOccupation("Student")
        .addPrimarySkill("Java")
        .addSecondarySkill("SQL");
ernest_k
  • 44,416
  • 5
  • 53
  • 99
1

This is a typical case of using a Builder Pattern. The setters in the builder pattern return this

toro
  • 194
  • 5
  • Not actually typical. From the article you linked, the builder pattern "`uses another object, a builder`", where these methods are just setters which use `Method Chaining`. – CPerkins Jun 28 '16 at 15:33