The Faceted Builder Pattern

The Faceted Builder Pattern is a sub pattern of the builder pattern. Its purpose is to clarify the construction of a complex object “fluently.” Let’s say we want to build a User object with its personal data (personal info, address, education and education). This pattern enables us to have this:

User user = new UserBuilder()
   .called()
      .firstname("Matthew")
      .middlename("Fred")
      .lastname("Dupont")
   .lives()
      .address("123 rue de la pomme")
      .city("Montauban")
      .postalcode(42331)
      .state("Occitanie")
      .country("France")
   .studied()
      .elementary("Ecole Municipal de Paul Laffort")
      .middleschool("College Pourvoira")
      .highschool("Le Bac de Demain")
      .university("Polytechnique")
   .build();

The idea is to define a UserBuilder class that will have three subclasses: UserInfoBuilder, UserAddressBuilder, UserEducationBuilder:

class User {
  // all fields here...
  private String firstname;
  private String lastname;
  ...

  User(UserBuilder builder) {
    this.firstname = builder.firstname();
    ...
  }

}

class UserBuilder {
   public UserInfoBuilder called() { // return info builder to set info
      ...
   }

   public UserAddressBuilder lives() { // same for address
      ...
   }

   public UserEducationBuilder studied() { // same for education
      ...
   }

   public User build() {
     return new User(this);
   }
}

// since UserInfoBuilder extends UserBuilder, we can call lives()
// or studied() once we are done setting up the personal info
class UserInfoBuilder extends UserBuilder { 

   private String firstname;
   ...

   public UserInfoBuilder firstname(String firstname) {
      this.firstname = firstname;
   }
   public UserInfoBuilder middlename(String ...) // same
   public UserInfoBuilder lastname(String ...) // same
}

class UserAddressBuilder extends UserBuilder {
   private String address;
   public UserAddressBuilder address(String address) {
      this.address = address;
   }
   public UserAddressBuilder city(...) // same
   public UserAddressBuilder postalcode(...) // same
   ...
}

class UserEducationBuilder extends UserBuilder {
   ...
   public UserEducationBuilder elementary(...) {
      ...
   }
   
   ...
}

That’s all!

Author: Toujon Lyfoung

This paragraph is supposed to be the place where I put my credentials and achievements. In my opinion, degrees and jobs do not tell much about a person. If you want to know me, read my posts! Blogging has been fun. I do not pretend to do much. I am simply processing, tracking and sharing my reflection. Comments are definitely welcomed to help me continue in my learning.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s