Top 30 Java Design Pattern Interview Questions & Answers

Java Design Patterns is the core for building any robust and scalable web based application developed using Java. Java Design Patterns are the set of standards defined as best practices in the software industry. Design Patterns provides the common solution for the repetitive problems occurring again and again and helps in defining the standard approach for consumption in your code development.

The below given are the most commonly asked Design Pattern Interview Questions and answers in Job Interviews and other competitive entrance exams.

1. What is Gang of Four ?

The Java Design Patterns were first published in 1994 by Erich Gamma, Richard Hel , John Vlissides and Ralph Johnson who were computer scientists and authors by profession also known as “Gang Of Four”. The book published was titled “Design Patterns Elements of Reusable Object-Oriented Software” which provided the details on the best practices and the worst usage of Design Patterns.

2. What are the different categories in Java Design Patterns ?

The Java Design Patterns are classified into below given categories based on the problem analysis

Java Design Pattern CategoryJava Design Pattern Names
Creational Java Design PatternsFactory Pattern
Abstract Factory Pattern
Build Patterns
Prototype Pattern
Singleton Pattern
Structural Java Design Patterns Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Proxy Pattern
Facade Pattern
Flyweight Pattern
Filter Pattern
Behavioral Java Design Patterns Interpreter Pattern
Template Pattern
Chain of Responsibility Pattern
Command Pattern
Iterator Pattern
Strategy Pattern
Visitor Pattern
J2EE Java Design Patterns MVC Pattern
Data Access Object Pattern
Transfer Object Pattern
Intercepting Filter Pattern
Front Controller Pattern

3. What are the benefits of using Java Design Patterns ?

As Java Design Patterns provides the set standards or practices for the common problems , it provides the below advantages when used in any application development

  • Design Patterns provides the industry standard approach for the recurring problems experienced by developers using object oriented programming.
  • Design Patterns are reusable and reduces total cost of ownership (TCO)
  • Usage of Design Patterns reduces the code development , make code easy to understand and debug for issues.
  • Design Patterns usage makes applications scalable and robust.

4. Provide examples of design patterns getting used in JDK library ?

The below given are few of the examples

Design PatternUsage in JDK Library
Singleton Patternused in the Calendar Classes
Observer Pattern used for handling events in AWT, Swing
Factory Patternused by wrapper classes for converting Integer, String values
Decorator Patternused by wrapper classes

5. What are creational design patterns ?

The creational design patterns provides the mechanism of creating objects by instantiating the class object directly using the new keyword. It provides the abstraction on the object creation process.

Employee emp = new Employee();

The new Keyword creates the instance for the class Employee.

6. What is a singleton design pattern ?

The concrete class can have multiple instance as per the object invocation from other concrete classes but when we need to strict the class with only one instance to be existed in the Virtual Java Machine, then such class need to be created as singleton class.

Singleton Design Pattern restricts and ensure that class only 1 instance should exist for the class. The class using singleton pattern creates an object and ensure only single instance is returned.

Examples:

  • Database class can be considered as singleton class implementing the singleton design pattern.
  • Java JDK implements singleton design pattern for calendar class.

Singleton Pattern can be created in 2 ways:

  • Early Instantiation: which means the creation of singleton instance at the startup / load time
  • Lazy Instantiation : which means the creation of singleton instance at the run-time.

7. What is the difference between a static class and a singleton class ?

The below are the primary differences between a static and singleton class

Static ClassSingleton class
Static class members are all staticSingleton class is not bounded only with static members.
Static class are loaded at startupsingleton class can be loaded at startup or lazy loaded ( at run-time)
Static class cannot be the top level class and cannot implement interfaces A singleton class can be a top level class and can also implement interfaces
Static class is stored as stack in memory spacesingleton class is stored as Heap in memory space

8. Can a singleton class be cloned ?

Yes, singleton class can be cloned. However to prevent singleton class from cloning, we can throw exception within the body of the clone( ) method.

9. What is a Factory Pattern?

  • The Factory Pattern is one of the most popular and commonly used Creational Design Pattern.
  • Factory Pattern is used in JDK library, Spring and Struts frameworks.
  • The factory pattern ensures the class instantiation from the client program to the factory class.
  • The factory pattern is useful when the super class have multiple sub-classes and based on the object instantiation one of the sub-class to be returned.
  • Factory Pattern is also known as Virtual Constructor.

10. What is a Abstract Factory Pattern ?

  • Abstract Factory Pattern is part of the Creational Design Patterns and helps in creating objects.
  • Abstract Factory Pattern is similar to the Factory Pattern but it is considered as factory of factories.
  • Abstract Factory Pattern defines the abstract class for creating the objects of related objects and provides the abstraction about the concrete sub-classes.
  • Abstract Factory Pattern is one level up from factory pattern

11. What is a Builder Pattern ?

Builder Design Pattern provides the way for handling more number of fields including optional fields and their inconsistent state by providing the mechanism to manage and return the final object.

Let’s consider that you have employee data where each field need to set . Like Employee firstName, lastName, emailAddress, contact, residential address, etc; In the normal java practice, we set each field separately but with the usage of Builder Pattern we can combine the values and pass it as a common method to set those values.

12. What is a Prototype Pattern ?

  • Prototype Design Pattern provides the mechanism to copy the original object to a new object for modifications.
  • Prototype pattern internally uses java cloning for copying the object.
  • Prototype pattern are quite useful when object creation is costly.
  • Example: Database operations uses Prototype pattern where the cloned object is cached , allowed for modifications and update the database which overall reduces the database calls.

13. What is an Adapter Pattern?

  • The adapter design pattern is one of the structural design patterns.
  • The adapter design pattern provides the mechanism to combine the functionalities of multiple incompatible class to generate a reusable class.
  • The adapter design pattern acts as the wrapper.
  • Example: Mobile Memory Card is an external device but gets connected to the Mobile and allows you to extend the overall mobile memory by getting connected through the mobile memory reader.

14. What is a Bridge Pattern?

  • The bridge design pattern is one of the structural design patterns.
  • The bridge design pattern provides the mechanism to decouple the interface(s) from the implementation by hiding implementation details (abstraction) from the client calls.
  • The bridge design pattern ensure that the concrete classes are independent from interface implementation classes

15. What is a Facade Design Pattern ?

  • The facade design pattern is one of the structural design patterns.
  • The earlier implementations had the tight binding between client and database operations or other system calls. Facade design patterns provides the mechanism to hide the complexities between the system calls and provides interface to the client to access the required implementations.
  • Example: The methods exposed by facade patterns can be consumed by the client program and hides the implementation of the database operations

16. What is a Composite Design Pattern ?

  • The composite design pattern is one of the structural design patterns.
  • The composite design pattern provides the mechanism to combine the group of objects as a single object providing the common structure for all objects.
  • The composite design pattern contains group of objects and allows modifications on those objects.

17. What is a Flyweight Design Pattern ?

  • The flyweight design pattern is one of the structural design patterns.
  • The flyweight design pattern provides the mechanism for reducing the number of objects created for the class by sharing the existing objects.
  • In case of no existing object found, flyweight pattern then creates the new object.
  • The flyweight design pattern avoid high memory consumption and helps in enhancing performance.

18. What is a Decorator Design Pattern ?

  • The decorator design pattern is one of the structural design patterns.
  • The decorator design pattern allows to modify the functionality for an object at run-time without impacting its existing structure.
  • Example: You have the class Pizza which provides Pizza Type ( Thin Crust, Thick Crust). You have another class as PizzaToppings which includes ( ‘Tomato, Onions, Olives, Peanuts, Extra Cheese,,etc) The decorator class can combine the Pizza and PizzaToppings and returns the output.
  • Example: BufferedReader and BufferedWriter classes in java

