Do you have your Java Design Pattern interview scheduled in the coming days? Prepare in advance with these frequently asked J2EE Design Pattern interview questions and answers. These interview questions focus primarily on the concepts of Design pattern, GoF, various Design patterns- Singleton, Observer, Factory, Builder, which will demonstrate your knowledge to the potential employer. All these questions are answered by the top industry experts and proved to be more useful in cracking the Design Pattern interview.
Antipatterns are counterparts to design patterns. By implementing a solution to a commonly occurring problem which generates negative consequences. This can be a result of a developer not knowing how best to apply a design pattern or applying with the wrong context. It presents a plan for reversing underlying causes and implementing product solutions.
While designing classes, sometimes we want the ability to restrict the creation of only one object for a particular class. Singleton pattern allows only one instance of a class to be created. On asking for a new object to be created, the class might return the pre-created object only and the same object is reused for the whole application. Advantage of the singleton pattern is that it saves memory. Whereas the disadvantage is that the code becomes tightly coupled as the singleton object acts as a global object. It doesn’t support inheritance and for some languages, the techniques like cloning, reflection and for distributed JVM applications the contract might break.
While designing applications, many times situation comes where we need to act when a certain object changes its state. This situation is generally referred to as publisher and subscriber theory as well. In this situation, all of the objects which have subscribed to the event of an object’s state change are automatically notified. This falls under the behavioural pattern category.
When there is a need for creating a duplicate object while keeping performance in mind, we can use a prototype pattern. It involves implementing a prototype interface which tells to create a clone of the current object. We should use the prototype pattern when the object creation is costly. Every time Instead of creating a new object, we shall reuse specific objects which are pre-created. For example, an object is to be created after a costly database operation. We can cache the object for reuse and return the clone on next request and update the database.
When classes are more complex in nature and when a client needs to create an object, builder pattern comes to the rescue. A client only needs to provide type and data for the class for the object to be created. It provides a common interface through which the object can be created.
It uses a factory builder pattern to decide which concrete class to initiate in order to build the desired type of object. For example, if you want to build a car. There are many components to build a car. And based on the type of car, there can be many differences. So assume that an automobile manufacturer wants to build a car.
An object pool is a creation pattern where, if the operation of creating an object is complex and time-consuming we can use Object Pool. A common application of Object Pool which applications use in general is with database connections. On application initialization, it pre-creates a set of objects and keeps them alive in a collection. Whenever there is a need, the authoritative manager assigns one object to the client and allows it to use. Once used, the object’s is not being destroyed but returned to the pool. Periodically the validity of an object is being checked by routines. If the object is not reusable, it gets destroyed and again the authoritative pool manager decides to create new objects based on the configuration/requirement by the client.
Chain of responsibility is a type of behavioural pattern. Whenever there is a situation where there are more than one object handles commands and the handler is not known in advance, the client is not aware of the handler in advance, and it is taken care automatically. And the request to be handled is handed over to a group of objects to process the request in a dynamic way.
Take an example of approval for an opening in a company. Once the manager has created a requirement of the opening/role, it needs to go through multiple hierarchies for approval from HR, Finance, Business etc. Approval from all departments if comes positive then only the overall approval status of the requirement is positive.
For example, if there are two buttons of a remote ( On and Off ), we want to use the same two buttons on more than one items selected in the menu. Like fan, Television, Tubelight. By selecting one of the items if we press On, it should send a command.
Many times it happens in the real world that one functionality might require data from multiple POJOs in the backend. Client needs to initiate multiple calls to receive all data. In those cases, one can create a POJO which is required by the functionality and on the server side, a server can aggregate data from multiple resources and create one POJO. Generally, that is called as a transfer object or a value object.
The front controller patterns act as a single point of contact for the client. It can handle all of the cross-cutting functions which are applied on every single request, for ex. Authentication, authorization, analytics, logging. Once the controller receives the request, it can then forward the request to process it further.
Generally, we use to validate if the object is null or not. Many times it becomes our first line of validation for any kind of parameters. In this pattern, the check of the Null object instance is replaced by a null object. In this pattern, we create an abstract class which might contain operations to be done. A concrete class which extends the abstract class provide do nothing implementation of this class and will be used whenever we want to use to check against the null value.
Antipatterns are counterparts to design patterns. By implementing a solution to a commonly occurring problem which generates negative consequences. This can be a result of a developer not knowing how best to apply a design pattern or applying with the wrong context. It presents a plan for reversing underlying causes and implementing product solutions.
While designing classes, sometimes we want the ability to restrict the creation of only one object for a particular class. Singleton pattern allows only one instance of a class to be created. On asking for a new object to be created, the class might return the pre-created object only and the same object is reused for the whole application. Advantage of the singleton pattern is that it saves memory. Whereas the disadvantage is that the code becomes tightly coupled as the singleton object acts as a global object. It doesn’t support inheritance and for some languages, the techniques like cloning, reflection and for distributed JVM applications the contract might break.
While designing applications, many times situation comes where we need to act when a certain object changes its state. This situation is generally referred to as publisher and subscriber theory as well. In this situation, all of the objects which have subscribed to the event of an object’s state change are automatically notified. This falls under the behavioural pattern category.
When there is a need for creating a duplicate object while keeping performance in mind, we can use a prototype pattern. It involves implementing a prototype interface which tells to create a clone of the current object. We should use the prototype pattern when the object creation is costly. Every time Instead of creating a new object, we shall reuse specific objects which are pre-created. For example, an object is to be created after a costly database operation. We can cache the object for reuse and return the clone on next request and update the database.
When classes are more complex in nature and when a client needs to create an object, builder pattern comes to the rescue. A client only needs to provide type and data for the class for the object to be created. It provides a common interface through which the object can be created.
It uses a factory builder pattern to decide which concrete class to initiate in order to build the desired type of object. For example, if you want to build a car. There are many components to build a car. And based on the type of car, there can be many differences. So assume that an automobile manufacturer wants to build a car.
An object pool is a creation pattern where, if the operation of creating an object is complex and time-consuming we can use Object Pool. A common application of Object Pool which applications use in general is with database connections. On application initialization, it pre-creates a set of objects and keeps them alive in a collection. Whenever there is a need, the authoritative manager assigns one object to the client and allows it to use. Once used, the object’s is not being destroyed but returned to the pool. Periodically the validity of an object is being checked by routines. If the object is not reusable, it gets destroyed and again the authoritative pool manager decides to create new objects based on the configuration/requirement by the client.
Chain of responsibility is a type of behavioural pattern. Whenever there is a situation where there are more than one object handles commands and the handler is not known in advance, the client is not aware of the handler in advance, and it is taken care automatically. And the request to be handled is handed over to a group of objects to process the request in a dynamic way.
Take an example of approval for an opening in a company. Once the manager has created a requirement of the opening/role, it needs to go through multiple hierarchies for approval from HR, Finance, Business etc. Approval from all departments if comes positive then only the overall approval status of the requirement is positive.
For example, if there are two buttons of a remote ( On and Off ), we want to use the same two buttons on more than one items selected in the menu. Like fan, Television, Tubelight. By selecting one of the items if we press On, it should send a command.
Many times it happens in the real world that one functionality might require data from multiple POJOs in the backend. Client needs to initiate multiple calls to receive all data. In those cases, one can create a POJO which is required by the functionality and on the server side, a server can aggregate data from multiple resources and create one POJO. Generally, that is called as a transfer object or a value object.
The front controller patterns act as a single point of contact for the client. It can handle all of the cross-cutting functions which are applied on every single request, for ex. Authentication, authorization, analytics, logging. Once the controller receives the request, it can then forward the request to process it further.
Generally, we use to validate if the object is null or not. Many times it becomes our first line of validation for any kind of parameters. In this pattern, the check of the Null object instance is replaced by a null object. In this pattern, we create an abstract class which might contain operations to be done. A concrete class which extends the abstract class provide do nothing implementation of this class and will be used whenever we want to use to check against the null value.
According to Payscale, the average pay for a Software Engineer with Design Pattern (Computer Science) skills is $90,989 per year. Java with Design Pattern skill can play roles like a programmer, software engineer, senior software engineer in Java, Java J2EE Developer, Java Microservices Developer, Java Server Side Developer, etc. There are many companies that hire Java programmer with Design Pattern skilled professionals. Few of these companies include- Morgan Stanley, Vanguard, FutureSoft IT, PayPal, Verizon, JP Morgan Chase, etc.
In case you are preparing for the Java Design Patterns job interview and in skeptics that what level of questions will be asked in a job interview then these Design Pattern with Java interview questions and answers framed by the experts will help to get acquainted with the nature of the questions you may encounter during your J2EE Design Pattern job interview. These Design Patterns in Java Questions and Answers has been designed with a special intention of helping job seekers preparing for various job interviews.
Java Design Pattern Interview Questions and Answers [ MOST ASKED DESIGN PATTERN INTERVIEW QUESTIONS]
8. What do you understand by the Open-Closed Principle (OCP)?
The Open close principle states that any class, component or entity should be open for extension but closed for modification. A class can be extended via Inheritance, Interfaces, Composition whenever required instead of modifying the code of the class. Consider an instance where we have a class that calculates the area of a square. Later, we get the requirement of calculating the area of a rectangle. Here, instead of modifying the original class, we can create one base class and this base class can be extended by the new class rectangle.
22. Consider a scenario where you are writing classes for providing market data and we have the flexibility to switch to different vendors or we can be directed to the Direct Exchange Feed. How will you approach this problem to design the system?
We can do this by having an interface called “MarketData” which will consist of the methods required by the Client. The MarketData should have the MarketDataProvider as the dependency by employing Dependency Injection. This ensures that even if the provider changes, the market data will not be impacted.
The implementation of this problem is left as an exercise to the reader.
Download PDF Your requested download is ready! Click
There are three types of design patterns. They are:
The following diagram represents the summary of the types of design patterns.