You are on page 1of 2

Q) what are design pattrens are know?

Creational design pateren : singleton,factory mehtod,abstract factory,Prototype


J2EE design patteren : MVC

Q)Singleton Design patteren ?


1.singleton pattern restricts the instantiation of a class and ensures that
only one instance of the class exists in the java virtual machine
2.Singleton pattern is used for logging, drivers objects, caching and thread
pool.
Rules:
1.Private constructor to restrict instantiation of the class from other
classes.
2.Private static variable of the same class that is the only instance
of the class.
3.Public static method that returns the instance of the class, this is
the global access point for outer world to get the instance of the singleton class.

public class ThreadSafeSingleton {

private static ThreadSafeSingleton instance;

private ThreadSafeSingleton(){}

public static ThreadSafeSingleton


getInstanceUsingDoubleLocking(){
if(instance == null){
synchronized (ThreadSafeSingleton.class) {
if(instance == null){
instance = new
ThreadSafeSingleton();
}
}
}
return instance;
}
}
Q) How many ways We can break singleton design pattern ?
1.With Deserialization
Singleton obj1 = Singleton.getInstance();
System.out.println("Singleton.getInstance().hashCode: " + obj1.hashCode());

Field field = Singleton.class.getDeclaredField("singletonObject");


field.setAccessible(true);
field.set(field, null);

Constructor constructor = Singleton.class.getDeclaredConstructor();


constructor.setAccessible(true);
Singleton obj2 = (Singleton) constructor.newInstance();
System.out.println("Reflection.newInstance().hashCode: " +
obj2.hashCode());

Overcome reflection issue: To overcome issue raised by reflection,


enums are used because java ensures internally that enum value is instantiated only
once. Since java Enums are globally accessible, they can be used for singletons.
Its only drawback is that it is not flexible i.e it does not allow lazy
initialization.
public enum Singleton
{
INSTANCE;
}

As enums don’t have any constructor so it is not possible for


Reflection to utilize it. Enums have their by-default constructor, we can’t invoke
them by ourself. JVM handles the creation and invocation of enum constructors
internally. As enums don’t give their constructor definition to the program, it is
not possible for us to access them by Reflection also. Hence, reflection can’t
break singleton property in case of enums.

2.Serialization:
// Serialize the object to file
ObjectOutput objectOutput = new ObjectOutputStream(new
FileOutputStream("file.txt"));
objectOutput.writeObject(obj);
objectOutput.close();

// DeSerailize the object from file


ObjectInput objectInput = new ObjectInputStream(new
FileInputStream("file.txt"));
Singleton obj2 = (Singleton) objectInput.readObject();
in.close();
Overcome serialization issue:-
To overcome this issue, we have to implement method readResolve()
method.
// implement readResolve method
protected Object readResolve()
{
return instance;
}
Q)Factory design patteren ?
When object creation for a class is complex then we can seperate creation process
into a method and we can return the final object from that method.
1.static factory factory method
2.instace factory method.
ex: DriverManager.getConncetion(url,username,pasword)

Q) Abstract factory design patteren ?


1.it is super factory for factories
2.Abstact factory is a factory of factories,ie one level above to the factory
methd design patteren.

Q)Prototype Design pattrn?


Create multiple objects of such class then , we need to apply prototype design
patteren.
this design paterensays that first create a object byinitializing through
contructor and create remaining object thought cloning of first object ,
by this way object created fastly.

Design pattern: Design pattern are used to reduce the application memory and
execution time, so that performance of application will be improved.

You might also like