Java Design Patterns Interview Questions and Answers

In this article, we will discuss the Java design patterns interview questions asked for experienced Java developers.

Let's discuss most useful and frequently asked interview questions of Java/Java EE design patterns.

1. Can you name few design patterns used in standard JDK library?

Here is a list of design patterns used Java's core libraries.
Abstract factory
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Builder
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Factory method
Define an interface for creating an object, but let subclasses decide which classto instantiate. Factory Method lets a class defer instantiation to subclasses.
Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Singleton
Ensure a class only has one instance, and provide a global point of access to it.
Read complete list of design patterns used in standard JDK libraries at Examples of GoF Design Patterns in Java's core libraries

2. What design patterns are used in Spring framework?

Here is a list of few known design patterns used in Spring Framework.
Read more about each of the above design patterns in-detail at Design Patterns used in Spring Framework

3. What are design patterns used in Hibernate Framework?

Here is a list of few known design patterns used in Hibernate Framework.

Read more about each of the above design patterns in-detail at Design Patterns used in Hibernate Framework

4. What are the design patterns used in your current projects?

You can explain the design patterns you have been using in yours with real-time scenarios.

5. What is a Singleton design pattern in Java? write code for thread-safe singleton in Java


6. What are the advantages of the Factory Pattern and when to use the Factory Pattern?

Advantages of the Factory Design Pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

Use the Factory Method pattern when

  • a class can't anticipate the class of objects it must create
  • a class wants its subclasses to specify the objects it creates
  • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
Read more about Factory design pattern at Factory Design Pattern in Java with Examples

7. Name few Object-Oriented Design Principles

  1. Encapsulate What Varies
  2. Code to an Interface Rather Than to an Implementation
  3. Delegation Principle
  4. The Open Closed Principle (OCP)
  5. Dry- Don't Repeat Yourself
  6. Single Responsibility Principle (SRP)
  7. Liskov's Substitution Principle (LSP)
  8. Interface Segregation Principle (ISP)
  9. Dependency Injection or Inversion Principle

8. What is the use of Design Patterns?

  • Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.
  • Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.
  • In addition, patterns allow developers to communicate using well-known, well-understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.

9. Name few GOF design patterns?

10. Name few J2EE desgin patterns

List of Core J2EE Design Patterns referred from book Core J2EE Patterns: Best Practices and Design Strategies (2nd Edition).

11. What is Singleton design pattern and when to use it?

Definition

Ensure a class only has one instance, and provide a global point of access to it.

Use the Singleton pattern when

  • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
  • when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

12. What is the Builder design pattern and when to use it?

Definition

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Use the Builder pattern when

  • the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
  • the construction process must allow different representations for the object that's constructed.




Comments

  1. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    WordPress development company in Chennai

    ReplyDelete

Post a Comment