Design Pattern Interview Questions Answers 2018
Which creational design pattern can be used in step by step creation of a sophisticated object?
A builder pattern is your best bet in this scenario. It typically designs intricate patterns from simple ones. It abstracts steps of object creation which ensure that various implementations of the steps can yield differently-represented objects.
Differentiate between the façade and proxy pattern.
A façade pattern conceals the complexities of the system. It gives the client an interface which they can access the system. Only a single class providing a simplified method to a client is involved. The system assigns a call to techniques of the current system classes. There exist a plethora of design patterns in programming which play various roles to simplify the system development process. Hence, design pattern interview questions are very many. We have, however, presented you with the most common ones you are likely to meet in the interview room. Make sure to go through this article before going for the interview. You can also suggest this article for your friends to get the knowledge as well
Differentiate between a layer and a tier?
A layer refers to the separation of the code and the design usually created for an application in varied files. A tier, on the other hand, is the physical location of the layer files.
What are the fundamental differences between a static and a singleton class?
A static class cannot be used as a top class or used to implement an interface. Singleton classes, on the other hand, can be utilized for that. While all the members of a static class are static, it is not a requirement in a Singleton class. The static class can only get initialized after being loaded to avoid lazy loading. Singleton classes can be lazily loaded. The static class is stored in a stack while the singleton is stored in a heap memory space.
Differentiate between the factory pattern and abstract factory pattern.
Among the design pattern interview questions, this one may not miss in your upcoming interview. Factory pattern is one of the widely used patterns in Java programming language based on its ability to provide the best way to create an object. Under this design pattern, an object is created without exposing the creation logic to the pattern. It also creates an object with a familiar interface. Abstract factory pattern, on the other hand, is a superior facility which can be used to create other factories.
What is the difference between factory and abstract factory design pattern?
Both factory and abstract factory 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.
Which design pattern will you use to create a complex object?
Builder design pattern is used to construct a complex object. It is designed to solve the issues with factory and abstract design pattern.
Give example of decorator design pattern in Java. Does it operate on object level or class level?
Decorator pattern enhances capability of individual object. Java IO uses decorator pattern extensively and classical example is Buffered class say for instance BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance
Define the decorator pattern.
Decorator patterns are tools that enable the addition of new functionality to an object. Just like the composite pattern, it is also a structural model. The decorator pattern wraps the current class.
What is Design Patterns and why anyone should use them?
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.
In what cases are the design patterns not recommended?
Design patterns are not applicable when the software being designed does not change with time and requirements. They are not used in a scenario where the requirements of the source code of a given applications are unique and similar.
What is design pattern?
Design patterns are tried and tested way to solve particular design issues by various programmers in the world. Design patterns are extension of code reuse.
Why use a factory class to instantiate a class when we can use 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 Control container/ •It gives you a lot more flexibility when it comes time to change the application as our creational logic is hidden from dependant code.
What is Filter pattern?
Filter pattern or Criteria pattern is a design pattern that enables developers to filter a set of objects using different criteria and chaining them in a decoupled way through logical operations. This type of design pattern comes under structural pattern as this pattern combines multiple criteria to obtain single criteria. Filter design pattern is useful where you want to add filters dynamically or you are implementing multiple functionalities and most of them require different filter criteria to filter something. In that case instead of hard coding the filters inside the functionalities, you can create filter criteria and re-use it wherever required.
What is Strategy pattern?
In Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern comes under behavior pattern.
Where would you prefer an abstract class over the interface?
It is possible to implement multiple interfaces but possible to extend only one class. Therefore, extending one class blocks your chances of extending other classes. • The role of an interface is to represent adjectives or behaviors. Therefore, by using an abstract class to account for the behavior of a class, the class will cease to have the clonable and runnable qualities simultaneously. That is because, in Java, two classes cannot be extended at the same time. • On-time critical application usually uses the abstract class since it is faster. • An abstract class is preferred in the case where there is an actual behavior across the hierarchy of inheritance which can be coded.
What is null Object Pattern?
Null Object pattern is a design pattern in which null object replaces NULL check for 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.
What is observer design pattern in Java?
Observer design pattern is based on communicating changes in state of object to observers so that they can take there action. Simple example is a weather system where change in weather must be reflected in 'Views' to show to public
What is observer design pattern in Java?
Observer design pattern is one of the behavioral design patterns which defines one-to-many dependencies between objects & is useful when we are interested in a state of an object and we want to get notified when there is any change in state of Object. In 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
What is the benefit of using 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. In prototype design pattern, if a similar object is already present then cloning is done keeping performance in mind.
What are the drawbacks of using singleton design pattern?
The major drawbacks of using 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.
In Java programming language, it is prohibited to access non-static variables from static methods?
The non-static data cannot be accessed from a static context in Java because non-static variables are identified with a certain instance of objects. Static variables aren't defined with any instance.
What is the observer design pattern as used in Java?
These are patterns intended to communicate changes in the status of an object to their particular observers so that they can respond to the changes accordingly.
Describe what design patterns are in programming.
These are representations of best practices by program and software developers. They are best solutions to problems that are frequently experienced in the process of software development.
What is composite pattern?
This is a design pattern used when there is a need to treat a set of objects in the same way as an individual object. They are made up of objects built in a tree structure to symbolize part of or the whole hierarchy. It falls under structural patterns and uniquely creates a class containing a set of its objects. It, therefore, provides strategies through which a set of similar objects can be modified.
What is a service oriented architecture?
This is a logical encapsulation of self-contained business functionality.
Define a singleton pattern.
This is a model that plays a role in ensuring that a single instance of a class initiated.
Give the role of the director in the implementation of a builder pattern.
This one is one of the most basic design pattern interview questions. A director class is vital in the creation sequence of an object. It is the receptor of the concrete builder, the application of abstract interface for creating objects as a parameter. It then implements the necessary processes on it.
Differentiate between the strategy and state design patterns as used in Java.
Though their structures may appear same, they are meant for different purposes. The state model is used to manage and describe the condition of the object. The strategy pattern allows the user to outline and choose a single algorithm from a group of interchangeable algorithms. Therefore, strategy pattern is client driven.
What are the various design patterns?
• Creational patterns: they give us a way to create objects while concealing the creation logic instead of instantiating objects directly using new operators. The strategy is aimed at giving the program a flexibility attribute with regards to choosing which objects have to be created for a certain use case. • Structural patterns: they have to do with the class object and composition. The concept of inheritance is utilized in the formation of interfaces. They also define ways of composing objects to get new functionalities. • Behavioral patterns: they entail communication process between objects.