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

CSE564 Lecture 14

CSE564 Lecture 14

Software Design
Builder
(202103)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

September 14, 2020
Tweet

Transcript

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

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

  3. jgs 564 00001110 GUI

  4. jgs 564 00001110 Decorator

  5. jgs 564 00001110 Main import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

    import javax.swing.*; public class Main extends JFrame implements ActionListener { CompanionPanel companionPanel = new CompanionPanel (); JButton b1 = new JButton("standard"); JButton b2 = new JButton("help"); JButton b3 = new JButton("affect"); JButton b4 = new JButton("helper+affect"); public Main() { setLayout(new BorderLayout(3,2)); add (companionPanel, BorderLayout.CENTER); JPanel control = new JPanel(new GridLayout(1,4)); control.add (b1); b1.addActionListener(this); control.add (b2); b2.addActionListener(this); control.add (b3); b3.addActionListener(this); control.add (b4); b4.addActionListener(this); add (control, BorderLayout.SOUTH); }
  6. jgs @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==b1) {

    BasicCompanion basic = new BasicCompanion(); panel.setCompanion(basic); panel.showYourself(); } else if (e.getSource()==b2) { BasicCompanion basic = new BasicCompanion(); HelperCompanion helper = new HelperCompanion(); helper.add(basic); panel.setCompanion(helper); panel.showYourself(); } else if (e.getSource()==b3) { BasicCompanion basic = new BasicCompanion(); AffectiveCompanion h = new AffectiveCompanion(); h.add(basic); panel.setCompanion(h); panel.showYourself(); } else if (e.getSource()==b4) { HelperCompanion helper2 = new HelperCompanion(); AffectiveCompanion affective = new AffectiveCompanion(); BasicCompanion basic2 = new BasicCompanion(); helper2.add(affective); affective.add(basic2); panel.setCompanion(helper2);panel.showYourself(); } } public static void main(String[] args) { JFrame main = new Main(); main.setSize(500,500); main.setVisible(true); main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  7. jgs 564 00001110 CompanionPanel import java.awt.GridLayout; import javax.swing.JLayeredPane; public class

    CompanionPanel extends JLayeredPane{ public Companion brain; public void setCompanion(Companion x) { brain = x; } public void showYourself() { removeAll(); setLayout(new GridLayout(1,1)); brain.doSomething(this); revalidate(); } }
  8. jgs 564 00001110 Companion import javax.swing.JComponent; public interface Companion {

    public void doSomething(JComponent panel); }
  9. jgs 564 00001110 BasicCompanion import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JLabel;

    public class BasicCompanion implements Companion { @Override public void doSomething(JComponent panel) { ImageIcon face = new ImageIcon("src/face.png"); JLabel label = new JLabel(); label.setIcon(face); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); label.setText("Hello Student, "); panel.add(label); } }
  10. jgs 564 00001110 CompanionDecorator import javax.swing.JComponent; public class CompanionDecorator implements

    Companion { protected Companion c; public void add(Companion c){ this.c=c; } @Override public void doSomething(JComponent panel) { this.c.doSomething(panel); } }
  11. jgs 564 00001110 AffectiveCompanion import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JLabel;

    public class AffectiveCompanion extends CompanionDecorator { @Override public void doSomething(JComponent panel){ super.doSomething(panel); ImageIcon face = new ImageIcon("src/cheer.png"); JLabel label = new JLabel(); label.setIcon(face); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); label.setText("I am here to cheer you."); panel.add(label); } }
  12. jgs 564 00001110 HelperCompanion import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JLabel;

    public class HelperCompanion extends CompanionDecorator { @Override public void doSomething(JComponent panel){ super.doSomething(panel); ImageIcon face = new ImageIcon("src/bubble.png"); JLabel label = new JLabel(); label.setIcon(face); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); label.setText("I am here to help you."); panel.add(label); } }
  13. jgs 564 00001110 GUI

  14. jgs 564 00001110 GoF Patterns

  15. jgs Builder Wrapper of an object When an object cannot

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

  17. 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.
  18. jgs 564 00001110 Builder

  19. 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(); } }
  20. 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”); } }
  21. 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); } }
  22. 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); } }
  23. 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 ? }
  24. jgs Fluent-Builder Wrapper of an object When an object cannot

    be produced in one step
  25. 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.
  26. 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(); } }
  27. 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); }
  28. 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); } }
  29. jgs 564 00001110 Example

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

    be built in a single method call
  31. jgs 564 00001110 GoF Patterns

  32. jgs 564 00001110 Abstract Factory

  33. 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."); } }
  34. 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; } }
  35. 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; } }
  36. 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(); } }
  37. 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.