Created
February 28, 2018 23:27
-
-
Save matheusmurta/0ff4023ad71fd1b8a87ad9d475c437ef to your computer and use it in GitHub Desktop.
exercicio1Singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exerciciosingleton; | |
import javax.swing.*; | |
public final class Singleton { | |
private static Singleton singleton; | |
public Singleton(){ | |
JOptionPane.showMessageDialog(null, "Objeto criado"); | |
} | |
public static Singleton getSingletonInstance(){ | |
if(singleton == null){ | |
singleton = new Singleton(); | |
} | |
return singleton; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exerciciosingleton; | |
import javax.swing.*; | |
public class ExercicioSingleton { | |
public static void main(String[] args) { | |
Singleton singleton1; | |
Singleton singleton2; | |
Singleton secondSingleton; | |
singleton1 = Singleton.getSingletonInstance(); | |
singleton2 = Singleton.getSingletonInstance(); | |
Singleton singleton3 = new Singleton(); | |
if(singleton1==singleton2){ | |
JOptionPane.showMessageDialog(null,"singleton1 e singleton2 "+ "Estão usando a mesma instância de singleton"); | |
} | |
System.err.println("Endereço - Singleton 1 " + singleton1.hashCode()); | |
System.err.println("Endereço - Singleton 2 " + singleton2.hashCode()); | |
System.err.println("Endereço - Singleton 3 " + singleton3.hashCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment