5

質問はインタビューで私に尋ねられました.私はそれに答えることができないと考えようとしました.クラスのインスタンス(オブジェクト)の数を制限するC++またはJAVAのコードが必要です.

4

5 に答える 5

10

工場を利用します。リリースされたインスタンス数のプライベート静的カウンターを保持します。インスタンス限定クラスのコンストラクターが表示されないようにしてください。

于 2013-09-26T19:11:32.163 に答える
6

C++static variableで aを使用して、このように試してください:-

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

オブジェクトが作成されるたびに、count 変数が 1 ずつ増加します。

ジャワで

サンプル実装は次のようになります:-

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;
    }
}
于 2013-09-26T19:13:05.933 に答える
2

はい、クラスのインスタンスを特定の制限まで制限できます。これは、 Singleton Design Patternの機能拡張にすぎません

コンストラクターを非公開にしてから、可視のコンストラクター メソッドを作成することで、インスタンスの作成数を制限したり (シングルトン デザイン パターンで行っているように)、インスタンスやその他の構築関連のタスクをリサイクルしたりできます。

new 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/

于 2017-07-09T11:27:23.657 に答える