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

CSE360 Lecture 20

CSE360 Lecture 20

Introduction to Software Engineering
Software Design Patterns: Decorator
(201806)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE360
    Introduction to Software Engineering
    Lecture 20:
    Software Design Patterns: Decorator
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu
    Office Hours: By appointment

    View full-size slide

  2. Case 1:
    Everything you need to know
    for project 4 about Decorator pattern.

    View full-size slide

  3. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 3
    Decorator

    View full-size slide

  4. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 4
    Main

    View full-size slide

  5. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 5
    Companion
    public interface Companion {
    public void doSomething();
    }

    View full-size slide

  6. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 6
    BasicCompanion
    public class BasicCompanion implements Companion {
    @Override
    public void doSomething() {
    System.out.print("Hello Student, ");
    }
    }

    View full-size slide

  7. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 7
    CompanionDecorator
    public class CompanionDecorator implements Companion {
    protected Companion c;
    public void add(Companion c){
    this.c = c;
    }
    @Override
    public void doSomething() {
    this.c.doSomething();
    }
    }

    View full-size slide

  8. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 8
    HelperCompanion
    public class HelperCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to help you. ");
    }
    }

    View full-size slide

  9. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 9
    AffectiveCompanion
    public class AffectiveCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to cheer you.");
    }
    }

    View full-size slide

  10. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 10
    Main

    View full-size slide

  11. Case 2:
    Everything you need to know
    for project 4 about Decorator pattern.

    View full-size slide

  12. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 12
    Decorator

    View full-size slide

  13. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 13
    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 panel = 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 (panel, 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);
    }

    View full-size slide

  14. @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);
    }
    }

    View full-size slide

  15. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 15
    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();
    }
    }

    View full-size slide

  16. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 16
    Companion
    import javax.swing.JComponent;
    public interface Companion {
    public void doSomething(JComponent panel);
    }

    View full-size slide

  17. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 17
    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);
    }
    }

    View full-size slide

  18. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 18
    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);
    }
    }

    View full-size slide

  19. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 19
    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);
    }
    }

    View full-size slide

  20. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 20
    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);
    }
    }

    View full-size slide

  21. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 21
    GUI

    View full-size slide

  22. Test Yourselves

    View full-size slide

  23. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 23
    Class Diagram

    View full-size slide

  24. CSE360 – Introduction to Software Engineering
    Javier G onzalez-Sanchez
    javiergs@ asu.edu
    Sum m er 2017
    Disclaim er. These slides can only be used as study m aterial for the class C SE360 at ASU. They cannot be distributed or used for another purpose.

    View full-size slide