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

CSC308 Lecture 12

CSC308 Lecture 12

Software Engineering I
Design Patterns - Singleton
(202210)

Javier Gonzalez-Sanchez
PRO

October 16, 2022
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSC 308 Software Engineering I Lecture 12: Design Patterns

    - Singleton Dr. Javier Gonzalez-Sanchez [email protected] users.csc.calpoly.edu/~javiergs | javiergs.com Building 14 -227 Office Hours: By appointment
  2. jgs Assignment 02

  3. jgs Fall 2022 | 00000010 Team Work

  4. jgs Fall 2022 | 00000010 Project – User create dots

  5. jgs Fall 2022 | 00000010 Project – User selects Cluster

    and clicks Run ✅
  6. jgs Fall 2022 | 00000010 Project – User selects Cluster

    and clicks Run ✅
  7. jgs Fall 2022 | 00000010 Project – User selects Line

    and clicks Run ✅
  8. jgs Fall 2022 | 00000010 Project – User selects Line

    and clicks Run ✅
  9. jgs Previously …

  10. jgs Fall 2022 | 00000010 GoF Patterns

  11. jgs Fall 2022 | 00000010 Scenario Observer + update() Observable

    + addObserver() + notifyObservers() + setChanged() Teacher Student Main
  12. jgs Fall 2022 | 00000010 Scenario public class Main {

    public static void main(String[]a) { Teacher teacher1 = new Teacher(); Teacher teacher2 = new Teacher(); Student s1 = new Student(); Student s2 = new Student(); teacher1.addObserver(s1); teacher2.addObserver(s2); teacher1.addObserver(s1); teacher2.addObserver(s2); teacher1.createQuestion(); teacher2.createQuestion(); } }
  13. jgs Fall 2022 | 00000010 Scenario

  14. jgs Singleton Design Patterns

  15. jgs Fall 2022 | 00000010 GoF Patterns

  16. jgs Fall 2022 | 00000010 Singleton

  17. jgs Fall 2022 | 00000010 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 } } }
  18. jgs Fall 2022 | 00000010 Singleton class Singleton{ private static

    Singleton _instance; // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } }
  19. jgs Fall 2022 | 00000010 Scenario

  20. jgs Fall 2022 | 00000010 Ideas

  21. jgs Fall 2022 | 00000010 Questions

  22. jgs

  23. jgs CSC 308 Software Engineering I Lab 12: Patterns Dr.

    Javier Gonzalez-Sanchez [email protected] users.csc.calpoly.edu/~javiergs | javiergs.com Building 14 -227 Office Hours: By appointment
  24. jgs Test Yourselves Class Diagram

  25. jgs Fall 2022 | 00000010 Output

  26. jgs Fall 2022 | 00000010 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); } }
  27. jgs Fall 2022 | 00000010 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; } }
  28. jgs Fall 2022 | 00000010 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()); } }
  29. jgs Fall 2022 | 00000010 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); } }
  30. jgs Fall 2022 | 00000010 Ideas

  31. jgs Fall 2022 | 00000010 Questions

  32. jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected]

    Fall 2022 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.