$30 off During Our Annual Pro Sale. View Details »

CSE564 Lecture 16

CSE564 Lecture 16

Software Design
Builder
(202110)

Javier Gonzalez-Sanchez
PRO

September 16, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSE 564 Software Design Lecture 16: Builder Dr. Javier

    Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Builder Wrapper of an object When an object cannot

    be produced in one step
  3. jgs 564 00001110 GoF Patterns

  4. 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.
  5. jgs 564 00001110 Builder

  6. 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(); } }
  7. 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”); } }
  8. 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); } }
  9. 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); } }
  10. 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 ? }
  11. jgs Fluent-Builder Wrapper of an object When an object cannot

    be produced in one step
  12. 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.
  13. 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(); } }
  14. 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); }
  15. 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); } }
  16. jgs 564 00001110 Example

  17. jgs 564 00001110 GoF Patterns

  18. jgs Factory Wrapper of a constructor One entire object to

    be built in a single method call
  19. jgs 564 00001110 Abstract Factory

  20. 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."); } }
  21. 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; } }
  22. 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; } }
  23. 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(); } }
  24. jgs 564 00001110 Questions

  25. jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. javiergs@asu.edu 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.