Java Design Patterns Interview Questions Preparation Course
4.0 (14 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,131 students enrolled
Wishlisted Wishlist

Please confirm that you want to add Java Design Patterns Interview Questions Preparation Course to your Wishlist.

Add to Wishlist

Java Design Patterns Interview Questions Preparation Course

Learn everything about Java Design Patterns. Save time in Interview preparation. Go high in your career!
4.0 (14 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,131 students enrolled
Last updated 8/2017
English
Current price: $10 Original price: $200 Discount: 95% off
5 hours left at this price!
30-Day Money-Back Guarantee
Includes:
  • 2 hours on-demand video
  • 4 Articles
  • Full lifetime access
  • Access on mobile and TV
  • Assignments
  • Certificate of Completion
What Will I Learn?
  • Understand Java Design Patterns
  • Learn important concepts of Java Design Patterns
  • Understand benefits of Java Design Patterns
  • Answer questions on Java Design Patterns
  • Best practices of Java Design Patterns
  • Understand Java Architecture and Java Design level concepts
  • Demand higher salary or promotion based on the knowledge gained!
View Curriculum
Requirements
  • Software Development Experience
  • Familiar with Java Programming
Description

Java Design Patterns is a very important topic in technical interview. Many fortune 500 organizations use Design Patterns. Big companies like Amazon, Netflix, Google etc use Java Design Patterns based architecture. This course is designed to help you answer interview questions on Java Design Patterns. 

Software Engineers with Java Design Patterns knowledge may get more salary than others with similar qualifications without Java Design Patterns knowledge.

In this course, you will learn how to handle interview questions on Java Design Patterns in Software Design and Development. We will explain you the important concepts of Java Design Patterns.

You will also learn the benefits and use cases of Java Design Patterns in this course. 

What is the biggest benefit of this course to me?

Finally, the biggest benefit of this course is that you will be able to demand higher salary in your next job interview.

It is good to learn Java Design Patterns for theoretical benefits. But if you do not know how to handle interview questions on Java Design Patterns, you can not convert your Java Design Patterns knowledge into higher salary.

What are the topics covered in this course?

We cover a wide range of topics in this course. We have questions on Java Design Patterns best practices, Factory, Singleton, Proxy Design Pattern etc.

How will this course help me?

By attending this course, you do not have to spend time searching the Internet for Java Design Patterns interview questions. We have already compiled the list of the most popular and the latest Java Design Patterns Interview questions. 

Are there answers in this course?

Yes, in this course each question is followed by an answer. So you can save time in interview preparation.

What is the best way of viewing this course?

You have to just watch the course from beginning to end. Once you go through all the videos, try to answer the questions in your own words. Also mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions. After going through this course 2-3 times, you will be well prepared to face a technical interview in Java Design Patterns.

What is the level of questions in this course?

This course contains questions that are good for a Fresher to an Architect level. The difficulty level of question varies in the course from a Fresher to an Experienced professional.

What happens if Java Design Patterns technology changes in future?

From time to time, we keep adding more questions to this course. Our aim is to keep you always updated with the latest interview questions on Java Design Patterns.

What are the sample questions covered in this course?

Sample questions covered in this course are as follows:


  1. When will you use Strategy Design Pattern in Java?
  2. What is Observer design pattern?
  3. What are the examples of Observer design pattern in JDK?
  4. How Strategy design pattern is different from State design pattern in Java?
  5. Can you explain Decorator design pattern with an example in Java?
  6. What is a good scenario for using Composite design Pattern in Java?
  7. Have you used Singleton design pattern in your Java project?
  8. What are the main uses of Singleton design pattern in Java project?
  9. Why java.lang.Runtime is a Singleton in Java?
  10. What is the way to implement a thread-safe Singleton design pattern in Java?
  11. What are the examples of Singleton design pattern in JDK?
  12. What is Template Method design pattern in Java?
  13. What are the examples of Template method design pattern in JDK?
  14. Can you tell some examples of Factory Method design pattern implementation in Java?
  15. What is the benefit we get by using static factory method to create object?
  16. What are the examples of Builder design pattern in JDK?
  17. What are the examples of Abstract Factory design pattern in JDK?
  18. What are the examples of Decorator design pattern in JDK?
  19. What are the examples of Proxy design pattern in JDK?
  20. What are the examples of Chain of Responsibility design pattern in JDK?
  21. What are the main uses of Command design pattern?
  22. What are the examples of Command design pattern in JDK?
  23. What are the examples of Interpreter design pattern in JDK?
  24. What are the examples of Mediator design pattern in JDK?
  25. What are the examples of Strategy design pattern in JDK?
  26. What are the examples of Visitor design pattern in JDK?
  27. How Decorator design pattern is different from Proxy pattern?
  28. What are the different scenarios to use Setter and Constructor based injection in Dependency Injection (DI) design pattern?
  29. What are the different scenarios for using Proxy design pattern?
  30. What is the main difference between Adapter and Proxy design pattern?
  31. When will you use Adapter design pattern in Java?
  32. What are the examples of Adapter design pattern in JDK?
  33. What is the difference between Factory and Abstract Factory design pattern?
  34. What is Open/closed design principle in Software engineering?
  35. What is SOLID design principle?
  36. What is Builder design pattern?
  37. What are the different categories of Design Patterns used in Object Oriented Design?
  38. What is the design pattern suitable to access elements of a Collection?
  39. How can we implement Producer Consumer design pattern in Java?
  40. What design pattern is suitable to add new features to an existing object?
  41. Which design pattern can be used when to decouple abstraction from the implementation?
  42. Which is the design pattern used in Android applications?
  43. How can we prevent users from creating more than one instance of singleton object by using clone() method?
  44. What is the use of Interceptor design pattern?
  45. What are the Architectural patterns that you have used?
  46. What are the popular uses of Façade design pattern?
  47. What is the difference between Builder design pattern and Factory design pattern?
  48. What is Memento design pattern?
  49. What is an AntiPattern?
  50. What is a Data Access Object (DAO) design pattern?


Who is the target audience?
  • Software Engineer, Sr. Software Engineer, Member Technical Staff, Expert
  • Java Architect, Java Expert
  • Software Architect, Development Manager, Director
  • Anyone who wants to learn Java Design Patterns
  • Person interested in going up in career!!
Compare to Other Interviewing Skills Courses
Curriculum For This Course
39 Lectures
01:56:36
+
Why should you learn Java Design Patterns?
3 Lectures 03:30
+
Java Design Patterns Interview Questions - Part 1
5 Lectures 17:42

Q. When will you use Strategy Design Pattern in Java?

Strategy pattern is very useful for implementing a family of algorithms. It is a behavioral design pattern.

With Strategy pattern we can select the algorithm at runtime. We can use it to select the sorting strategy for data. We can use it to save files in different formats like- .txt, .csv, .jpg etc.

Preview 03:24

Q.    What is Observer design pattern?

In Observer design pattern, there is a Subject that maintains the list of Observers that are waiting for any update on the Subject. Once there is an update in Subject it notifies all the observers for the change.

E.g. In real life, students are waiting for the result of their test. Here students are the observers and test is the subject. Once the result of test is known, testing organization notifies all the students about their result.

The most popular use of Observer pattern is in Model View Controller (MVC) architectural pattern.

Main issue with Observer pattern is that it can cause memory leaks. The subject holds a strong reference to observers. If observers are not de-registered in time, it can lead to memory leak. 

What is Observer design pattern?
04:03

Q.    How Strategy design pattern is different from State design pattern in Java?

State design pattern is a behavioral design pattern that is use for defining the state machine for an object. Each state of an object is defined in a child class of State class. When different actions are taken on an Object, it can change its state.

Strategy pattern is also a behavioral pattern, but it is mainly used for defining multiple algorithms. With same action of a client, the algorithm to be used can change.

Some people consider State pattern similar to Strategy pattern, since an Object changes its Strategy with different method invocations. But the main difference is that in State pattern internal state of an Object is one of the determining factors for selecting the Strategy for change of state.

Where as in Strategy pattern, client can pass some external parameter in input during method invocation that determines the strategy to be used at run time.

Therefore State pattern is based on the Object’s internal state, where as Strategy pattern is based on Client’s invocation.

State pattern is very useful in increasing the maintainability of the code in a large code-base.

Preview 03:25

Q.    Can you explain Decorator design pattern with an example in Java?

Some people call Decorator pattern as Wrapper pattern as well. It is used to add the behavior to an object, without changing the behavior of other objects of same class.

One of the very good uses of Decorator pattern is in java.io package. We can have a FileInputStream to handle a File. To add Buffering behavior we can decorate FileInputStream  with BufferedInputStream. To add the gzip behavior BufferedInputStream we can decorate it with GzipInputStream. To add serialization behavior to GzipInputStream, we can decorate it with ObjectInputStream.

5. Can you explain Decorator design pattern with an example in Java?
03:03

Q.    What is a good scenario for using Composite design Pattern in Java?

Some of the good scenarios where Composite design pattern can be used are as follows:

Tree Structure: The most common use of Composite design pattern is Tree structure. If you want to represent data in a Tree data structure, Composite pattern can be used.

E.g. In an Organization, to a Manager has Employees. But Manager is also an Employee. If we start from CEO level, there is one big tree for the whole organization structure. Under that big tree there are many sub-trees. This can be easily represented with Composite design pattern.

Recursion: Another use of Composite design pattern is Recursion. If we have a Recursion based algorithm, we need data to be passed to algorithm in a data structure that treats individual objects and compositions at each level of recursion uniformly.

E.g.  To implement a recursive Polynomial Solving algorithm, we can use Composite design pattern to store the intermediate results.

Graphics: Another good use of Composite design pattern is in Graphics. We can group shapes inside a composite and make higher-level groups of smaller groups of shapes to complete the graphics to be displayed on screen.


What is a good scenario for using Composite design Pattern in Java?
03:47
+
Java Design Patterns Interview Questions - Part 2
5 Lectures 15:02


Q.    Have you used Singleton design pattern in your Java project?

Yes. Singleton is one of the most popular design patterns in enterprise level Java applications. Almost in every project we see some implementation of Singleton.

With Singleton pattern we can be sure that there is only one instance of a class at any time in the application.

This helps in storing properties that have to be used in the application in a unique location.

Have you used Singleton design pattern in your Java project?
02:15


Q.    Why java.lang.Runtime is a Singleton in Java?

In Java, java.lang.Runtime is implemented on Singleton design pattern.

Runtime is the class that acts as an interface with the environment in which Java process is running. Runtime contains methods that can interact with the environment.

Like- totalmemory() method gives the total memory in JVM. maxMemory() method gives the maximum memory that JVM can use.

There is an exit() method to exit the Java process. We do not want multiple objects in JVM to have exit() method.

Similarly there is gc() method that can run the Garbage Collector. With only one copy of gc() method, we can ensure that no other object can run the Garbage Collector when one instance of GC is already running.

Preview 02:30


Q.    What is the way to implement a thread-safe Singleton design pattern in Java?

In Java there are many options to implement a thread-safe Singleton pattern. Some of these are as follows:

  • I.         Double Checked Locking: This is the most popular method to implement Singleton in Java. It is based on Lazy Initialization. In this we first check the criteria for locking before acquiring a lock to create an object. In Java we use it with volatile keyword.
  • II.         Bill Pugh Singleton: We can also use the method by Bill Pugh for implementing Singleton in Java. In this we use an Inner Static class to create the Singleton instance.
  • III.         Enum: We can also use Java enum to create thread-safe implementation. Java enum values are accessible globally so these can be used as a Singleton.

Sample code:

class DoubleCheckSingleton {

    private volatile HelloSingleton helloSingleton; // Use Volatile

   

    public HelloSingleton getHelloSingleton() {

        HelloSingleton result = helloSingleton;

        if (result == null) {

            synchronized(this) { // Synchronize for thread safety

                result = helloSingleton;

                if (result == null) {

    result = new HelloSingleton();

                    helloSingleton = result;

                }

            }

        }

        return result;

    }

}

Sample code:

public class SingletonBillPugh {

    // Inner class that holds instance

    private static class InnerSingleton{

        private static final SingletonBillPugh INSTANCE = new SingletonBillPugh();

    }

     // Private constructor

    private SingletonBillPugh(){}

  

public static SingletonBillPugh getInstance(){

        return InnerSingleton.INSTANCE;

    }

}

When first time SingletonBillPugh is loaded in memory, InnerSingleton is not loaded. Only when getInstance() method is called, InnerSingleton class is loaded and an Instance is created.

Sample Code:

public enum SingletonEnum {

    INSTANCE;

   

    public static void doImplementation(){

     …..

    }

}

What is the way to implement a thread-safe Singleton design pattern in Java?
03:25


Q.    What is Template Method design pattern in Java?

It is a behavioral design pattern. We can use it to create an outline for an algorithm or a complex operation. We first create the skeleton of a program. Then we delegate the steps of the operation to subclasses. The subclasses can redefine the inner implementation of each step.

E.g. While designing a Game in Java, we can implement it as an algorithm with Template Method pattern. Each step in the game can be deferred to subclasses responsible for handling that step.

Let say we implement Monopoly game in Java. We can create methods like initializeGame(), makeMove(), endGame() etc. Each of these methods can be handled in subclasses in an independent manner.

We can use same algorithm for Chess game with same set of abstract methods. The subclass for Chess game can provide the concrete implementation of methods like initializeGame(), makeMove(), endGame() etc.

Template Method pattern is very useful in providing customizable class to users. We can create the core class with a high level implementation. And our users can customize our core class in their custom subclasses.

What is Template Method design pattern in Java?
04:13


Q.    Can you tell some examples of Factory Method design pattern implementation in Java?

Factory Method pattern is a creational design pattern. A Factory is an object that is used to create more objects.

In general, a Factory object has methods that can be used to create a type of objects. Some people call it Factory Method design pattern as well.

Some of the examples of Factory Method pattern in JDK are:

  • Java.lang.Class.forName()
  • java.net.URLStreamHandlerFactory.createURLStreamHandler(String)
  • java.util.Calendar.getInstance()
  • java.util.ResourceBundle.getBundle()
  • java.text.NumberFormat.getInstance()
  • java.nio.charset.Charset.forName()
  • java.util.EnumSet.of()
  • javax.xml.bind.JAXBContext.createMarshaller()
Can you tell some examples of Factory Method design pattern implementation in Ja
02:39
+
Java Design Patterns Interview Questions - Part 3
5 Lectures 19:20

Q.    What is the benefit we get by using static factory method to create object?

By using Static Factory Method we encapsulate the creation process of an object. We can use new() to create an Object from its constructor. Instead we use static method of a Factory to create the object. One main advantage of using Factory is that Factory can choose the correct implementation at runtime and create the right object. The caller of method can specify the desired behavior.

E.g. If we have a ShapeFactory with createShape(String type) method. Client can call ShapeFactory.createShape(“Circle”) to get a circular shape. ShapeFactory.createShape(“Square”) will return square shape. In this way, ShapeFactory knows how to create different shapes based on the input by caller.

Another use of Factory is in providing access to limited resources to a large set of users.

E.g. In ConnectionPool, we can limit the total number of connections that can be created as well as we can hide the implementation details of creating connection. Here ConnectionPool is the factory. Clients call static method ConnectionPool.getConnection().


What is the benefit we get by using static factory method to create object?
03:36


Q.    What are the examples of Builder design pattern in JDK?

In JDK there are many places where Builder design pattern is used. Some of these are as follows:

  • I.         java.lang.StringBuilder.append(): StringBuilder is based on Builder pattern.
  • II.         java.nio.IntBuffer.put(): Invocation of put() method return IntBuffer. Also there are many variants of this method to build the IntBuffer.
  • III.         javax.swing.GroupLayout.Group.addComponent(): We can use addComponent() method to build a UI that can contain multiple levels of components.
  • IV.         java.lang.Appendable
  • V.         java.lang.StringBuffer.append(): StringBuffer is similar to StringBuilder and it is also based on Builder design pattern.

Q.   What are the examples of Abstract Factory design pattern in JDK?

In JDK there are many places where Abstract Factory design pattern is used. Some of these are as follows:

  • javax.xml.xpath.XPathFactory.newInstance()
  • javax.xml.parsers.DocumentBuilderFactory.newInstance()
  • javax.xml.transform.TransformerFactory.newInstance()

Q.   What are the examples of Decorator design pattern in JDK?

In JDK there are many places where Decorator design pattern is used. Some of these are as follows:

  • I.         In java.io package many classes use Decorator pattern. Subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor that can take the instance of same type and decorate it with additional behavior.
  • II.         In java.util.Collections, there are methods like checkedCollection(), checkedList(), checkedMap(), synchronizedList(), synchronizedMap(),  synchronizedSet(), unmodifiableSet(), unmodifiableMap() and unmodifiableList() methods that can decorate an object and return the same type.
  • III.         In javax.servlet package, there are classes like javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper that are based on Decorator design pattern.
Preview 04:46


Q.    What are the examples of Proxy design pattern in JDK?

Proxy design pattern provides an extra level of indirection for providing access to another object. It can also protect a real object from any extra level of complexity.

In JDK there are many places where Proxy design pattern is used. Some of these are as follows:

  • java.lang.reflect.Proxy
  • java.rmi.*
  • javax.inject.Inject
  • javax.ejb.EJB
  • javax.persistence.PersistenceContext

Q.   What are the examples of Chain of Responsibility design pattern in JDK?

In JDK there are many places where Chain of Responsibility design pattern is used. Some of these are as follows:

  • I.         java.util.logging.Logger.log(): In this case Logger class provides multiple variations of log() method that can take the responsibility of logging from client in different scenarios. The client has to just call the appropriate log() method and Logger will take care of these commands.
  • II.         javax.servlet.Filter.doFilter(): In the Filter class, the Container calls the doFilter method when a request/response pair is passed through the chain. With filter the request reaches to the appropriate resource at the end of the chain. We can pass FilterChain in doFilter() method to allow the Filter to pass on the request and response to the next level in the chain.
What are the examples of Proxy design pattern in JDK?
03:06

Q.    What are the main uses of Command design pattern?

Command design pattern is a behavioral design pattern. We use it to encapsulate all the information required to trigger an event. Some of the main uses of Command pattern are:

  • I.         Graphic User Interface (GUI): In GUI and menu items, we use command pattern. By clicking a button we can read the current information of GUI and take an action.
  • II.         Macro Recording: If each of user action is implemented as a separate Command, we can record all the user actions in a Macro as a series of Commands. We can use this series to implement the “Playback” feature. In this way, Macro can keep on doing same set of actions with each replay.
  • III.         Multi-step Undo: When each step is recorded as a Command, we can use it to implement Undo feature in which each step can by undo. It is used in text editors like MS-Word.
  • IV.         Networking: We can also send a complete Command over the network to a remote machine where all the actions encapsulated within a Command are executed.
  • V.         Progress Bar: We can implement an installation routine as a series of Commands. Each Command provides the estimate time. When we execute the installation routine, with each command we can display  the progress bar.
  • VI.         Wizard: In a wizard flow we can implement steps as Commands. Each step may have complex task that is just implemented within one command.
  • VII.         Transactions: In a transactional behavior code there are multiple tasks/updates. When all the tasks are done then only transaction is committed. Else we have to rollback the transaction. In such a scenario each step is implemented as separate Command.

Q.   What are the examples of Command design pattern in JDK?

In JDK there are many places where Command design pattern is used. Some of these are as follows:

  • All implementations of java.lang.Runnable
  • All implementations of javax.swing.Action


What are the main uses of Command design pattern?
04:28


Q.    What are the examples of Interpreter design pattern in JDK?

Interpreter design pattern is used to evaluate sentences in a language. E.g. In SQL we can use it to evaluate a query by evaluating each keyword like SELECT, FROM, WHERE clause.

In an Interpreter implementation there is a class for each keyword/symbol. A sentence is just a composite of these keywords. But the sentence is represented by Syntax tree that can be interpreted.

In JDK there are many places where Interpreter design pattern is used. Some of these are as follows:

  • java.util.Pattern
  • java.text.Normalizer
  • Subclasses of java.text.Format: DateFormat, MessageFormat, NumberFormat
  • Subclasses of javax.el.ELResolver: ArrayELResolver, MapELResolver, CompositeELResolver etc.

Q.   What are the examples of Mediator design pattern in JDK?

By using Mediator pattern we can decouple the multiple objects that interact with each other. With a Mediator object we can create many-to-many relationships in multiple objects.

In JDK there are many places where Mediator design pattern is used. Some of these are as follows:

  • java.util.Timer: schedule() methods in Timer class act as Mediator between the clients and the TimerTask to be scheduled.
  • java.util.concurrent.Executor.execute(): The execute() method in an Executor class acts as a Mediator to execute the different tasks.
  • java.util.concurrent.ExecutorService
  • java.lang.reflect.Method.invoke(): In Method class of reflection package, invoke() method acts as a Mediator.
  • java.util.concurrent.ScheduledExecutorService: Here also schedule() method and its variants are Mediator pattern implementations.
What are the examples of Interpreter design pattern in JDK?
03:24
+
Java Design Patterns Interview Questions - Part 4
5 Lectures 13:27

Q.    What are the examples of Strategy design pattern in JDK?

In JDK there are many places where Strategy design pattern is used. Some of these are as follows:

  • I.         java.util.Comparator: In a Comparator we can use compare() method to change the strategy used by Collections.sort() method.
  • II.         javax.servlet.http.HttpServlet: In a HttpServlet class service() and doGet(), doPost() etc. methods take HttpServletRequest and HttpServletResponse and the implementor of Servlet processes it based on the strategy it selects.

Q.   What are the examples of Visitor design pattern in JDK?

By using Visitor design pattern we can add new virtual methods to existing classes without modifying their core structure.

In JDK there are many places where Visitor design pattern is used. Some of these are as follows:

  • javax.lang.model.element.AnnotationValue and AnnotationValueVisitor
  • java.nio.file.FileVisitor and SimpleFileVisitor
  • javax.lang.model.type.TypeMirror and TypeVisitor
  • javax.lang.model.element.Element and ElementVisitor
  • javax.faces.component.visit.VisitContext and VisitCallback


What are the examples of Strategy design pattern in JDK?
02:16


Q.    How Decorator design pattern is different from Proxy pattern?

Main differences between Decorator and Proxy design pattern are:

  • Decorator provides an enhanced interface after decorating it with additional features. Proxy provides same interface since it is just acting as a proxy to another object.
  • Decorator is a type of Composite pattern with only one component. But each decorator can add additional features. Since it is one component in Decorator, there is no object aggregation.
  • Proxy can also provide performance improvement by lazy loading. There is nothing like this available in Decorator.
  • Decorator follows recursive composition. Proxy is just one object to another object access.
  • Decorator is mostly used for building a variety of objects. Proxy is mainly used for access to another object.
How Decorator design pattern is different from Proxy pattern?
02:12


Q.    What are the different scenarios to use Setter and Constructor based injection in Dependency Injection (DI) design pattern?

We use Setter injection to provide optional dependencies of an object. Constructor injection is used to provide mandatory dependency of an object.

In Spring IoC, Dependency Injection is heavily used. There we have to differentiate between the scenario suitable for Setter based and Constructor based dependency injection.

Q.   What are the different scenarios for using Proxy design pattern?

Proxy design pattern can be used in a wide variety of scenario in Java. Some of these are as follows:

  • I.         Virtual Proxy: This is a virtual object that acts as a proxy for objects that are very expensive to create. It is used in Lazy Loading. When client makes the first request, the real object is created.
  • II.         Remote Proxy: This is a local object that provides access to a remote object. It is generally used in Remote Method Invocation (RMI) and Remote Procedure Call (RPC). It is also known as a Stub.
  • III.         Protective Proxy: This is an object that control the access to a Master object. It can authenticate and authorize the client for accessing the Master object. If client has right permissions, it allows client to access the main object.
  • IV.         Smart Proxy: It is an object that can add additional information to the main object. It can track the number of other objects accessing the main object. It can track the different clients from where request is coming. It can even deny access to an object if the number of requests is greater than a threshold.
What are the different scenarios to use Setter and Constructor based injection i
03:24


Q.    What is the main difference between Adapter and Proxy design pattern?

Adapter pattern provides a different interface to an object. But the Proxy always provides same interface to the object.

Adapter is like providing an interface suitable to client’s use. But Proxy is same interface that has additional feature or check.

E.g. In electrical appliances we use Adapter to convert from one type of socket to another type of socket. In case of proxy, we have a plug with built-in surge protector. The interface for plug and the original device remains same.

Q.   When will you use Adapter design pattern in Java?

If we have two classes with incompatible interfaces, we use Adapter pattern to make it work. We create an Adapter object that can adapt the interface of one class to another class.

It is generally used for working with third party libraries. We create an Adapter class between third party code and our class. In case of any change in third party code we have to just change the Adapter code. Rest of our code can remain same and just take to Adapter.

Q.   What are the examples of Adapter design pattern in JDK?

In JDK there are many places where Adapter design pattern is used. Some of these are as follows:

  • java.util.Arrays.asList(): This method can adapt an Array to work as a List.
  • java.util.Collections.list(): This method can adapt any collection to provide List behavior.
  • java.util.Collections.enumeration(): This method returns an enumeration over the collection.
  • java.io.InputStreamReader(InputStream): This method adapts a Stream to Reader class.
  • java.io.OutputStreamWriter(OutputStream): This method adapts an OutputStream to Writer class.
  • javax.xml.bind.annotation.adapters.XmlAdapter.marshal()
What is the main difference between Adapter and Proxy design pattern?
03:22


Q.    What is the difference between Factory and Abstract Factory design pattern?

With Factory design pattern we can create concrete products of a type that Factory can manufacture. E.g. If it is CarFactory, we can produce, Ford, Toyota, Honda, Maserati etc.

With Abstract Factory design pattern we create a concrete implementation of a Factory. E.g. DeviceFactory can be Abstract and it can give us GoogleDeviceFactory, AppleDeviceFactory etc. With AppleDeviceFactory we will get products like- iPhone, iPad, Mac etc. With GoogleDeviceFactory we will get products like- Nexus phone, Google Nexus tablet, Google ChromeBook etc.

So it is a subtle difference between Factory and Abstract Factory design pattern. One way to remember is that within Abstract Factory pattern, Factory pattern is already implemented.

What is the difference between Factory and Abstract Factory design pattern?
02:13
+
Java Design Patterns Interview Questions - Part 5
5 Lectures 16:15

Q.    What is Open/closed design principle in Software engineering?

Open/closed design principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.

Open/closed principle term was originated by Bertrand Meyer in his book Object Oriented Software Construction.

As per this principle, if a module is available for extension then it is considered open. If a module is available for use by other modules then it is considered closed.

Further Robert C. Martin has mentioned it as O in SOLID principles of Object Oriented design.

It is used in State and Strategy design patterns. Context class is closed for modification. But new functionality can be added by writing new strategy code.


What is Open/closed design principle in Software engineering?
02:43


Q.    What is SOLID design principle?

SOLID word in SOLID design principle is an acronym for:

  • I.         S: Single responsibility. A Class should have a single responsibility.
  • II.         O: Open-closed. Software entities should be open for extension but closed for modification.
  • III.         L: Liskov substitution. Objects in a program should be replaceable by subclasses of same type without any adverse impact.
  • IV.         I: Interface segregation. Multiple client specific interfaces are preferable over single generic interface.
  • V.         D: Dependency inversion. Program should depend on abstract entities. It should not depend on concrete implementation of an interface.
What is SOLID design principle?
03:11

 

Q.    What is Builder design pattern?

Builder design pattern is a creational design pattern. We can use Builder pattern to create complex objects with multiple options.

E.g. when we have to create a Meal in a restaurant we can use Builder pattern. We can keep adding options like- Starter, Drink, Main Course, and Dessert etc. to create complete meal. When a user selects other options of Starter, Drink Main Course, Dessert another type of meal is created.

Main feature of Builder pattern is step-by-step building of a complex object with multiple options.

What is Builder design pattern?
03:17

Q.    What are the different categories of Design Patterns used in Object Oriented Design?

In Object Oriented design mainly three categories of design patterns are used. These categories are:

  • Creational Design Patterns:
  • Builder
  • Factory Method
  • Abstract Factory
  • Object Pool
  • Singleton
  • Prototype
  • Structural Design Patterns:
  • Adapter
  • Bridge
  • Façade
  • Decorator
  • Composite
  • Flyweight
  • Proxy
  • Behavioral Design Patterns:
  • Command
  • Iterator
  • Chain of Responsibility
  • Observer
  • State
  • Strategy
  • Mediator
  • Interpreter


What are the different categories of Design Patterns used in Object Oriented Des
04:21


Q.    What is the design pattern suitable to access elements of a Collection?

We can use Iterator design pattern to access the individual elements of a Collection. In case of an ordered collection we can get Iterator that returns the elements in an order.

In Java there are many implementation of Iterator in Collections package. We have iterators like- Spliterator, ListIterator etc. that implement Iterator pattern.

Q.   How can we implement Producer Consumer design pattern in Java?

We can use BlockingQueue in Java to implement Producer Consumer design pattern.

It is a concurrent design pattern.

Q.   What design pattern is suitable to add new features to an existing object?

We can use Decorator design pattern to add new features to an existing object. With a Decorator we work on same object and return the same object with more features. But the structure of the object remains same since all the decorated versions of object implement same interface.

What is the design pattern suitable to access elements of a Collection?
02:43
+
Java Design Patterns Interview Questions - Part 6
5 Lectures 14:34


Q.    Which design pattern can be used when to decouple abstraction from the implementation?

We can use Bridge design pattern to detach the implementation from the abstraction.

Bridge is mainly used for separation of concern in design. We can create an implementation and store it in the interface, which is an abstraction. Where as specific implementation of other features can be done in concrete classes that implement the interface.

Often Bridge design pattern is implemented by using Adapter pattern.

E.g. we have Shape interface. We want to make Square and Circle shapes. But further we want to make RedSquare, BlackSquare shapes and GreenCircle, WhiteCircle shapes. In this case rather than creating one hierarchy of all the shapes, we separate the Color concern from Shape hierarchy.

So we create two hierarchies. One is Shape to Square and Shape to Circle hierarchy. Another one is Color to Red, Black, Green, White hierarchy. In this way we can create multiple types of shapes with multiple colors with Bridge design pattern.

Which design pattern can be used when to decouple abstraction from the implement
02:24


Q.    Which is the design pattern used in Android applications?

Android applications predominantly use Model View Presenter design pattern.

  • I.         Model: This is the domain model of the Android application. It contains the business logic and business rules.
  • II.         View: These are the UI components in your application. These are part of the view. Also any events on UI components are part of view module.
  • III.         Presenter: This is the bridge between Model and View to control the communication. Presenter can query the model and return data to view to update it.
  • IV.         E.g. If we have a Model with large news article data, and view needs only headline, then presenter can query the data from model and only give headline to view. In this way view remains very light in this design pattern.
Which is the design pattern used in Android applications?
03:04


Q.    How can we prevent users from creating more than one instance of singleton object by using clone() method?

First we should not implement the Cloneable interface by the object that is a Singleton.

Second, if we have to implement Cloneable interface then we can throw exception in clone() method.

This will ensure that no one can use clone() method or Cloneable interface to create more than one instance of Singleton object.

How can we prevent users from creating more than one instance of singleton objec
02:31


Q.    What are the Architectural patterns that you have used?

Architectural patterns are used to define the architecture of a Software system. Some of the patterns are as follows:

  • I.         MVC: Model View Controller. This pattern is extensively used in the architecture of Spring framework.
  • II.         Publish-subscribe: This pattern is the basis of messaging architecture. In this case messages are published to a Topic. And subscribers subscribe to the topic of their interests. Once the message is published to a topic in which a Subscriber has an interest, the message is consumed by the relevant subscriber.
  • III.         Service Locator: This design pattern is used in a service like JNDI to locate the available services. It uses as central registry to maintain the list of services.
  • IV.         n-Tier: This is a generic design pattern to divide the architecture in multiple tiers. E.g. there is 3-tier architecture with Presentation layer, Application layer and Data access layer. It is also called multi-layer design pattern.
  • V.         Data Access Object (DAO): This pattern is used in providing access to database objects. The underlying principle is that we can change the underlying database system, without changing the business logic. Since business logic talks to DAO object, there is no impact of changing Database system on business logic.
  • VI.         Inversion of Control (IoC): This is the core of Dependency Injection in Spring framework. We use this design pattern to increase the modularity of an application. We keep the objects loosely coupled with Dependency Injection.
What are the Architectural patterns that you have used?
04:18

Q.    What are the popular uses of Façade design pattern?

Some of the popular uses of Façade design pattern are as follows:

  • I.         A Façade provides convenient methods for common tasks that are used more often.
  • II.         A Façade can make the software library more readable.
  • III.         A Façade can reduce the external dependencies on the working of inner code.
  • IV.         A Façade can act as a single well-designed API by wrapping a collection of poorly designed APIs.
  • V.         A Façade pattern can be used when a System is very complex and difficult to use. It can simplify the usage of complex system.


What are the popular uses of Façade design pattern?
02:17
+
Java Design Patterns Interview Questions - Part 7
4 Lectures 12:33

Q.    What is the difference between Builder design pattern and Factory design pattern?

Both Factory and Builder patterns are creational design patterns. They are similar in nature but Factory pattern is a simplified generic version of Builder pattern.

We use Factory pattern to create different concrete subtypes of an Object. The client of a Factory may not know the exact subtype. E.g. If we call createDrink() of a Factory, we may get Tea or Coffee drinks.

We can also use Builder pattern to create different concrete subtypes of an object. But in the Builder pattern the composition of the object can be more complex. E.g. If we call createDrink() for Builder, we can getCappuccino Coffee with Vanilla Cream and Sugar, or we can get Latte Coffee with Splenda and milk cream.

So a Builder can support creation of a large number of variants of an object. But a Factory can create a broader range of known subtypes of an object.


What is the difference between Builder design pattern and Factory design pattern
03:06


Q.    What is Memento design pattern?

  • Memento design pattern is used to implement rollback feature in an object. In a Memento pattern there are three objects:
  • Originator: This is the object that has an internal state.
  • Caretaker: This is the object that can change the state of Originator. But it wants to have control over rolling back the change.
  • Memento: This is the object that Caretaker gets from Originator, before making and change. If Caretaker wants to Rollback the change it gives Memento back to Originator. Originator can use Memento to restore its own state to the original state.

E.g. One good use of memento is in online Forms. If we want to show to user a form pre-populated with some data, we keep this copy in memento. Now user can update the form. But at any time when user wants to reset the form, we use memento to make the form in its original pre-populated state. If user wants to just save the form we save the form and update the memento. Now onwards any new changes to the form can be rolled back to the last saved Memento object. 

What is Memento design pattern?
03:14


Q.    What is an AntiPattern?

An AntiPattern is opposite of a Design Pattern. It is a common practice in an organization that is used to deal with a recurring problem but it has more bad consequences than good ones.

AntiPattern can be found in an Organization, Architecture or Software Engineering.

Some of the AntiPatterns in Software Engineering are:

  • I.         Gold Plating: Keep on adding extra things on a working solution even though these extra things do not add any additional value.
  • II.         Spaghetti Code: Program that are written in a very complex way and are hard to understand due to misuse of data structures.
  • III.         Coding By Exception: Adding new code just to handle exception cases and corner case scenarios.
  • IV.         Copy Paste Programming: Just copying the same code multiple times rather than writing generic code that can be parameterized.
What is an AntiPattern?
03:08


Q.    What is a Data Access Object (DAO) design pattern?

DAO design pattern is used in the data persistent layer of a Java application. It mainly uses OOPS principle of Encapsulation.

By using DAO pattern it makes the application loosely coupled and less dependent on actual database.

We can even implement some in-memory database like H2 with DAO to handle the unit-testing.

In short, DAO hides the underlying database implementation from the class that accesses the data via DAO object.

Recently we can combine DAO with Spring framework to inject any DB implementation.

What is a Data Access Object (DAO) design pattern?
03:05
+
Bonus Material
2 Lectures 04:21
Bonus Lecture: What Next?
02:07

Test Your Knowledge
02:13
About the Instructor
KnowledgePowerhouse !
3.7 Average rating
236 Reviews
3,760 Students
18 Courses
Top most career courses! 18 Courses, 3300+ students!

I am a Software Architect with expertise in Cloud Computing, Amazon Web Services, Microservices, Data Science, Hadoop, Spark, Machine Learning and Java architecture. Learning, using and sharing Technology is my passion.

I have built systems that are running enterprise software of companies across the world. I have gained a lot of knowledge by working hands-on on these large scale software projects.

With these courses I aim to share my knowledge with the future Software Engineers, Developers, Leaders and Architects . 

I am sure the knowledge in these courses can give you extra power to win in life.

All the best!!