这个问题在采访中问我,我试着想不出答案,我想要一段代码,用C++或JAVA来限制一个类的实例(对象)的数量。如何限制C++或JAVA中类的实例数量?
回答
使用工厂。保持发布实例数量的私有静态计数器。不要让您的实例限定类的构造函数可见。
什么是工厂? – khajvah
@ khajvah工厂是一种用来创建物体的方法,就像工厂是现实生活一样;) –
这是你应该谷歌的东西。那里有很多例子。 google:“java工厂设计模式”和“java工厂方法”。 –
尝试这样使用在static variable
C++: -
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;
}
}
这是如何防止任何事情? (别介意多线程......) –
@DanielFrey: - 我只是想说明一点,我们可以检测到有多少对象被创建,然后可能会试图限制它。如果我错了,请纠正我! :) –
@RahulTripathi至少让它'如果(count == N)抛出“达到极限”;'或什么回答这个问题。 –
正确答案是使用工厂方法,如果问题是如何做到这一点...... 通过使构造私人
一个非常简单的例子是:
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个对象。
只有在未为该类创建对象时才会调用构造函数,之后它将仅发送该引用。
希望这有助于
一个非常简单的解决方案
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
有了这个功能,我们可以添加其他方法和变量按规定类的
是的,我们可以限制情况下,高达一些特定的限制。这只是一个增强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内存。
- 1. 如何限制EJB实例的数量?
- 2. 如何限制PHP中的类实例?
- 3. 如何限制IIS中WCF服务的实例数量?
- 4. Symfony命令实例的限制数量
- 5. 从限制类对象实例的数量给定的数字
- 6. 将实例方法的参数限制为实例的类型
- 7. 限制不使用静态变量创建类的实例数
- 8. 我如何限制实例化对象的数量?
- 9. 如何限制访问实例的请求数量?
- 10. 限制编号。在Java中的类的实例
- 11. 在java和c + +中的实例变量
- 12. 根据表中实例的数量限制数据输入
- 13. 如何在C++中实例化一个成员变量中的子类实例?
- 14. 类限制JAVA与C#implemetation
- 15. Objective-C:类别中的实例变量
- 16. 如何限制实例在AWS中使用IAM服务启动实例类型
- 17. 如何限制Microsoft Word的实例?
- 18. C#一个类的无限实例
- 19. 在C++中得到一个Java类的实例变量JNI
- 20. 限制MDI应用程序中窗口实例的数量
- 21. 如何限制参数类的成员以匹配外部类的实例?
- 22. 如何从java中的类创建大量实例
- 23. 如何基于Java中的类参数实例化泛型类
- 24. 如何限制浮点数或定义数据类型的变量在php中
- 25. Java中的实例变量
- 26. 从Java中的抽象类实施实例变量的实现
- 27. AdMob实例限制
- 28. C++模板实例化限制
- 29. Ruby的attr_accessor如何生成类变量或类实例变量而不是实例变量?
- 30. 如何限制java接口在多个类中实现
'类都能跟得上{都能跟得上()=删除; };'将类限制为零实例;) –
使用保存所创建实例数量的构建器。尽管如此,这并没有反映出来。 –
这个问题是毫无意义的,除非有一个线程上下文并限制工作线程的数量。 +1 Daniel Frey –