Live Classes: Upskill your knowledge Now!
Chat NowPublished - Tue, 06 Dec 2022
Design patterns are generally sets of standardized practices used in the software development industry. Design Patterns represent the solutions given by the community to general problems faced in every-day tasks regarding software development.
Let's have a look at the most frequently asked design pattern interview questions and answers. These questions will help you with your coding interviews as well as competitive exams.
Based on problem analysis, we can categorize design patterns into the following categories.
Creational patterns
Structural patterns
Behavioral patterns
J2EE patterns
In 1994, four authors Erich Gamma, Ralph Johnson, Richard Hel, and John Vlissides published a book titled Design Patterns Elements of Reusable Object-Oriented Software. This book introduced the concept of Design Pattern in Software development.
These four authors are known as Gang of Four GOF.
Creational design patterns are related to the way of creating objects. Creational design patterns are used when a decision is made at the time of instantiation of a class.
Since new keyword is used to create an object in Java, So, here we are creating the instance using the new keyword. In some cases, the nature of the object must be changed according to the nature of the program. In such cases, we should use the creational design patterns to provide a more general and flexible approach.
Abstract Factory Pattern states that define an abstract class or interface for creating families of related objects but without specifying their concrete sub-classes. That means Abstract Factory allowed a class to return a factory of classes. That is why the Abstract Factory Pattern is one level higher than the Factory Pattern.
Structural patterns are used to provide solutions and efficient standards regarding class compositions and object structures. They depend on the concept of inheritance and interfaces to allow multiple objects or classes to work together and form a single working whole.
Structural design patterns are responsible for how classes and objects can be composed to form larger structures.
Singleton pattern in Java is a pattern which allows a single instance within an application. One good example of the singleton pattern is java.lang.Runtime.
Singleton Pattern states that define a class that has only one instance and provides a global point of access to it.
In other words, it is the responsibility of the class that only a single instance should be created, and all other classes can use a single object.
There are two ways of creating a Singleton pattern.
1. Early Instantiation
It is responsible for the creation of instance at load time.
2. Lazy Instantiation
It is responsible for the creation of instance when required.
Adapter pattern converts the interface of a class into another interface based on the requirement.
In other words, it let you convert the interface according to requirement while using the class service with a different interface.
It is also known as Wrapper.
It is used in the following cases:
The following points should need to be taken care to describe the design pattern.
The decorator pattern is one of the popular Java design patterns. It is common because of its heavy usage in java.io (package). The Decorator Pattern uses composition in place of inheritance to extend the functionality of an object at runtime.
BufferedReader and BufferedWriter are some excellent examples of decorator pattern in Java.
This question is a commonly asked Java design pattern interview question as both Strategy and State pattern has the same structure. The UML class diagram of both patterns looks precisely the same, but their intent is different.
The state design pattern is used to manage and define the state of an object, while the Strategy pattern is used to describe a set of an interchangeable algorithm.
Composite design pattern allows clients to operate collectively on objects that may or may not represent a hierarchy of objects.
Advantage of composite design patterns is as follows.
It is used in the following cases:
Some of the design patterns which are used in the JDK library are as follows.
Advantages of builder design patterns are as follows.
There are many ways to write a Thread-safe singleton in Java.
Yes, it is possible to create a clone of a singleton object.
The term Proxy stands for an object representing another object. The proxy pattern provides a substitute or placeholder for another purpose to control access to it.
According to Gangs of four, a Proxy Pattern "provides control for accessing the original object."
We can perform many security operations like hiding the information of the original object, on-demand loading, etc.
It is also called as placeholder or surrogates.
There are many cases where the proxy pattern is beneficial. Let's have a look at some different proxies.
Protection proxy
It controls access to the real subject based on some condition.
Virtual proxies
Virtual proxies are used to instantiate the expensive object. The proxy manages the lifetime of the real subject in the implementation.
It decides the need for the instance creation and when to reuse it. Virtual proxies optimize performance.
Caching proxies
Caching proxies are used to cache expensive calls to the real subject. There are many caching strategies that the proxy can use.
Some of them are read-through, write-through, cache-aside, and time-based. The caching proxies are used for enhancing performance.
Remote proxies
Remote proxies are used in distributed object communication. The remote proxy causes execution on the remote object by invoking a local object method.
Smart proxies
Smart proxies are used to implement log calls and reference counting to the object.
In the chain of responsibility pattern, Sender sends a request to a chain of objects, and any object in the chain can handle the request.
A Chain of Responsibility Pattern avoids coupling the sender of a request to its receiver. For example, an ATM service uses the Chain of Responsibility design pattern in monetary transactions.
Moreover, we can explain that usually, each receiver contains the reference of another receiver. If one object can fail to handle the request, then it sends the same to the next receiver and so on.
Usage of Chain of Responsibility Pattern
It is used in the following cases:
The motive of the Adapter pattern is to make interfaces of one or more classes to look similar.
The Bridge pattern is designed to isolate a class's interface from its implementation so we can vary or substitute the implementation without changing the client code.
The service locator is used to create class dependencies. The Class is still responsible for creating its dependencies no matter whether if it is using service locator or not.
Service locators are also used to hide dependencies. We can't say by looking at an object whether it connects with a database or not when it obtains connections from a locator.
With Dependency injection, the class which contains its dependencies neither knows nor cares where they came from.
One significant difference is that Dependency injection is much easier to unit test because we can pass in it mock implementations of its dependent objects. We could combine the two objects and apply the service locator.
This pattern is one of the most-used patterns from J2EE Design pattern category. It is quite similar to the concept of Model-View-Controller. The abbreviation MVC is taken from the Model-view-controller concept.
Models are objects, used as blueprints for all of the objects that will be used in the application.
Views contain the presentational aspect of the data and information located in the models.
Controllers control both model and view as they serve as a connection between the two objects. The controller plays the role of an interface between View and Model and also intercepts all the incoming requests.
The intercepting filter design pattern is used to intercept and manipulate a request and response before and after the request processing. Filters perform the authentication/ authorization/ logging or tracking of request and then forward the requests to corresponding handlers. Let's have a look at some basic entities of Intercepting design pattern.
Filter
It performs a certain task before or after the execution of request by request handler.
Filter Chain
It contains multiple filters and helps to execute them in defined order on target.
Target
The target object is the request handler
Filter Manager
It manages the filters and Filter Chain.
Client
The client object is one who sends a request to the Target object.
Benefits of Intercepting Filter Design Pattern
Data Access Object Pattern is used to isolate low-level data accessing API or actions from high-level business services. Following are the components in the DAO Pattern.
Data Access Object Interface
DAO interface describes the standard actions to be performed on a model object(s).
Data Access Object concrete class
This class implements a DAO interface. This class is accountable to get data from a data source which can be Xml/database or any other storage mechanism.
Model Object or Value Object
This object is a plain old java object containing get/set methods to store data retrieved using DAO class.
The difference between VO and JDO is that the JDO is a persistent technology that competes against entity beans. It allows to create POJO (plain old java objects) and persevere them to the database.
While VO (value objects) represents an abstract design pattern, that is used in conjunction with entity beans, JDBC and JDO.
Fri, 16 Jun 2023
Fri, 16 Jun 2023
Fri, 16 Jun 2023
Write a public review