CSE564 Lecture 10

CSE564 Lecture 10

Software Design
Design Patterns: Decorator
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

August 10, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 10: Design Patterns -

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

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

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

    Decorator
  5. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 5 jgs

    Component abstract class Component { public abstract void operation(); }
  6. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 6 jgs

    ConcreteComponent class ConcreteComponent extends Component { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } }
  7. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 7 jgs

    Decorator abstract class Decorator extends Component { protected Component component; public void setComponent(Component component) { this.component = component; } @override public void operation() { if (component != null) { component.operation(); } } }
  8. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 8 jgs

    ConcreteDecoratorA class ConcreteDecoratorA extends Decorator { @override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA-Operation()”); } }
  9. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 9 jgs

    ConcreteDecoratorB class ConcreteDecoratorB extends Decorator { @override public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } }
  10. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 10 jgs

    Main class MainApp { static void main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } }
  11. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 11 jgs

    Main class MainApp { static void main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } } ConcreteComponent - Operation() ConcreteDecoratorA - Operation() ConcreteDecoratorB - Operation()
  12. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 12 jgs

    Decorator vs Inheritance § Both allow you to change how an object behaves § The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime. § Inheritance adds behavior at compilation-time.
  13. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 13 jgs

    Scenario CompanionHelper CompanionTroll Companion
  14. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 14 jgs

    Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper
  15. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 15 jgs

    Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper
  16. jgs Case 1: Everything you need to know

  17. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 18 jgs

    Decorator
  18. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 19 jgs

    Main
  19. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 20 jgs

    Companion public interface Companion { public void doSomething(); }
  20. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 21 jgs

    BasicCompanion public class BasicCompanion implements Companion { @Override public void doSomething() { System.out.print("Hello Student, "); } }
  21. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 22 jgs

    CompanionDecorator public class CompanionDecorator implements Companion { protected Companion c; public void add(Companion c){ this.c = c; } @Override public void doSomething() { this.c.doSomething(); } }
  22. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 23 jgs

    HelperCompanion public class HelperCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to help you. "); } }
  23. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 24 jgs

    AffectiveCompanion public class AffectiveCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to cheer you."); } }
  24. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 25 jgs

    Main
  25. jgs Case 2: Everything you need to know

  26. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 27 jgs

    GUI
  27. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 28 jgs

    Decorator
  28. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 29 jgs

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

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

    Companion import javax.swing.JComponent; public interface Companion { public void doSomething(JComponent panel); }
  32. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 33 jgs

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

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

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

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

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