19. What is a Proxy Design Pattern ?

  • The proxy design pattern is one of the structural design patterns.
  • The proxy design pattern provides the mechanism to control the access of implementation class by extending a new class for handling the operations. If the received request is validated, then proxy pattern invokes the original class to provide the functionality.
  • The proxy design pattern helps in hiding the details of actual implementation class to the outside world.
  • Example: SOA Architecture provides the OSB Proxy which receives the request and then invokes the original web service for the functionality.

20. What is a Chain Of Responsibility Design Pattern ?

  • The chain of responsibility design pattern is one of the behavioral design patterns.
  • The chain of responsibility design pattern provides the mechanism where the request from the client program is passed to the Chain of Objects for processing the request. The chain object validates the received request, assigns the request to the object for execution.
  • The chain of responsibility design pattern adds the decoupling between the sender and receiver of the request as per request type.

21. What is a Command Design Pattern ?

  • The command design pattern is one of the behavioral design patterns.
  • The command design pattern is a data driven design pattern in the request-response model.
  • Provides the mechanism where the request is sent to the invoker, invoker passes the request to the command object (encapsulated), the command object passes the request to the appropriate receiver for processing.
  • Example: The command object can provide implementation for the Open File , Save File , Delete File, etc by providing the File command Pattern for receiving the client program request for each of these operations.

22. What is an Interpreter Design Pattern ?

  • The interpreter design pattern is one of the behavioral design patterns.
  • The interpreter design pattern provides the interpreter to define the grammatical representation of the language.
  • Example: Java JVM which interprets the java source code and converts it into byte code.

23. What is an Iterator Design Pattern ?

  • The iterator design pattern is one of the behavioral design patterns.
  • The iterator design pattern provides the mechanism to traverse the elements of collection object in the sequential order.
  • Example: Java Collection Framework uses Iterator Pattern.

24. What is a Visitor Design Pattern ?

  • The visitor design pattern is one of the behavioral design patterns.
  • The visitor design pattern provides the mechanism to perform operations on the similar objects.
  • The visitor class changes the calculation algorithm on the element class
  • Example: User logged in to an electronics online shopping and adds Laptop , mobile charger, mouse. When check out is clicked, it returns the listing of items with the total price and discounts applied which is applied on top of the available items in the cart.

25. What is a Strategy Design Pattern ?

  • The strategy design pattern is one of the behavioral design patterns.
  • The strategy design pattern provides the mechanism to have multiple algorithm for a specific task and client program decides which algorithm to be invoked at run-time.

26. What is an Observer Design Pattern ?

  • The observer design pattern is one of the behavioral design patterns.
  • The observer design pattern provides the mechanism where the object state change is notified.
  • The object that watch on the state of another object are called Observer
  • The object that is being watched is called Subject
  • Example: Event handling in java for AWT , Swing, Java Message Services

27. What is MVC Design Pattern ?

  • MVC is the one of the most common design pattern used in building java based web applications.
  • MVC stands for Model-View-Controller Pattern
  • Model : model represents the Java POJO or an java object carrying application data
  • View: View represents the Front End representation of the data that is returned by model
  • Controller: handles the request and response for each user action. It controls the data flow into model object and returns the updates model to the view.

28. What is a Template Design Pattern ?

  • The template design pattern is one of the behavioral design patterns.
  • The template design pattern provides the mechanism where an abstract class provides the template for invoking methods.
  • The template methods could provide the default implementation which is common for all sub-classes or specific method invocation to the sub-classes

29. What is State Design Pattern ?

  • The state design pattern is one of the behavioral design patterns.
  • The state design pattern provides the mechanism where the class bhavior changes on the change of the state

30. what is Business Delegate Design Pattern ?

  • The business delegate design patterns decouples the presentation layer and business layer where
  • Client: manages the presentation or view layer using JSP, JSF, servlets
  • Business Delegation: provides entry point to the client program to access business service methods
  • Lookup Service: Lookup Service object provides the reference of the business object required for implementation as per client request received.
  • Business Service: is an interfaace for the Concrete classes to provide actual business implementation logic