Introduction To Design Pattern Interview Questions And Answer
Design patterns are a well-described solution to the most commonly encountered problems which occur during software development. Design pattern represents the best practices evolved over a period of time by experienced software developers. They promote reusability, which leads to a more robust and maintainable code.
Design Patterns were first described in the book A Pattern Language by architect Christopher Alexander. Later they were described in Design Patterns: Elements of reusable object-oriented software written by four authors (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), also referred to as the “Gang of Four”.
The Design patterns can be classified into three main categories:
• Creational Patterns
• Behavioral Patterns
• Functional Patterns
So While Preparing for a job interview in Design. I am sure you want to know the most common 2021 Design Pattern Interview Questions and answers that will help you crack the Design Pattern Interview with ease. Below is the list of top Design Pattern Interview Questions and answers at your rescue.
Listed below are some of the commonly asked 2021 Design pattern interview questions:
Part 1 – Design Pattern Interview Questions (Basic)
1. What is Singleton Pattern in Java?
Singleton pattern is a creational pattern that allows only one instance of a class to be created, which will be available to the whole application. The major advantage of the Singleton design pattern is its saves memory because the single instance is reused again and again; there is no need to create a new object at each request. For example, in our application, we can use a single database connection shared by multiple objects instead of creating a database connection for every request.
2. What are the drawbacks of using a singleton design pattern?
The major drawbacks of using a singleton design pattern are:
a) Singleton causes code to be tightly coupled. The singleton object is exposed globally and is available to a whole application. Thus, classes using this object become tightly coupled; any change in the global object will impact all other classes using it.
b) They hide dependencies instead of exposing them.
c) Singleton Pattern does not support inheritance.
d) Singleton principle can be violated by techniques such as cloning. If an application is running on multiple JVM’s, then, in this case, Singleton might be broken.
3. Which design pattern will you use to create a complex object?
A builder design pattern is used to construct a complex object. It is designed to solve the issues with the factory and abstract design pattern.
4. Why use a factory class to instantiate a class when we can use a new operator?
Factory classes provide flexibility in terms of design. Below are some of the benefits of factory class:
•Factory design pattern results in more decoupled code as it allows us to hide creational logic from dependant code
•It allows us to introduce an Inversion of the Control container.
•It gives you a lot more flexibility when changing the application as our creational logic is hidden from dependant code.
5. What is the difference between a factory and an abstract factory design pattern?
Both factory and abstract factories are creational design patterns. The major difference between these two is, a factory pattern creates an object through inheritance and produces only one Product. On the other hand, an abstract factory pattern creates the object through composition and produce families of products.
Part 2 – Design Pattern Interview Questions (Advanced)
6. What is the observer design pattern in Java?
Answer: Observer design pattern is one of the behavioral design patterns that defines one-to-many dependencies between objects & is useful when we are interested in a state of an object. We want to get notified when there is any change in the state of the Object. In the Observer design pattern, when one object changes its state, all its dependent objects are automatically notified, the object is called Subject, and dependants are called Observers. Java provides libraries to implement Observer design pattern using java.util.Observable class & java.util.Observer interface.
7. Difference between strategy and state design pattern in Java?
Both Strategy, as well as a state design pattern, are similar in practice, but they have different implementation; the following are some of the major differences between these two:
a. The strategy design pattern defines a set of algorithms to carry out a specific behavior, whereas the State design pattern allows an object to alter its behavior when its internal state changes.
b. The strategy design pattern does not allow us to store a reference to the context object, whereas the state design pattern stores the reference to the context object which contains them.
c. In the strategy design pattern, the client is aware of the strategy which is chosen for implementation, whereas in the state design pattern, the client does not decide which state to be chosen for implementation.
d. Strategy pattern deals with HOW an object performs a certain task, whereas the state design pattern deals with what an object is.
e. There is no successor/predecessor relationship present in strategy design pattern, whereas states are related to one/another as successor & predecessor in state design pattern states.
8. What is the Null Object Pattern?
Null Object pattern is a design pattern in which the null object replaces the NULL check for the instance variable. Instead of putting a check for a null value, Null Object reflects a do nothing relationship. It can also be used to provide default behavior in case data is not available.
9. Give an example of a decorator design pattern?
The decorator pattern, also known as a structural pattern, is used to add additional functionality to a particular object at runtime. It wraps the original object through a decorator object. For example, when you are buying a burger, you can customize it by adding extra filling and sauces; now, the cost of these items has to be added to the final price. The customization will differ from customer to customer and offer from a shop. Creating different classes of burgers with different fillings will end up creating a lot of classes. The decorator solves this problem by extending the functionality of a single Burger class at runtime based on customer requests.
10. What is the benefit of using a prototype design pattern over creating an instance using the new keyword?
Sometimes, object creation is heavyweight and requires a lot of resources; creating a new instance will impact the performance. In such cases, a prototype design pattern is used, which refers to creating duplicate objects. If a similar object is already present in the prototype design pattern, then cloning is done, keeping performance in mind.
This has been a comprehensive guide to the Design Pattern Interview Questions and answers so that the candidate can crackdown these Design Pattern Interview Questions easily. You may also look at the following articles to learn more –