Java 构造函数
Java中的Singleton类
在面向对象编程中,单例类是一次只能有一个对象(类的一个实例)的类。
第一次之后,如果我们尝试实例化Singleton类,那么新变量也指向创建的第一个实例。因此,无论我们通过任何实例对类中的任何变量所做的任何修改,它都会影响创建的单个实例的变量,并且如果我们通过定义的该类类型的任何变量来访问该变量,就会看到该变量。
设计一个单例类:
使构造函数为私有。
编写一个具有此单例类的返回类型对象的静态方法。在这里,Lazy初始化的概念用于编写这种静态方法。
普通类与单例类:普通类和单例类在实例化方面的区别在于,对于普通类我们使用构造函数,而对于单例类我们使用getInstance()方法(示例代码:I)。通常,为了避免混淆,我们也可以在定义此方法时使用类名作为方法名(示例代码:II)。
用getInstance()方法实现Singleton类
// Java program implementing Singleton class
// with getInstance() method
class Singleton
{
// static variable single_instance of type Singleton
private static Singleton single_instance = null;
// variable of type String
public String s;
// private constructor restricted to this class itself
private Singleton()
{
s = "Hello I am a string part of Singleton class";
}
// static method to create instance of Singleton class
public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();
return single_instance;
}
}
// Driver Class
class Main
{
public static void main(String args[])
{
// instantiating Singleton class with variable x
Singleton x = Singleton.getInstance();
// instantiating Singleton class with variable y
Singleton y = Singleton.getInstance();
// instantiating Singleton class with variable z
Singleton z = Singleton.getInstance();
// changing variable of instance x
x.s = (x.s).toUpperCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
System.out.println("\n");
// changing variable of instance z
z.s = (z.s).toLowerCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
}
}
输出:
String from x is HELLO I AM A STRING PART OF SINGLETON CLASS
String from y is HELLO I AM A STRING PART OF SINGLETON CLASS
String from z is HELLO I AM A STRING PART OF SINGLETON CLASS
String from x is hello i am a string part of singleton class
String from y is hello i am a string part of singleton class
String from z is hello i am a string part of singleton class
说明:在Singleton类中,当我们第一次调用getInstance()方法时,它创建了名为single_instance的类的对象并将其返回给变量。由于single_instance是静态的,因此它从null更改为某个对象。下一次,如果我们试图调用getInstance()方法,由于single_instance不为null,它会返回给变量,而不是再次实例化Singleton类。这部分是由if条件完成的。
在主类中,我们通过调用静态方法getInstance(),用3个对象x,y,z实例化单例类。但实际上在创建对象x之后,变量y和z指向对象x,如图所示。因此,如果我们改变对象x的变量,当我们访问对象y和z的变量时就会反映出来。此外,如果我们更改对象z的变量,这反映在我们访问对象x和y的变量时。
用方法名实现与类名相同的Singleton类
// Java program implementing Singleton class
// with method name as that of class
class Singleton
{
// static variable single_instance of type Singleton
private static Singleton single_instance=null;
// variable of type String
public String s;
// private constructor restricted to this class itself
private Singleton()
{
s = "Hello I am a string part of Singleton class";
}
// static method to create instance of Singleton class
public static Singleton Singleton()
{
// To ensure only one instance is created
if (single_instance == null)
{
single_instance = new Singleton();
}
return single_instance;
}
}
// Driver Code
class Main
{
public static void main(String args[])
{
// instantiating Singleton class with variable x
Singleton x = Singleton.Singleton();
// instantiating Singleton class with variable y
Singleton y = Singleton.Singleton();
// instantiating Singleton class with variable z
Singleton z = Singleton.Singleton();
// changing variable of instance x
x.s = (x.s).toUpperCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
System.out.println("\n");
// changing variable of instance x
z.s = (z.s).toLowerCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
}
}
输出:
String from x is HELLO I AM A STRING PART OF SINGLETON CLASS
String from y is HELLO I AM A STRING PART OF SINGLETON CLASS
String from z is HELLO I AM A STRING PART OF SINGLETON CLASS
String from x is hello i am a string part of singleton class
String from y is hello i am a string part of singleton class
String from z is hello i am a string part of singleton class
说明:在Singleton类中,当我们第一次调用Singleton()方法时,它创建一个名为single_instance的Singleton类对象并将其返回给变量。由于single_instance是静态的,因此它从null更改为某个对象。下次,如果我们尝试调用Singleton()方法,由于single_instance不为null,它将返回给变量,而不是再次实例化Singleton类。