Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE564 Lecture 16

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

CSE564 Lecture 16

Software Design
Builder
(202110)

Javier Gonzalez-Sanchez PRO

September 16, 2020

Transcript

25 slides
  1. 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
  2. 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.
  3. 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(); } }
  4. 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”); } }
  5. 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); } }
  6. 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); } }
  7. 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 ? }
  8. 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.
  9. 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(); } }
  10. 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); }
  11. 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); } }
  12. 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."); } }
  13. 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; } }
  14. 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; } }
  15. 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(); } }
  16. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming