2013-09-26 35 views
4

这个问题在采访中问我,我试着想不出答案,我想要一段代码,用C++或JAVA来限制一个类的实例(对象)的数量。如何限制C++或JAVA中类的实例数量?

+3

'类都能跟得上{都能跟得上()=删除; };'将类限制为零实例;) –

+0

使用保存所创建实例数量的构建器。尽管如此,这并没有反映出来。 –

+0

这个问题是毫无意义的,除非有一个线程上下文并限制工作线程的数量。 +1 Daniel Frey –

回答

9

使用工厂。保持发布实例数量的私有静态计数器。不要让您的实例限定类的构造函数可见。

+0

什么是工厂? – khajvah

+0

@ khajvah工厂是一种用来创建物体的方法,就像工厂是现实生活一样;) –

+1

这是你应该谷歌的东西。那里有很多例子。 google:“java工厂设计模式”和“java工厂方法”。 –

4

尝试这样使用在static variableC++: -

struct myclass 
{ 
    myclass() 
    { 
    if(count == N) { /*throw some exception here!*/ } 
    ++count; 
    } 
    ~myclass() 
    { 
    --count; 
    } 
private: 
    static std::size_t count = 0; 
}; 

每当创建了对象,则计数变量按1

递增在JAVA

甲样本实施可能是这样的: -

public class MyClass { 
    private static final int LIMIT = 10; //Set this to whatever you want to restrict 
    private static int count = 0; 
    private MyClass() {} 
    public static synchronized MyClass getInstance() { 
     if (count < LIMIT) { 
      MyClass myClass = new MyClass(); 
      count++; 
      return myClass; 
     } 
     return null; 
    } 
} 
+1

这是如何防止任何事情? (别介意多线程......) –

+0

@DanielFrey: - 我只是想说明一点,我们可以检测到有多少对象被创建,然后可能会试图限制它。如果我错了,请纠正我! :) –

+1

@RahulTripathi至少让它'如果(count == N)抛出“达到极限”;'或什么回答这个问题。 –

1

正确答案是使用工厂方法,如果问题是如何做到这一点...... 通过使构造私人
一个非常简单的例子是:

class SingletonClass{ //i.e class with only one instance 
private int a; 
private SingletonClass() // here you see constructor is private 
{} 
private static SingletonClass ref; // this is static reference of the class 
public static SingletonClass getInstance(){ //this is the factory method 
if(ref==null) 
ref=new SingletonClass(); 
return ref; 
} 
public void setA(int a){ 
this.a = a; 
} 
public int getA(){ 
return a; 
} 
} 
class Demo{ 
public static void main(String []s){ 
SingletonClass s,p; // 2 references for the SingletonClass 
s = new SingletonClass(); /*this will generate compile time error since contructor is      private */ 
s = SingletonClass.getInstance(); 
p = SingletonClass.getInstance(); 
s.setA(10); 
p.setA(20); 
System.out.println(s.getA()+" "+p.getA()); 
} 
} 

这里输出将是,因为两个参考指向相同的对象。
实际上对象不是,除非构造函数被称为(这就是为什么它被称为构造函数),如果我们使构造函数成为私有的,那么我们可以限制对象的创建,然后用工厂方法来控制创建,就像我们想要的那样在这种情况下,它只允许创建1个对象。
只有在未为该类创建对象时才会调用构造函数,之后它将仅发送该引用。
希望这有助于

0

一个非常简单的解决方案

class XYZ { 
private statuc XYZ[] instance; 
public XYZ[] getInstance(){ 

if (instance == null) { 

instance = new XYZ[4] ; // here I am restricted to make only 4 inxtance 

}// if ends here 
return instance; 

}// getinstance() ends here 

}// class ends here 

有了这个功能,我们可以添加其他方法和变量按规定类的

1

是的,我们可以限制情况下,高达一些特定的限制。这只是一个增强Singleton Design Pattern

通过使我们的构造函数私人,然后创建一个可见的构造方法,我们可以限制实例创建的数量(像我们在单身设计模式中做的那样)或回收实例或其他建筑相关的任务。

做新的x()永远不会返回null,但使用工厂模式,您可以返回null。

下面是相同的Java实现:

public class LimitObjectCreationTest { 

     public static void main(String[] args) { 

     LimitClass obj; 
     int i=1; 
     while(i<=20) 
     { 
      obj = LimitClass.getLimInstance(); 
      i++; 
     } 
     } 
} 


class LimitClass { 

    /** 
    * count of alive instances in JVM 
    */ 
    public static int objCount = 0; 

    /** 
    * private constructor 
    * increases the objCount static variable on every object creation 
    */ 
    private LimitClass(){ 
     objCount++; 
     System.out.println("instance " + objCount + " created"); 
    } 

    /** 
    * static factory method to return LimitClass instance 
    * @return instance of LimitClass if not reached to threshold, else returns null 
    */ 
    public static synchronized LimitClass getLimInstance() { 
     if(objCount < 3){ 
      return new LimitClass(); 
     } 
     System.out.println("Instance Limit reached. Can not create new Object"); 
     System.gc(); 
     return null; 
    } 

    /** 
    * decreases the objCount static variable when JVM runs garbage collection 
    * and destroys unused instances 
    */ 
    @Override 
    public void finalize() 
    { 
     System.out.println("Instance destroyed"); 
     objCount--; 
    } 
} 

输出

instance 1 created 
instance 2 created 
instance 3 created 
Instance Limit reached. Can not create new Object 
Instance Limit reached. Can not create new Object 
Instance destroyed 
Instance Limit reached. Can not create new Object 
Instance destroyed 
instance 3 created 
Instance destroyed 
instance 2 created 
instance 3 created 
Instance Limit reached. Can not create new Object 
Instance Limit reached. Can not create new Object 
Instance destroyed 
Instance Limit reached. Can not create new Object 
Instance destroyed 
instance 3 created 
Instance destroyed 
instance 3 created 
instance 3 created 
Instance Limit reached. Can not create new Object 
Instance Limit reached. Can not create new Object 
Instance destroyed 
Instance Limit reached. Can not create new Object 
Instance destroyed 
Instance destroyed 
instance 1 created 
instance 2 created 

输出可能会因您的分配JVM内存。

参考:http://codepumpkin.com/use-private-constructor-java/