Sunday, September 21, 2014

Java Design Pattern interview questions and answers

1. What is design pattern?

Answer:
A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations.

2. What is Singleton design pattern? When do you use it?

Keyword:
only one instance, a global point of access.
logging, cache, service registry, thread pool, configuration settings, database connection manager.

Answer:
Singleton design pattern ensures a class has only one instance, and provide a global point of access to it.

Use the Singleton pattern when
· there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
· when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

In practice, Singleton pattern is used in logging, cache, service registry, thread pool, configuration settings, database connection manager.

3. How to implement a thread-safe singleton in Java?

Keyword:
Eager initialization
Lazy initialization
On demand holder
Enum

Answer:
a. Eager initialization:
public class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

b. Lazy initialization:
public class Singleton {
  private static Singleton INSTANCE;

  private Singleton() {}

  public static synchronized Singleton getInstance() {
    if (INSTANCE == null) {
      INSTANCE = new Singleton();
    }
    return INSTANCE;
  }
}

c. Initialization On Demand Holder Idiom:
public class Singleton {
private Singleton() { }

private static class SingletonHolder {
public static final Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

d. Using Enum:
public enum Singleton {
INSTANCE;
}

4. What is Factory Method pattern? When do you use it?

Keyword:
define a creation interface, subclasses implement concrete creation logic.
Class.newInstance()

Answer:
Factory Method pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Use the Factory Method pattern when
· a class can't anticipate the class of objects it must create.
· a class wants its subclasses to specify the objects it creates.
· classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Examples in Java API:
java.lang.Class#newInstance()
java.lang.reflect.Constructor#newInstance()
java.lang.Boolean#valueOf(String)

5. What is Abstract Factory pattern? When do you use it?

Keyword:
define an interface of creating families of products without specifying concrete classes.
DriverManager#getConnection()
DocumentBuilderFactory#newInstance()

Answer:
Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Use the Abstract Factory pattern when
· a system should be independent of how its products are created, composed, and represented.
· a system should be configured with one of multiple families of products.
· a family of related product objects is designed to be used together, and you need to enforce this constraint.
· you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Examples in Java API:
java.util.Calendar#getInstance()
java.sql.DriverManager#getConnection()
javax.xml.parsers.DocumentBuilderFactory#newInstance()

6. Explain the SOLID principles in Object-Oriented Design.

Keyword:
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle

Answer:
S (SRP): Single responsibility principle. It means a class should have only a single responsibility.

O (OCP): Open/closed principle. It means software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

L (LSP): Liskov substitution principle. It means objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

I (ISP): Interface segregation principle. It means clients should not be forced to depend upon interfaces that they don't use.

D (DIP): Dependency inversion principle. It means: high-level modules should not depend on low-level modules. Both should depend on abstractions; Abstractions should not depend on details. Details should depend on abstractions.

More Java Design Pattern interview questions and answers: Java Interview Notes

  • What is the difference between Factory Method and Abstract Factory?
  • What is Builder pattern? When do you use it?
  • What is Adapter pattern? When do you use it?
  • What is Chain of Responsibility pattern? When do you use it?
  • What is Observer pattern? When do you use it?
  • What is Template Method pattern? When do you use it?
  • ......

Java Interview Notes

300+ frequently asked Java interview questions with concise summaries and detailed answers. Topics include: Java & OOP, Strings & Collections, IO JVM & GC, Multithreading, Generics Reflection & Annotations, Design Patterns, Java EE, Spring, JPA & Hibernate.



JavaScript Interview Notes

100+ frequently asked JavaScript interview questions with concise summaries and detailed answers. Topics include: JavaScript Basics, DOM, BOM, Object-Oriented JavaScript, Function, Scope, Closure, JSON, XML, Ajax, jQuery. 
Download on the AppStore    Get it On Google Play

 SQL Interview Notes

100+ frequently asked SQL and Database interview questions with concise summaries and detailed answers.  
Topics include: SQL Basic Concepts, SQL DDL & DML, Advanced SQL, Database Design and Performance Tuning.  
Download on the AppStore    Get it On Google Play


No comments:

Post a Comment