CSE564 Lecture 09

CSE564 Lecture 09

Software Design
Design Patterns: Singleton
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

August 09, 2020
Tweet

Transcript

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

    Singleton Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Quiz 02 Available on Canvas The lecture continues at

    6:35 PM
  3. jgs Example 2 Observer

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

    Scenario JFrame Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom Observer + update() JPanel TutorPanel JPanel
  5. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 5 jgs

    Student (version 2) import java.util.*; import javax.swing.*; public class Student extends JPanel implements Observer { JLabel label = new JLabel(); public Student () { this.setBackground(Color.lightGray); ImageIcon background = new ImageIcon("src/student.png"); label.setIcon(background); add(label); } public String answerQuestion (String question) { return "I am thinking about \'" + question +"\'"; } @Override public void update(Observable o, Object arg) { String x = ((Tutor)o).getQuestion(); String y = this.answerQuestion(x); label.setText(y); } }
  6. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 6 jgs

    Tutor (version 2) import java.util.Observable; public class Tutor extends Observable { private String theQuestion; public void askQuestion() { theQuestion = questions[((int)(Math.random()*10))%4]; setChanged(); notifyObservers(); } public void askQuestion(String s) { theQuestion = s; setChanged(); notifyObservers(); } public String getQuestion() { return theQuestion; } }
  7. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 7 jgs

    TutorPanel import javax.swing.*; import javax.awt.*; public class TutorPanel extends JPanel implements ActionListener{ JTextField question = new JTextField(15); JButton ok = new JButton("send"); Tutor tutor; public TutorPanel(Tutor tutor) { this.tutor = tutor; setBackground(Color.white); ImageIcon background = new ImageIcon("src/prof.png"); JLabel label = new JLabel(); label.setIcon(background); add (question); add (ok); ok.addActionListener(this); add(label); } @Override public void actionPerformed(ActionEvent e) { tutor.askQuestion(question.getText()); } }
  8. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 8 jgs

    Classroom (version 2) import java.awt.GridLayout; import javax.swing.JFrame; public class Classroom extends JFrame { public Classroom() { Student student = new Student (); Tutor tutor = new Tutor(); tutor.addObserver(student); setLayout(new GridLayout(2,1)); add(student); add(new TutorPanel(tutor)); } public static void main(String[] args) { JFrame window = new Classroom(); window.setSize(500, 500); window.setVisible(true); } }
  9. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 9 jgs

    Output
  10. jgs Singleton

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

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

    Singleton
  13. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 13 jgs

    Singleton class MainApp { public static void Main(String []a){ // Constructor is protected -- cannot use new Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); // Test for same instance if (s1 == s2){ // true - Objects are the same instance } } }
  14. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 14 jgs

    Singleton class Singleton{ private static Singleton _instance; // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } }
  15. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 15 jgs

    Scenario Quiz Tutor System Companion ControlCenter << singleton>>
  16. jgs Decorator To be Continued …

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