Slide 1

Slide 1 text

jgs CSE 564 Software Design Lecture 16: Builder Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Builder Wrapper of an object When an object cannot be produced in one step

Slide 3

Slide 3 text

jgs 564 00001110 GoF Patterns

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

jgs 564 00001110 Builder

Slide 6

Slide 6 text

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(); } }

Slide 7

Slide 7 text

jgs 564 00001110 Builder pattern class Director { public void constructStudent( Builder builder ) { builder.setSalary(“10”); builder.setAccess(“LIMITED”); builder.setHours(“20”); builder.setComputer(“no”); } public void constructInstructor( Builder builder ) { builder.setSalary(“50”); builder.setAccess(“FULL”); builder.setHours(“40”); builder.setComputer(“yes”); } }

Slide 8

Slide 8 text

jgs 564 00001110 Builder pattern interface Builder { void setSalary (String salary); void setAccess (String access); void setHours (String hours); void setComputer(String computer); } class StudentBuilder extends Builder { private int salary; private String access; private int hours); private Computer c; // setters for all variables public void setSalary (int salary) { this.salary = Integer.parseInt(salary)); } public void setAccess (String access) {this.access = access;} public void setHours (String hours) {this.hours = Integer.parseInt(hours)); } public void setComputer(String computer) {c = computer.equals(“yes”)?new Computer():null;} public Student getResult() { // you can validate here that all data exist return new Student (salary, access, hours, computer); } }

Slide 9

Slide 9 text

jgs 564 00001110 Builder pattern interface Builder { void setSalary (String salary); void setAccess (String access); void setHours (String hours); void setComputer(String computer); } class InstructorBuilder extends Builder { private int salary; private String access; private int hours; private Computer c; // setters for all variables public void setSalary (int salary) { this.salary = Integer.parseInt(salary)); } public void setAccess (String access) {this.access = access;} public void setHours (String hours) {this.hours = Integer.parseInt(hours)); } public void setComputer(String computer) {c = new Computer();} public Student getResult() { // you can validate here that all data exist return new Instructor (salary, access, hours, computer); } }

Slide 10

Slide 10 text

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 ? }

Slide 11

Slide 11 text

jgs Fluent-Builder Wrapper of an object When an object cannot be produced in one step

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

jgs 564 00001110 Fluent-Builder import java.awt.*; public class Client { public static void main(String[] args) { FluentBuilder builder = new BoxFluentBuilder() .setColor(Color.RED) .setSize(20) .setX(10) .setY(20); Product box = ((BoxFluentBuilder)builder).get(); } }

Slide 14

Slide 14 text

jgs 564 00001110 Fluent-Builder import java.awt.Color; public interface Builder { public Builder setColor(Color color); public Builder setSize(int size); public Builder setX(int x); public Builder setY(int y); }

Slide 15

Slide 15 text

jgs 564 00001110 Fluent-Builder import java.awt.*; public class BoxFluentBuilder implements FluentBuilder { private Color color; private int size, x, y; public Builder setColor (Color color) { this.color = color; return this; } public Builder setSize(int size) { this.size = size; return this; } public Builder setX(int x) { this.x = x; return this; } public Builder setY(int y) { this.y = y; return this; } public Box get() { return new Box (color, size, x, y); } }

Slide 16

Slide 16 text

jgs 564 00001110 Example

Slide 17

Slide 17 text

jgs 564 00001110 GoF Patterns

Slide 18

Slide 18 text

jgs Factory Wrapper of a constructor One entire object to be built in a single method call

Slide 19

Slide 19 text

jgs 564 00001110 Abstract Factory

Slide 20

Slide 20 text

jgs 564 00001110 Products public interface Shape { void draw(); } public class RoundedRectangle implements Shape { public void draw() { System.out.println("Inside RoundedRectangle/draw() method."); } } public class RoundedSquare implements Shape { public void draw() { System.out.println("Inside RoundedSquare/draw() method."); } } public class Rectangle implements Shape { public void draw() { System.out.println("Inside Rectangle/draw() method."); } }

Slide 21

Slide 21 text

jgs 564 00001110 Factories public abstract class AbstractFactory { Shape getShape(String shapeType) ; } public class ShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }

Slide 22

Slide 22 text

jgs 564 00001110 Factories public class RoundedShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new RoundedRectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new RoundedSquare(); } return null; } }

Slide 23

Slide 23 text

jgs 564 00001110 Client public class Client { public static void main(String[] args) { AbstractFactory shapeFactory = new ShapeFactory(); //get an object of Shape Rectangle Shape shape1 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape1.draw(); //get an object of Shape Square Shape shape2 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape2.draw(); ] //get shape factory AbstractFactory shapeFactory1 = new RoundedShapeFactory(); //get an object of Shape Rectangle Shape shape3 = shapeFactory1.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape3.draw(); //get an object of Shape Square Shape shape4 = shapeFactory1.getShape("SQUARE"); //call draw method of Shape Square shape4.draw(); } }

Slide 24

Slide 24 text

jgs 564 00001110 Questions

Slide 25

Slide 25 text

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.