Get ready for your next interview with our comprehensive question library
Design patterns are reusable solutions to commonly occurring problems in software design. They represent best practices and proven solutions that have evolved over time. Design patterns are important because they:
The concept was popularized by the "Gang of Four" (GoF) book which categorized 23 fundamental patterns into Creational, Structural, and Behavioral categories.
The three main categories are:
Creational Patterns: Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation (e.g., Singleton, Factory, Builder)
Structural Patterns: Deal with object composition and relationships between entities (e.g., Adapter, Decorator, Facade)
Behavioral Patterns: Focus on communication between objects and the assignment of responsibilities (e.g., Observer, Strategy, Command)
The Singleton pattern ensures that a class has only one instance and provides global access to that instance.
When to use:
Basic implementation:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
The Factory Method pattern creates objects without specifying their exact classes. It defines an interface for creating objects, but lets subclasses decide which class to instantiate.
Example:
interface Animal {
void makeSound();
}
abstract class AnimalFactory {
abstract Animal createAnimal();
}
class DogFactory extends AnimalFactory {
Animal createAnimal() {
return new Dog();
}
}
Benefits:
The Adapter pattern allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces.
Example:
// Existing interface
interface MediaPlayer {
void play(String filename);
}
// Incompatible interface
interface AdvancedPlayer {
void playVlc(String filename);
}
// Adapter
class MediaAdapter implements MediaPlayer {
AdvancedPlayer advancedPlayer;
public void play(String filename) {
if (filename.endsWith(".vlc")) {
advancedPlayer = new VlcPlayer();
advancedPlayer.playVlc(filename);
}
}
}
Use cases:
The Facade pattern provides a simplified interface to a complex subsystem. It hides the complexity of the system and provides an easier interface for clients.
Benefits:
Example:
A computer facade that hides the complexity of starting CPU, memory, hard drive, etc., and provides a simple start()
method.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime