Top 50 Java Design Pattern Interview Questions
Top 50 Java Design Pattern Interview Questions
Java
Design-Pattern
Interview Questions & Answers
Knowledge Powerhouse
No part of this book can be copied in any form. The publisher and the author have used good
faith efforts to ensure that the information in this book is correct and accurate. The
publisher and the author disclaim all responsibility for errors or omissions. Use of the
information in this book is at your own risk.
www.KnowledgePowerhouse.com
DEDICATION
To our readers!
CONTENTS
ACKNOWLEDGMENTS
We thank our readers who constantly send
feedback and reviews to motivate us in creating
these useful books with the latest information!
INTRODUCTION
For a client, at runtime we can vary the algorithm based on the type
of request we have received.
E.g. In real life, students are waiting for the result of their test. Here
students are the observers and test is the subject. Once the result of
test is known, testing organization notifies all the students about
their result.
Main issue with Observer pattern is that it can cause memory leaks.
The subject holds a strong reference to observers. If observers are
not de-registered in time, it can lead to memory leak.
3. What are the examples of Observer
design pattern in JDK?
In JDK there are many places where Observer design pattern is
used. Some of these are as follows:
1. java.util.Observer, java.util.Observable
2. javax.servlet.http.HttpSessionAttributeListener
3. javax.servlet.http.HttpSessionBindingListener
5. javax.faces.event.PhaseListener
4. How Strategy design pattern is
different from State design pattern in
Java?
State design pattern is a behavioral design pattern that is use for
defining the state machine for an object. Each state of an object is
defined in a child class of State class. When different actions are
taken on an Object, it can change its state.
E.g.
Open a FileInputStream:
Add buffering:
Add Gzip:
Add Serialization:
With Singleton pattern we can be sure that there is only one instance
of a class at any time in the application.
Similarly there is gc() method that can run the Garbage Collector.
With only one copy of gc() method, we can ensure that no other
object can run the Garbage Collector when one instance of GC is
already running.
Due to all these reasons there is only one copy of Runtime in Java.
To ensure single copy of Runtime, it is implemented as a Singleton
in Java.
10. What is the way to implement a
thread-safe Singleton design pattern
in Java?
In Java there are many options to implement a thread-safe Singleton
pattern. Some of these are as follows:
Sample code:
class DoubleCheckSingleton {
private volatile HelloSingleton helloSingleton; // Use Volatile
}
2. Bill Pugh Singleton: We can also use the method by Bill
Pugh for implementing Singleton in Java. In this we use an
Inner Static class to create the Singleton instance.
Sample code:
// Private constructor
private SingletonBillPugh(){}
Sample Code:
INSTANCE;
public static void doImplementation(){
…..
}
}
11. What are the examples of
Singleton design pattern in JDK?
In JDK there are many places where Singleton design pattern is
used. Some of these are as follows:
java.awt.Desktop.getDesktop()
12. What is Template Method design
pattern in Java?
It is a behavioral design pattern. We can use it to create an outline
for an algorithm or a complex operation. We first create the skeleton
of a program. Then we delegate the steps of the operation to
subclasses. The subclasses can redefine the inner implementation of
each step.
We can use same algorithm for Chess game with same set of
abstract methods. The subclass for Chess game can provide the
concrete implementation of methods like initializeGame(),
makeMove(), endGame() etc.
Java.lang.Class.forName()
java.net.URLStreamHandlerFactory.createURLStreamHandler(String)
java.util.Calendar.getInstance()
java.util.ResourceBundle.getBundle()
java.text.NumberFormat.getInstance()
java.nio.charset.Charset.forName()
java.util.EnumSet.of()
javax.xml.bind.JAXBContext.createMarshaller()
15. What is the benefit we get by using
static factory method to create
object?
By using Static Factory Method we encapsulate the creation process
of an object. We can use new() to create an Object from its
constructor. Instead we use static method of a Factory to create the
object. One main advantage of using Factory is that Factory can
choose the correct implementation at runtime and create the right
object. The caller of method can specify the desired behavior.
3. javax.swing.GroupLayout.Group.addComponent(): We can
use addComponent() method to build a UI that can contain
multiple levels of components.
4. java.lang.Appendable
javax.xml.xpath.XPathFactory.newInstance()
javax.xml.parsers.DocumentBuilderFactory.newInstance()
javax.xml.transform.TransformerFactory.newInstance()
18. What are the examples of
Decorator design pattern in JDK?
In JDK there are many places where Decorator design pattern is
used. Some of these are as follows:
In JDK there are many places where Proxy design pattern is used.
Some of these are as follows:
java.lang.reflect.Proxy
java.rmi.*
javax.inject.Inject
javax.ejb.EJB
javax.persistence.PersistenceContext
20. What are the examples of Chain of
Responsibility design pattern in JDK?
In JDK there are many places where Chain of Responsibility design
pattern is used. Some of these are as follows:
java.util.Pattern
java.text.Normalizer
java.util.concurrent.ExecutorService
In JDK there are many places where Visitor design pattern is used.
Some of these are as follows:
javax.lang.model.element.AnnotationValue and
AnnotationValueVisitor
javax.faces.component.visit.VisitContext and
VisitCallback
27. How Decorator design pattern is
different from Proxy pattern?
Main differences between Decorator and Proxy design pattern are:
javax.xml.bind.annotation.adapters.XmlAdapter.marshal()
33. What is the difference between
Factory and Abstract Factory design
pattern?
With Factory design pattern we can create concrete products of a
type that Factory can manufacture. E.g. If it is CarFactory, we can
produce, Ford, Toyota, Honda, Maserati etc.
This will ensure that no one can use clone() method or Cloneable
interface to create more than one instance of Singleton object.
44. What is the use of Interceptor
design pattern?
Interceptor design pattern is used for intercepting a request. Primary
use of this pattern is in Security policy implementation.
If you enjoyed this book and gained knowledge from it in any way, then I’d like
to ask you for a favor. Would you be kind enough to leave a review for this
book on Amazon.com?