ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How do you prevent for creating another instance of Singleton using clone() method in java?

user-image
تم إضافة السؤال من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
تاريخ النشر: 2014/04/23

The class will need to implement the clone() mehod as below:

 

public class Singleton {

 

    private static final Singleton singleton = new Singleton();

    private Singleton() { }

    public static Singleton getInstance(){

        return singleton;

    }

    @Override

    protected Object clone() throws CloneNotSupportedException {

        throw new CloneNotSupportedException("Cloning of this class is not allowed"); 

    }

}

Maria Thomas
من قبل Maria Thomas , Tech Lead , J P Morgan Chase

If your class which is based on the Singleton pattern does not implement the Cloneable interface then the clone() method cannot be called on that object. 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