CSE564 Lecture 11

CSE564 Lecture 11

Software Design
Design Patterns: Builder and Factory
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

August 11, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 11: Design Patterns: Builder

    and Factory Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Previously

  3. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 3 jgs

    GoF Patterns
  4. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 4 jgs

    Test Yourselves CSE 110 Academic Status Report Grade Roster Class Roster
  5. jgs Builder Wrapper of an object When an object cannot

    be produced in one step
  6. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 6 jgs

    GoF Patterns
  7. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 7 jgs

    Builder
  8. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 8 jgs

    // 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(); } } Builder pattern
  9. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 9 jgs

    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”); } }
  10. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 10 jgs

    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); } }
  11. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 11 jgs

    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); } }
  12. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 12 jgs

    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 ? }
  13. jgs Factory Wrapper of a constructor One entire object to

    be built in a single method call
  14. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 14 jgs

    GoF Patterns
  15. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 15 jgs

    Abstract Factory
  16. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 16 jgs

    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."); } }
  17. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 17 jgs

    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; } }
  18. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 18 jgs

    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; } }
  19. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 19 jgs

    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(); } }
  20. jgs Adapter Convert the interface of a class into another

    interface clients expect
  21. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 21 jgs

    GoF Patterns
  22. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 22 jgs

    Adapter
  23. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 23 jgs

    Example RectangleAdapter + draw Rectangle + draw + draw
  24. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 24 jgs

    Client public class Client { public static void main(String[] args) { Shape shape = new RectangleAdapter(new Rectangle(); int x1 = 10, y1 = 20; int x2 = 30, y2 = 60; shape.draw(x1, y1, x2, y2); } }
  25. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 25 jgs

    Adapter class RectangleAdapter implements Shape { private Rectangle adaptee; public RectangleAdapter(Rectangle rectangle) { this.adaptee = rectangle; } public void draw(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x2 - x1); int height = Math.abs(y2 - y1); adaptee.draw(x, y, width, height); } }
  26. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 26 jgs

    Adaptee class Rectangle { public void draw(int x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point”); } }
  27. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 27 jgs

    Review § Prototype § Composite § Flyweight § Proxy
  28. jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez javiergs@asu.edu Fall

    2020 Disclaimer. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.