[英]Creating class static getter instead of object link
When you have many classes, it is often necessary to get access to fields/methods of one class from the other one. 当您有很多类时,通常必须从另一类中访问一个类的字段/方法。 I'm not very experienced with Java and I've recently found this method which seem to be very convenient (better than passing variables from one class to another):
我对Java不太熟悉,最近发现这种方法似乎非常方便(比将变量从一个类传递到另一类更好):
class MyClass {
private static MyClass _instance = null;
public MyClass(){
...
}
public static getInstance(){
if (_instance == null) {
_instance = new MyClass();
}
return _instance;
}
}
Now in any other class I can simply do 现在在任何其他班上我都可以做
MyClass.getInstance().callSomething()
However I do not see such way of accessing other classes in Java examples and tutorials that I find in the Internet. 但是,在Internet上找到的Java示例和教程中,我看不到这种访问其他类的方式。 So that got me thinking, are there maybe some potential problems with such approach?
所以这让我开始思考,这种方法是否可能存在一些潜在的问题? Especially when you have many classes and hence many such static calls in each?
尤其是当您有很多类,因此每个类中都有很多此类静态调用时?
MyClass.getInstance().callSomething()
MyClass.getInstance()。callSomething()
However I do not see such way of accessing other classes in Java examples and tutorials that I find in the Internet.
但是,在Internet上找到的Java示例和教程中,我看不到这种访问其他类的方式。
The approach you have specified is one of the most commonly used design pattern called Singleton pattern. 您指定的方法是最常用的设计模式之一,称为Singleton模式。
In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java Virtual Machine. 在Java中,Singleton模式将确保在Java虚拟机中仅创建一个类的实例。 It is used to provide global point of access to the object.
它用于提供对对象的全局访问点。 In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.
在实际使用中,Singleton模式用于日志记录,缓存,线程池,配置设置,设备驱动程序对象。 Design pattern is often used in conjunction with Factory design pattern.
设计模式通常与Factory设计模式结合使用。
However, your code does not seem to fulfill the contract of Singleton Design pattern. 但是,您的代码似乎不符合Singleton Design模式的约定。 It primarily requires your class to have only private constructors (to avoid other classes from instantiating it).
它主要要求您的类只有私有构造函数(以避免其他类实例化它)。
public MyClass(){ // Constructor needs to be private
...
}
Also the getInstance()
method does not even seem to be valid. 同样,
getInstance()
方法甚至似乎无效。 Try this: 尝试这个:
public static MyClass getInstance(){
It is recommended to implement Singleton in Java 5 or above version using Enum . 建议使用Enum在Java 5或更高版本中实现Singleton。 Enum is thread safe and implementation of Singleton through Enum ensures that your singleton will have only one instance even in a multithreaded environment
Enum是线程安全的,并且通过Enum实现Singleton可以确保即使在多线程环境中 ,Singleton也只有一个实例。
Follow this link for detailed understanding of Singleton pattern using Enum 点击此链接可详细了解使用Enum的Singleton模式
Here is a good link which can help you understand Singleton pattern in detail. 这是一个很好的链接,可以帮助您详细了解Singleton模式。
This design pattern is called Singleton, have a look at the following question. 这种设计模式称为Singleton,请看以下问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.