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

CSE460 Lecture 17

CSE460 Lecture 17

Software Analysis and Design
Singleton
(202103)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSE 460 Software Analysis and Design Lecture 17: Singleton

    Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs 00010000 Singleton class Main { 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 } } }
  3. jgs 00010000 Singleton class Singleton{ private static Singleton instance; //

    Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (instance == null){ instance = new Singleton(); } return instance; } }
  4. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez [email protected]

    Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.