有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

c#如何使类只允许创建3个对象

我在采访中被问到这个问题,如何创建一个只能创建3个对象的类

我建议使用一个静态变量和一个静态函数来创建对象,在返回新对象的引用时,只需检查静态变量中的值,以检查已创建对象的数量

第二种方法,我告诉他只在那个类中使用3个相同类的静态对象,让用户只使用这些对象

请告诉我执行上述操作的最佳方法

谢谢


共 (6) 个答案

  1. # 1 楼答案

    像这样的。。。简单的单身模式。。。可以通过getter为这三个实例添加直接访问

    import java.util.ArrayList;
    import java.util.Collections;
    
    public class ThreeInstanceClazz {
    
        private static ArrayList<ThreeInstanceClazz> instances= null;
    
        private ThreeInstanceClazz(){
            // Only a private constructor!!
        }
    
        public static ThreeInstanceClazz getInstance(){
            if(instances == null){
                instances = new ArrayList<ThreeInstanceClazz>(3);
            }
    
            if(instances.size() < 3){
                ThreeInstanceClazz newOne = new ThreeInstanceClazz();
                instances.add(new ThreeInstanceClazz());
    
                // return newly created
                return newOne;
            }else{
                // return random instance ... or anything else
                Collections.shuffle(instances);
                return instances.get(0);
            }
        }
    }
    
  2. # 2 楼答案

    第一步是编写自己的虚拟机。反思可以用来回避这里的大多数答案,一个定制的类加载器可以解决所有问题。这个故事的寓意是:拥有足够特权的代码可以对类执行任何操作

  3. # 3 楼答案

    我的实现如下,它并不完美,但我最初的想法

     public class ThreeInstances
    {
    
        private static int TOTALINSTANCECOUNT = 0;
    
        private ThreeInstances()
        {
        }
    
         private object objLock = new object();
    
         private static List<ThreeInstances> objThreeInstances = new List<ThreeInstances>();
    
         public static ThreeInstances GetInstance()
         {
    
             if (TOTALINSTANCECOUNT < 3)
             {
                 lock (objLock)
                 {
                     objThreeInstances.Add(new ThreeInstances());
                     Interlocked.Increment(ref TOTALINSTANCECOUNT);
                     return objThreeInstances[TOTALINSTANCECOUNT];
    
                 }
    
             }
             else
             {
                 Random r = new Random(0);
              int value =    r.Next(2);
              return objThreeInstances[value];
             }
    
    
         }
    
         ~ThreeInstances()
         {
             Interlocked.Decrement(ref TOTALINSTANCECOUNT);
    
             if (TOTALINSTANCECOUNT < 3)
             {
                 lock (objLock)
                 {
                     objThreeInstances.Add(new ThreeInstances());
                     Interlocked.Increment(ref TOTALINSTANCECOUNT);
                     return objThreeInstances[TOTALINSTANCECOUNT];
    
                 }
    
             }
         }
    }
    
  4. # 4 楼答案

    单例设计模式经过一些修改,两个实例支持三个实例,而不是一个

  5. # 5 楼答案

    在Java中,最简单的解决方案是使用枚举

  6. # 6 楼答案

    我认为您的第一种方法更接近这个问题:生成私有构造函数和静态newInstance()方法,该方法检查之前创建了多少个实例,如果超过3个,则返回null

    不过,第二种方法也可以

    编辑@Saurabh:尽管这个问题没有说明在对象被gc加密的情况下该怎么做,但让我们来发展一下:

    1. 对第一种解决方案的恶意攻击:重写finalize()方法,减少对象的静态计数器
    2. 一个对象池,带有某种锁定机制,只允许三个用户同时使用这些对象