Java Design Pattern Interview Questions and Answers
What is a software design pattern?
A reusable software design solution in general for the problems that are recurrently occuring. Design pattern is not a solution but a description for how to solve a problem in different situations. OOP design patterns establishes the relationships between classes or objects. Design patterns are specifically dealt with problem solving at the software design level.
When to use Strategy Design Pattern in Java?
Strategy pattern in quite useful for implementing set of related algorithms e.g. compression algorithms, filtering strategies etc. Strategy design pattern allows you to create Context classes, which uses Strategy implementation classes for applying business rules. This pattern follow open closed design principle and quite useful in Java. One example of Strategy pattern from JDK itself is a Collections.sort() method and Comparator interface, which is a strategy interface and defines strategy for comparing objects. Because of this pattern, we don't need to modify sort() method (closed for modification) to compare any object, at same time we can implement Comparator interface to define new comparing strategy (open for extension).
How do I document a design pattern?
The following are the major points for describing a pattern.
Short and meaningful name for the pattern is to be selected.
Problem description and goals and constraints that may occur in that context is to be identified.
The participating classes and objects in the design pattern and their structure, responsibilities and their collaborations are to be decided. An abstract description is applied in many different situations are provided by the solution.
Identify the uses in the real systems and its efficiency.
Can you write thread-safe Singleton in Java?
There are multiple ways to write thread-safe singleton in Java e.g by writing singleton using double checked locking, by using static Singleton instance initialized during class loading. By the way using Java enum to create thread-safe singleton is most simple way.
What is an analysis pattern?
It is a software pattern which is not related to a language. It is related to a business domain like health care, telecom. The visiting activity of a patient in the health care domain would be subject to a number of patterns.