Design Patterns in Java – Singleton, Factory, Builder, Adapter, Observer, and More
Learn the most common design patterns in Java, including Singleton, Factory, Builder, Prototype, Adapter, Decorator, Strategy, and Observer. Understand how to implement them to solve common software design problems.
Design patterns are proven solutions to common problems encountered in software design. These patterns offer templates for solving issues in an efficient and reusable way. In Java, design patterns are widely used to write maintainable, scalable, and flexible code. This tutorial will introduce creational, structural, and behavioral design patterns and provide Java examples for each.
1. Singleton Design Pattern
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is often used for managing shared resources like database connections or thread pools.
Key Characteristics:
- Only one instance of the class exists.
- Provides a global point of access to that instance.
- The instance is created lazily (when required).
Example – Singleton in Java:
Key Points:
- The private constructor ensures that no other instances can be created.
- The static
getInstance()method is used to retrieve the single instance.
2. Factory Design Pattern
The Factory pattern provides a way to create objects without specifying the exact class of object that will be created. This is useful for creating families of related or dependent objects.
Key Characteristics:
- Defines an interface for creating an object.
- Subclasses implement the interface to create different types of objects.
- Promotes loose coupling between client classes and the objects they create.
Example – Factory in Java:
Key Points:
- The ShapeFactory provides an abstraction to create different types of shapes without specifying the exact class.
3. Builder Design Pattern
The Builder pattern is used to construct complex objects by separating the construction process from the representation. This pattern allows you to create an object step by step.
Key Characteristics:
- Splits the object creation process into multiple steps.
- Encapsulates the construction process of an object.
- Allows an object to be created in multiple representations.
Example – Builder in Java:
Key Points:
- The ComputerBuilder class helps create a complex Computer object step by step.
- Each method in the builder returns the builder itself to allow method chaining.
4. Prototype Design Pattern
The Prototype pattern allows you to clone objects, creating new objects that are copies of existing ones. It is often used when creating new instances of an object is expensive.
Key Characteristics:
- Clones objects to create new instances.
- Reduces the overhead of creating new objects from scratch.
Example – Prototype in Java:
Key Points:
- The
clone()method creates a new object based on the current object, enabling quick object creation.
5. Adapter Design Pattern
The Adapter pattern is used to enable incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces.
Key Characteristics:
- Allows classes with incompatible interfaces to work together.
- Converts the interface of a class into another interface that a client expects.
Example – Adapter in Java:
Key Points:
- The MediaAdapter converts the AudioPlayer interface into the required format for the MediaPlayer interface.
6. Decorator Design Pattern
The Decorator pattern allows behavior to be added to an individual object dynamically, without affecting the behavior of other objects from the same class.
Key Characteristics:
- Enhances the functionality of an object at runtime.
- Provides an alternative to subclassing for extending functionality.
Example – Decorator in Java:
Key Points:
- The SportsCar decorator enhances the BasicCar object by adding additional behavior.
7. Strategy Design Pattern
The Strategy pattern allows an algorithm’s behavior to be selected at runtime. It defines a family of algorithms and makes them interchangeable.
Key Characteristics:
- Allows the algorithm to be chosen at runtime.
- Promotes flexibility and decoupling.
Example – Strategy in Java:
Key Points:
- The PaymentContext allows different PaymentStrategy objects to be used interchangeably.
8. Observer Design Pattern
The Observer pattern defines a one-to-many dependency between objects. When one object changes its state, all dependent objects are notified automatically.
Key Characteristics:
- A subject maintains a list of observers.
- Observers are notified of changes in the subject.
Example – Observer in Java:
Key Points:
- The Subject maintains the list of Observers and notifies them of any changes.