Interview Questions of Design Pattern
I’m Arsalan Mirbozorgi;
These Design Pattern Interview Questions are designed to give you an idea of what to expect when it comes to interviewing for the Design Pattern position. As far as I can tell, the best interviewers don’t plan to ask you a specific question during your interview; instead, they begin by asking you a general inquiry about the topic and then expand on it based on your answers.
What is a Design Pattern, and how are they used?
Design patterns represent object-oriented software engineers’ best practices. Several design patterns can be used to solve common issues faced by software engineers. Over a long period, several software engineers tried and failed to come up with these solutions.
Do you know what GIF stands for?
Designers’ Patterns – Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides was published in 1994. It was the beginning of Design Patterns in software development. The Gang of Four is the collective name for this group of authors (GOF).
Which Design Patterns are there?
There are three types of design patterns: generative, structural, and behavioral.
Instead of instantiating objects directly, these design patterns hide the creation logic and allow you to build objects without new operators. As a result, the software may more easily determine which objects are required for a specific use case. An important aspect of structural patterns is their focus on class and object composition. The concept of inheritance is used to construct interfaces and define ways to compose objects to acquire additional functions. This kind of pattern is about how objects start communicating with each other.
Is there a list of J2EE patterns available?
The presentation tier is the focus of these design patterns. The Sun Java Center has recognized these trends.
What is a Factory pattern, and how does it differ from other patterns?
One of the most applied design patterns in Java is the factory pattern. One of the finest ways to make an object is through this pattern, which is classified as a creational pattern.
Objects are created in a Factory design without revealing the creation mechanism to the client, and they are referred to via a common interface.
What is the purpose of the Abstract Factory pattern, and how does it work?
Factory patterns revolve around a super factory that produces other factories. When it comes to manufacturing, this facility is known as the factory of factories. An object can be created in the best possible method thanks to the creational patterns used in this type of design pattern.
Without explicitly defining each object’s class, an abstract factory pattern interface takes care of generating the factory. The Factory pattern can be applied to any factory that is generated.
Definition: What is the Singleton pattern?
As far as design patterns go, the Java Singleton pattern is one of the most basic. One of the finest ways to make an object is through this pattern, which is classified as a creational pattern. A single class is responsible for making an object while ensuring that only one object is created at any given time. Using this class, you can get to the class’s lone object directly without initializing it.
In Java, how do you build a Singleton class?
There are two steps involved. The first step is to make the class’s constructor private so that the new operator cannot be used to instantiate it. A method can be used to return an object of the object if it isn’t null, or it can be used to build and return the object.
A static class and a singleton class are two different types of classes.
Singleton classes and static classes are very different in the following ways.
Singleton classes can implement interfaces, but static classes can’t because they are not top-level classes.
If you’re using a Singleton class, you don’t have to make all its members static.
A singleton class can be lazily loaded, but a static class cannot be lazily loaded because it is already initialized when it is loaded.
There is a difference between a static class object and a singleton class object.
Is it possible to clone a singleton?
Is there a way to stop a singleton object from being cloned?
When using the clone() method, throw an exception.
Some of the design patterns utilized in the JDK library can be listed here.
The JDK library employs a number of these design patterns.
Wrapper classes employ the Decorator pattern.
Runtime and Calendar classes use the Singleton pattern.
Wrapper classes like Integer.valueOf use the factory pattern.
Event handling frameworks like swing and awt employ the observer pattern.
Factory pattern has what advantages?
If you’re looking for a simple way to alter your application’s code, the factory pattern is for you.
What is the Builder pattern?
The Builder pattern is applied to create a complicated item by constructing it step-by-step with smaller elements. No other objects are required for this construction to function.
What is a Prototype pattern, and how does it differ from other patterns?
It’s essential to keep performance in mind while producing a copy of an object. A prototype interface instructs the user to create a copy of the current object.
What is the use of the Prototype pattern?
When creating an object directly is prohibitively expensive, this pattern can be utilized. It may be necessary to build an object following a costly database operation. We can store the object, return its clone on the next request, and only update the database when necessary, thus decreasing the number of database queries.
Is there an Adapter pattern?
The adapter pattern is a bridge between different types of interfaces. This pattern entails a single class that serves as a bridge between two or more disparate interfaces.
What is an Adapter pattern?
Using a card reader to connect a memory card to a laptop is a real-world example. Card reader and laptop are plugged in so that a memory card can be read from a memory card reader.
Definition: What is the Bridge design?
Abstraction and its implementation can be decoupled by using a bridge, allowing the two to change independently of each other. This is a structural pattern because it provides a bridge between the implementation and abstract classes.
Using an interface as a bridge, the functionality of concrete classes can be decoupled from the implementation of the interface. It is possible to make structural changes to either type of class without impacting the other.
Where can I learn more about filter patterns?
Decoupled logical operations can be chained together using the Filter pattern or Criteria pattern to filter a set of objects using distinct criteria. If you combine numerous criteria to get one single criteria, you get this form of pattern.
What is a Composite pattern?
When we need to treat a group of objects in the same way as a single object, we utilize the composite pattern. The composite pattern applies a tree structure to represent both the parts and the entire hierarchy of an item. This form of design pattern falls under the category of structural patterns since it generates a tree-like structure for a collection of things.
Create a class that includes a collection of its own objects using this pattern. This class provides methods for altering a collection of identical objects.
What is the Decorator pattern?
Additions can be made to an object using the Decorator pattern without affecting the object’s structure. Design patterns like this fall under the structural category since they serve as a wrapper around an existing class.
Decorator classes are created that wrap the original classes and give additional functionality while keeping the class method signatures intact.
What is the pattern of a Facade?
System complexity is hidden, and an interface is provided for the client to access the system through the facade pattern. Structured patterns include this form of design because it adds an interface to disguise the complexity of an already complicated system.
We create a single class that implements the client’s simplified methods and delegate calls to methods in other system classes to implement this design pattern.
Definition: What is Flyweight?
In order to reduce the number of objects created, flyweight patterns are usually employed. This reduces the amount of memory footprint and increases performance. It’s a structural pattern since it gives solutions to reduce the number of objects in an application, therefore improving its structure.
A new object is created in the Flyweight pattern if an existing object of the same type cannot be located.
Definition of Proxy pattern
In the proxy pattern, a class acts as an abstraction of another class’s functionality. Structural patterns include this type of pattern. Create an object that acts as an intermediary between your application and external systems.
What is the Chain of Responsibilities?
As the name implies, the responsibility chain pattern produces a chain of recipient objects for a request. Decoupling the sender and recipient of a request based on the kind of request features this design pattern. Behavioral patterns include this one.
As a general rule, each receiver in this pattern carries a link to another receiver. The request is passed on to the next recipient if the first one is unable to handle it.
What is the Command pattern?
As a behavioral design technique, it falls under the topic of command patterns. A command is supplied to the invoker object by wrapping a request in an object. For this command, the Invoker object searches for a suitable command implementation and sends it to the related implementation.
Definition of Interpreter pattern
Analyzing language grammar or expression can be done through the use of an interpretation pattern. Behavior patterns include this kind of pattern. An expression interface that tells the system how to understand a given situation is used in this pattern.
An example of the Interpreter pattern can be found here.
In SQL parsing, a symbol processing engine, and other applications, this approach is used.
Are you familiar with the pattern known as the “Iterator?”
In Java and other programming languages, the iterator pattern is a common design pattern.
Environment for writing code on the internet. A collection object can be accessed sequentially using this pattern without having to know its underlying representation. Behavioral patterns include the iterator pattern.
Where can you find information about services?
This design pattern has the following components.
The actual service that will deal with the request is referred to as a service. In the JNDI server, check for a reference to such a service.
Initial Context / JNDI Context The context includes a link to the service that was utilized to perform the lookup.
To acquire services, Service Locator applies JNDI to create a single point of contact.
Service references can be stored in a cache so that they can be retrieved at any time.
The object that invokes the services through ServiceLocator is called the Client.
Mediator pattern: what is it, and how does it work?
Using a mediator pattern, numerous objects or classes can communicate more simply. Using this design pattern, you can create a mediator class that handles all communication between classes and makes it simpler to maintain the code. The mediator pattern can be classified as a type of behavior.
What is a Memento pattern, and how can you recognize one?
An object’s initial state can be restored using the Memento pattern. The behavioral pattern category includes the Memento pattern.
The Memento pattern uses the following actor classes.
There are three kinds of actors in the Memento pattern: Restoring an object’s previous state is possible with Memento. The Originator stores object states in Memento objects, and the Caretaker object is responsible for restoring them.
In what way does the Observer pattern differ from other patterns?
Based on relationships between things, for example if one object is modified, its dependents are automatically informed. This is a behavioral pattern that is seen. The Observer pattern makes use of several actor classes.
Three actor classes are used in the observer pattern. I am the subject, and I am the observer. The observers can be attached to and detached from a client object using methods on a “subject” object. To better comprehend what is going on, we have built an abstract class that extends the concrete class Subject.
A state pattern is what?
A class’s behavior in a State pattern changes depending on the state of the class. A behavior pattern classifies this kind of pattern. An object’s behavior changes in response to changes in its state object’s behavior, as defined by the State pattern.
Definition of Null Object
Checking for a NULL object instance is replaced by using a null object in this pattern. Null Object represents a relationship in which nothing is done instead of an if the check for a null value. If no data is available, a Null object can be used to deliver a default response.
When using the Null Object pattern, an abstract class describing the operations to be performed is created, as numerous concrete classes extend it. A null object class provides a do-nothing abstract class implementation, which can be used everywhere a null value is required.
What is a strategy?
A class’s behavior or algorithm can be tweaked in real-time with the Strategy pattern. Behavior patterns are the umbrella term for this type of design pattern.
As part of the Strategy pattern, we create a context object that behaves differently depending on the strategy it represents. Strategy objects alter the context object’s executing algorithm.
What is a template?
An abstract class that follows the Template pattern offers a set of predefined templates and ways of executing its functions. The method implementation can b overridden by its subclasses as needed, but the method invocation must follow the conventions set by an abstract class. This pattern falls into the area of behavior patterns.
What is a visitor’s “pattern”??
A visitor class modifies the execution algorithm of an element class in the Visitor pattern. This allows the element’s execution mechanism to adapt to changes in the number of visitors. This pattern falls under the area of behavioral patterns. The element must allow a visitor object to handle the operation on the element.
MVC stands for what?
Model-View-Controller Pattern is the full name of the MVC design pattern. This design pattern is used to decouple the various aspects of an application.
Model – Model is a representation of a Java POJO or object that contains information. If the data in the controller changes, logic can be built in to update the controller.
Model – Model is a representation of the data in the model.
A controller can control both the model and the view. Whenever data changes, the model object’s display is updated automatically. It detaches the view from our model.
What is the “Business Delegate” pattern??
Presentation and business layers are separated by using a pattern called Business Delegate Pattern. Its primary purpose is to decrease presentation-tier code’s ability to communicate with business-tier code via remote lookup.
JSP, servlet, or UI Java code may be used for the presentation tier.
Client entities can access Business Service methods through the Business Delegate class, which acts as a single point of entry. To offer business object access to business delegate objects, lookup service objects, locate, and retrieve relevant business implementations.
The interface between a business service and another business service. The implementation of concrete classes provides the actual business application logic.
It’s a pattern known as the “Composite Entity.”
The EJB persistence method employs the Composite Entity pattern. An EJB entity bean called a Composite entity represents a set of objects as a graph. As controlled by EJB entity beans, any internally dependent beans on a composite entity are automatically updated. Participants in Composite Entity Bean include those listed below. It is the principal entity bean for a Composite Entity. Objects can be used for persistence that is coarse-grained or contains a coarse-grained object itself.
This object contains dependent items since it is coarse-grained. It has its own life cycle and governs the life cycles of other items that depend on it.
A dependent object is one whose persistence lifecycle is dependent on a coarse-grained object.
A strategy represents a Composite Entity’s implementation strategy.
Do you know the Data Access Object pattern (DAO) and how it is used in programming?
The Data Access Object Pattern starts separating various low-level APIs and different operations from other services that are considered high-level. Participants in the Data Access Object Pattern are as follows.
To conduct standard operations on model objects, this interface defines a set of methods (s).
Real-world implementation of data access object (DAO) specification. An XML file, database, or any other storage mechanism can be used as the data source for this class.
Data is stored in a simple POJO called a “Model Object” (also known as a “Value Object”).
What is the function of the Front Controller?
If you want to ensure that one person handles all requests, you can use the front controller pattern. An authentication/authorization/logging/tracking handler can forward the requests to the appropriate handlers for further processing. This design pattern has the following components.
A single front controller handles all requests to the program (web or desktop-based).
The front controller can dispatch the request to a specified handler using a dispatcher object.
Requests are made for views, which are the object of the request.
In what way does the Intercepting Filter pattern differ from other filter patterns?
We utilize the intercepting filter design pattern when we wish to pre- or post-process a request or answer from the application. Requests go through a series of filters before being forwarded to their intended destination. When a request is authenticated or authorized by a filter, the request is forwarded to the appropriate handlers.
What are the components of the Intercepting Filter pattern, and how are they connected?
This design pattern has the following components.
Before or after the request is handled by the request handler, a filter can be used to perform a specific job on the request.
Using a Filter Chain, numerous filters can be executed in a certain order on the target.
The request handler is the target object.
Filter Chain, The Filter Chain is managed by the Filter Manager.
This is the object that makes a request to the Target object on behalf of the Client.
Definition: What is the Service Locator?
Services can be found via JNDI lookups using the service locator design pattern. JNDI queries might be expensive. Hence the Service Locator approach utilizes caching. A service object is cached in JNDI for the first time a service is needed. In order to speed up the application, the Service Locator caches any subsequent searches for the same or similar services.
A pattern is known as “Transfer Object.”
When sending data from one client to another with various attributes, the Transfer Object pattern is employed. Value Object is another name for a transfer object. It’s a POJO class with getter/setter methods and the ability to be serialized for network transmission. It doesn’t do anything at all. Normally, the server-side business class recovers information from the data set and populates the POJO prior to sending it to the customer or passing it as a worth. It is read-only for the client to access the transfer object. Using a transfer object created by the client, the server can change all of the database’s information at once. This design pattern has the following components.
Using the Transfer Object, the Business Service appends data to the Business Object.
It’s a simple POJO with only the ability to set and retrieve characteristics.
Object – The Object is either requested or sent by the Client to the Business Object.
You can also review your previous projects to ensure that you can speak confidently on the subject. Make sure your fundamental notions are solid before answering difficult questions in an interview, as this is what your interviewer is looking for in a new hire.
For one thing, it doesn’t matter if you couldn’t answer a few questions, but it does matter that you responded with confidence. So don’t worry about how you appear; just be confident. All the best for your future endeavors, and we at tutorialspoint wish you the best of luck. Thank you very much:-)