不懂单例的概念class
I dont understand the concept of a singleton class
我知道如何在java中制作单例class,但我不明白的是单例的概念。比如为什么我需要一个单例 class 以及为什么我要使用单例而不是常规 class?
"Singleton pattern restricts the instantiation of a class and ensures
that only one instance of the class exists in the java virtual
machine."
我刚读了那个定义,但我不明白,如果 class 有一个或多个实例,它会发生什么变化。
为什么我只想拥有 class.
的一个实例
当您只需要整个应用程序共享的 class 的一个实例时,使用单例。
这个原则的很好的例子是 class负责访问外部资源的 es。例如,您希望整个应用程序共享相同的数据库连接(或至少连接池),而不是让每个需要它的 class 打开自己的连接。
单例 class 具有可以与同一上下文中的其他 class 共享的属性。 (应用程序、会话、...)
例如,如果您必须计算 Web 应用程序中已连接用户的数量。每次用户连接时,您都会在唯一共享 class.
中增加一个计数器
在某些情况下,我们需要在整个应用程序中公开共享资源,例如数据库连接,但我们不想
- 预先创建共享对象(在创建客户端对象之前)。
- 将共享对象显式传递给每个客户端对象。
那我们就可以使用Singleton设计模式了
典型的单例class看起来像
public class MySingleton {
private MySingleton INSTANCE
private MySingleton() {
}
public static MySingleton getInstance() {
if (INSTANCE == null) {
syncronized (MySingleton.class) {
if (INSTANCE == null) {
INSTANCE = new MySingleton();
}
}
}
return INSTANCE;
}
// instance methods exposing business operation
}
但是我们可以通过将每个公开业务操作的实例方法设为静态来实现类似的行为。在这种方法中,我们甚至不需要创建单个对象。
那我们为什么需要单例呢?
嗯,答案很简单。将实际实现与客户端隔离开来。基本上我们在这里应用 abstraction OOP 原则。
如果单例 class 是被各种客户端使用的库的一部分,并且库希望根据客户端改变实现,这很有用。
此类单例的示例可以是
public class MySingleton {
private MySingleton INSTANCE
private MySingleton() {
}
public static MySingleton getInstance() {
if (INSTANCE == null) {
syncronized (MySingleton.class) {
if (INSTANCE == null) {
INSTANCE = new MySingletonChild(); // here we are creating an object of subclass of MySingleton.
}
}
}
return INSTANCE;
}
// instance methods exposing business operation
}
希望这有助于理解单例设计模式。
我知道如何在java中制作单例class,但我不明白的是单例的概念。比如为什么我需要一个单例 class 以及为什么我要使用单例而不是常规 class?
"Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine."
我刚读了那个定义,但我不明白,如果 class 有一个或多个实例,它会发生什么变化。 为什么我只想拥有 class.
的一个实例当您只需要整个应用程序共享的 class 的一个实例时,使用单例。
这个原则的很好的例子是 class负责访问外部资源的 es。例如,您希望整个应用程序共享相同的数据库连接(或至少连接池),而不是让每个需要它的 class 打开自己的连接。
单例 class 具有可以与同一上下文中的其他 class 共享的属性。 (应用程序、会话、...) 例如,如果您必须计算 Web 应用程序中已连接用户的数量。每次用户连接时,您都会在唯一共享 class.
中增加一个计数器在某些情况下,我们需要在整个应用程序中公开共享资源,例如数据库连接,但我们不想
- 预先创建共享对象(在创建客户端对象之前)。
- 将共享对象显式传递给每个客户端对象。
那我们就可以使用Singleton设计模式了
典型的单例class看起来像
public class MySingleton {
private MySingleton INSTANCE
private MySingleton() {
}
public static MySingleton getInstance() {
if (INSTANCE == null) {
syncronized (MySingleton.class) {
if (INSTANCE == null) {
INSTANCE = new MySingleton();
}
}
}
return INSTANCE;
}
// instance methods exposing business operation
}
但是我们可以通过将每个公开业务操作的实例方法设为静态来实现类似的行为。在这种方法中,我们甚至不需要创建单个对象。
那我们为什么需要单例呢?
嗯,答案很简单。将实际实现与客户端隔离开来。基本上我们在这里应用 abstraction OOP 原则。
如果单例 class 是被各种客户端使用的库的一部分,并且库希望根据客户端改变实现,这很有用。
此类单例的示例可以是
public class MySingleton {
private MySingleton INSTANCE
private MySingleton() {
}
public static MySingleton getInstance() {
if (INSTANCE == null) {
syncronized (MySingleton.class) {
if (INSTANCE == null) {
INSTANCE = new MySingletonChild(); // here we are creating an object of subclass of MySingleton.
}
}
}
return INSTANCE;
}
// instance methods exposing business operation
}
希望这有助于理解单例设计模式。