因为Singleton Design Pattern只实例化一个对象,所以在下面的程序中创建了多少个对象?

时间:2016-02-07 02:32:13

标签: java singleton

//SingletonObject.java

package Test;

public class SingletonObject {

    private SingletonObject() {

    }

    private static SingletonObject instance = new SingletonObject();
    private static SingletonObject instance1 = new SingletonObject();

    public static SingletonObject getInstance(){
        return instance;

    }

    public static SingletonObject getInstance1(){
        return instance1;
    }

    public void showMessage(){
        System.out.println("Hello World !!! ");
    }

    public void dispMsg(){
        System.out.println("Hai");
    }

}

//SingletonObjectDemo.java

package Test;

public class SingletonObjectDemo {
    public static void main(String[] args){

        //SingletonObject obj = new SingletonObject();

        SingletonObject obj = SingletonObject.getInstance();
        obj.showMessage();

        SingletonObject obj1 = SingletonObject.getInstance1();
        obj1.showMessage();
        obj1.dispMsg();

    }

}

3 个答案:

答案 0 :(得分:1)

<强>两个即可。由于您的Singleton未正确实施(您可以对构造函数进行本地访问,实例应为final)。你可以改变它,

private static final SingletonObject instance = new SingletonObject();
private static final SingletonObject instance1 = instance;

答案 1 :(得分:1)

实现单例的方法有多种我通常使用的方法是:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   int counter = 0;
   double array[POINTS];

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   increments = (abs(xmin) + xmax) / POINTS;

   double x = xmin + increments * counter;
   double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');

   while (x <= xmax)
   {
      cout << fixed << showpos << setw(15) << setprecision(2) << x <<     setw(15)
     << setprecision(4) << 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) << endl;
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
         max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
         min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      counter++;
      x = xmin + increments * counter;

   } // End of while (x <= xmax)

   system("pause");
   return 0;
} // End of function main()

并将您的默认构造函数设为私有:

public static SingletonObject getInstance()
{
   if ( instance == null )
   {
      instance = new SingletonObject();
   }
   return instance;

}

有了这个,你可以保证只返回一个SingletonObject

的实例

答案 2 :(得分:0)

定义单例的最简单方法是使用enum

public enum SingletonObject {
    INSTANCE;

    public void showMessage(){
        System.out.println("Hello World !!! ");
    }

    public void dispMsg(){
        System.out.println("Hai");
    }
}

如果你想像在你的例子中那样定义两个对象,你可以给它另一个枚举值。