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
PRO

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 564 00001110 Announcement § Quiz 04 open at the

    end of the lecture and it is due by midnight today § Midterm Exam Wednesday March 10 During the lecture time. No exceptions § Final Exam Wednesday April 21 During the lecture time. No exceptions
  3. jgs Previously

  4. jgs 564 00001110 GUI

  5. jgs 564 00001110 Decorator

  6. 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); }
  7. 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); } }
  8. 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(); } }
  9. jgs 564 00001110 Companion import javax.swing.JComponent; public interface Companion {

    public void doSomething(JComponent panel); }
  10. 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); } }
  11. 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); } }
  12. 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); } }
  13. 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); } }
  14. jgs 564 00001110 GUI

  15. jgs 564 00001110 GoF Patterns

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

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

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

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

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

  31. 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.