Design pattern interview question in Java
design pattern interview question are integral part of any good list of core Java interview questions. Java is a popular Object oriented programming language and have lots of design pattern and design principles, contributed by many developers and open source framework. As a Java programmer its expected from you to know OOPS concept like Abstraction, Encapsulation and polymorphism, What is design pattern in Java, Some popular Java design pattern and most importantly when to use those design pattern in Java application. purpose of asking design pattern interview question in Java is to check whether Java programmer are familiar to those essential design patterns or not. Design patterns in Java interviews are as important as multi-threading, collection and programming questions. If you are senior or experienced Java programmer than expect more complex and tough design pattern in Java interview e.g. Chain of responsibility design pattern and solving real time software design questions.
Top Java design pattern questions and answers
Here is my list of top 10 design pattern interview question in Java. I have also provided answer of those Java design pattern question as link. no matter which level of Java interview are you going e.g. programmer, software engineer, senior software engineer in Java, you can expect few question from Java design pattern.
What is Observer design pattern in Java? When do you use Observer pattern in Java?
This is one of the most common Java design pattern interview question. Observer pattern is based upon notification, there are two kinds of object Subject and Observer. Whenever there is change on subject’s state observer will receive notification. See What is Observer design pattern in Java with real life example for more details.
What is decorator pattern in Java? Can you give an example of Decorator pattern?
Decorator pattern is another popular java design pattern question which is common because of its heavy usage in java.io package. BufferedReader and BufferedWriter are good example of decorator pattern in Java. See How to use Decorator pattern in Java fore more details.
What is Singleton pattern in Java?
Singleton pattern in Java is a pattern which allows only one instance of Singleton class available in whole application. java.lang.Runtime is good example of Singleton pattern in Java. There are lot’s of follow up questions on Singleton pattern see 10 Java singleton interview question answers for those followups
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. See Why Enum singleton is better in Java for more details.
What is Factory pattern in Java? What is advantage of using static factory method to create object?
Factory pattern in Java is a creation Java design pattern and favorite on many Java interviews.Factory pattern used to create object by providing static factory methods. There are many advantage of providing factory methods e.g. caching immutable objects, easy to introduce new objects etc. See What is Factory pattern in Java and benefits for more details.
What is difference between Factory and Abstract factory in Java
see here of this Java interview question.
What is Builder design pattern in Java? When do you use Builder pattern ?
Builder pattern in Java is another creational design pattern in Java and often asked in Java interviews because of its specific use when you need to build an object which requires multiple properties some optional and some mandatory. See When to use Builder pattern in Java for more details
Can you write code to implement producer consumer design pattern in Java?
Producer consumer design pattern is a concurrency design pattern in Java which can be implemented using multiple way. if you are working in Java 5 then its better to use Concurrency util to implement producer consumer pattern instead of plain old wait and notify in Java. Here is a good example of implementing producer consumer problem using BlockingQueue in Java.
Can you give an example of SOLID design principles in Java?
There are lots of SOLID design pattern which forms acronym SOLID, read this list of SOLID design principles for Java programmer to answer this Java interview question.
What is difference between Abstraction and Encapsulation in Java?
I have already covered answer of this Java interview question in my previous post as Difference between encapsulation and abstraction in Java. See there to answer this question.
This was my list of 10 popular design pattern interview question in Java. I have not included MVC (Model View Controller) design pattern because that is more specific to J2EE and Servlet JSP interview, but if you are going for any Java interview which demands experience in J2EE than you must prepare MVC design pattern. That’s all on Java design pattern interview question and answers. Please let us know if you have any other interesting question on Java design pattern.