Skip to content

Instantly share code, notes, and snippets.

@Jimexist
Last active April 4, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimexist/6e1a8507411730c38a7dc1288bc4b051 to your computer and use it in GitHub Desktop.
Save Jimexist/6e1a8507411730c38a7dc1288bc4b051 to your computer and use it in GitHub Desktop.
Java short tutorials

Prefer immutable objects by default

POJO

public class Person {
  
  private firstName;
  private lastName;
  private email;
  private List<String> addresses;
  
  // defualt ctor, we don't need it if it's empty
  // public Person() {
  // }
    
  // getters and setters
  
  public void setAddresses(Collection<String> addresses) {
    // this.address = address;
    this.address = new ArrayList<>(addresses); // copy? no?
    // what about getters? what if users get the list and then
    // change it?
  }
}

The problem is:

  • not immutable, hard to reason about thread safety
  • what if it gets changed unintentionally?
  • what about collections?

Immutable Java class

public class Person {
  
  // note final
  private final firstName;
  private final lastName;
  private final email;
  private final ImmutableList<String> addresses;
  
  public Person(String firstName, String lastName, String email, Collection<String> addresses) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.addresses = ImmutableList.copyOf(addresses);
  }
  
  // getters only
}
  • immutable, so thread safe
  • easy to reason and maintain
  • if you use ImmutableCollections, they are also copy-free

Immutable Builder

Use builders to build objects that take multiple optional constructor params.

public class Person {

  private final String firstName;
  private final String lastName;
  private final String email;
  private final ImmutableList<String> addresses; 

  public Person(String firstName, String lastName) {
    this(firstName, lastName, "");
  }

  public Person(String firstName, String lastName, String email) {
    this(firstName, lastName, email, ImmutableList.<String>of());
  }

  public Person(String firstName, String lastName, String email, ImmutableList<String> addresses) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.addresses = addresses;
  }

  // getters
}

Builder Pattern

import com.google.common.collect.ImmutableList;

import java.util.List;

/**
 * Created by jiayu on 4/4/16.
 */
public class Person {

  private final String firstName;
  private final String lastName;
  private final String email;
  private final ImmutableList<String> addresses;

  private Person(String firstName, String lastName, String email, List<String> addresses) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.addresses = ImmutableList.copyOf(addresses);
  }

  public static class Builder {

      private String firstName;
      private String lastName;
      private String email;
      private List<String> addresses;

      public Builder setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
      }

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

      public Builder setEmail(String email) {
        this.email = email;
        return this;
      }

      public Builder setAddresses(List<String> addresses) {
        this.addresses = addresses;
        return this;
      }

      public Person build() {
        return new Person(firstName, lastName, email, addresses);
      }
  }
//            Person p = new Person.Builder()
//                    .setFirstName("jiayu")
//                    .setLastName("liu")
//                    .build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment