简体   繁体   English

Java单一实例意味着

[英]Java single instance means

"In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java". “在Java中,Singleton模式将确保在Java中仅创建一个类的实例”。

I am not able to understand single instance means here. 我在这里无法理解单一实例的意思。

For example : 例如 :

 A  a = new A();

here what is a ? 这是什么? is it object or instance? 它是对象还是实例? if a is instance, does it mean that we can't do like below. 如果是is实例,是否表示我们不能像下面这样。

A b = new A() ie another instance 'b' A b = new A()即另一个实例“ b”

any diagrammatic example would help me. 任何图表示例都会对我有所帮助。

Thanks 谢谢

In your example, a is a reference to an instance of A , that got created by calling new A() . 在您的示例中, a是对A实例的引用,该实例是通过调用new A()创建的。

You can very well do A b = new A(); 您可以很好地做到A b = new A(); , this means that now you have two instances of A around. ,这意味着现在您周围有两个A实例。

And an object means the same as an instance. 对象的含义与实例相同。

a there is a reference to the newly created object that new A() creates. a引用了new A()创建的新创建的对象。 That has nothing to do with the singleton pattern, though; 但是,这与单例模式无关。 that's always true. 总是如此。

In the singleton pattern, you would find a way of ensuring that new A() is only called once ever in the program. 在单例模式中,您将找到一种确保new A()在程序中仅被调用一次的方法。 The typical way this is done is to make A 's constructor private, and create the instance from within A (which is thus the only class allowed to call the private constructor). 完成此操作的典型方法是将A的构造函数设为私有,然后从A内创建实例(因此,这是唯一允许调用私有构造函数的类)。

public class A {
    public static final A instance = new A();

    private A() {}

}

This approach does have a few edge cases, though: you can create extra instances through reflection, and through serialization if A is serializable. 但是,这种方法确实有一些极端的情况:您可以通过反射和序列化(如果A可序列化)来创建额外的实例。 To really make a singleton, the best way in Java is to use enums: 真正实现单例,Java中最好的方法是使用枚举:

public enum A {
    INSTANCE;
    // fields, methods etc go here
}

The JVM will ensure that only one A.INSTANCE exists in the process. JVM将确保该过程中仅存在一个A.INSTANCE

这里的'a'是指向创建的'new A()'引用。一旦创建了一个对象,您就无法在使用单例模式时创建其他对象。使用A b = new A()是错误的。

objects ARE instances. 对象是实例。 instances (= a case or particular example) of classes, which are some things written in programming language that define the semantics of objects themselves. 类的实例(=一个案例或一个特定的例子),它们是用编程语言编写的一些东西,它们定义了对象本身的语义。 When you use a singleton you define a class with a private constructor, this means that only instances of that class can call the constructor, so only instances of that class can create other instances of that class. 使用单例时,您将使用私有构造函数定义一个类,这意味着只有该类的实例才能调用该构造函数,因此只有该类的实例才能创建该类的其他实例。 the next step is to write a method that says. 下一步是编写一个说的方法。 "do you want a reference to an instance of this object? well, if anyone asked this to me before, i'll create it and then i'll give it to you, or rather i give to you an instance already created". “您要引用该对象的实例吗?好吧,如果有人以前问过我,我会先创建它,然后再交给您,或者我给您一个已经创建的实例”。

So now you are asking "wait, if i need an instance of this class to get an instance of this class, how can i get an instance of this class without an instance of this class??!?". 因此,现在您正在问“等等,如果我需要该类的实例来获取该类的实例,我如何在没有该类的实例的情况下获取该类的实例?!?”。 therese a simple answer to a complicated question: use static methods and fields 那里是一个复杂问题的简单答案:使用静态方法和字段

static code does not belong to any instance of the class, static variables and static method code are stored in other zones of address space, so you can use it before you have an instance of the singleton class 静态代码不属于该类的任何实例,静态变量和静态方法代码存储在地址空间的其他区域中,因此您可以在拥有单例类的实例之前使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM