jgs 564 00001110 Builder § A Builder is a wrapper of an object. It is used when an object cannot be produced in one single step. § Builder isolates the product’s internal representation and the steps for the product’s construction. § Use the Builder pattern to get rid of having a Product with multiple constructors with a diverse number of parameters each, i.e., as an option for providing default values for attributes in an object.
jgs 564 00001110 Builder pattern // For creating complex objects // Delegate to several builders public class Client { public static void main( string[] args ) { Director director = new Director( ); StudentBuilder sBuilder = new StudentBuilder(); InstructorBuilder iBuilder = new InstructorBuilder(); director.constructStudent( sBuilder ); Student s = sBuilder.getResult(); s.show(); director.constructInstructor( iBiulder ); Instructor i = iBuilder.GetResult(); i.Show(); } }
jgs 564 00001110 Builder pattern class Student { private int salary; private String access; private int hours; private Computer c; // common simple trivial getters // common simple trivial setters // common simple trivial constructor // what about making the constructor private ? } class Instructor { private int salary; private String access; private int hours; private Computer c; // common simple trivial getters // common simple trivial setters // common simple trivial constructor // what about making the constructor private ? }
jgs 564 00001110 Fluent-Builder § The Fluent Builder is a builder whose design relies on method chaining. In doing so, it aims to promote code legibility.
jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.