Design patterns are reusable solutions to common problems in software design. They help in creating more maintainable, scalable, and efficient software. In this article, we will delve into some advanced design patterns that are essential for any seasoned developer.
Singleton Pattern 🌟
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. This pattern is useful when exactly one object is needed to coordinate actions across the system.
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Factory Method Pattern 🛠️
The Factory Method pattern defines an interface for creating an object, but lets subclasses alter the type of objects that will be created. This pattern is useful when a class can't anticipate the class of objects it needs to create.
public interface Creator {
Product createProduct();
}
public class ConcreteCreatorA implements Creator {
public Product createProduct() {
return new ConcreteProductA();
}
}
public class ConcreteCreatorB implements Creator {
public Product createProduct() {
return new ConcreteProductB();
}
}
Builder Pattern 🏗️
The Builder pattern is a design pattern that separates the construction of a complex object from its representation. This pattern is useful when you need to create objects with many fields and it's not clear how to manage the dependencies between them.
public class House {
private String foundation;
private String walls;
private String roof;
public House(HouseBuilder builder) {
this.foundation = builder.foundation;
this.walls = builder.walls;
this.roof = builder.roof;
}
public static class HouseBuilder {
private String foundation;
private String walls;
private String roof;
public HouseBuilder setFoundation(String foundation) {
this.foundation = foundation;
return this;
}
public HouseBuilder setWalls(String walls) {
this.walls = walls;
return this;
}
public HouseBuilder setRoof(String roof) {
this.roof = roof;
return this;
}
public House build() {
return new House(this);
}
}
}
Observer Pattern 🌈
The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. This pattern is useful when you need to notify multiple objects about the state changes of a single object.
public interface Observer {
void update(String message);
}
public class ConcreteObserver implements Observer {
public void update(String message) {
System.out.println("Received message: " + message);
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
For more information on design patterns, you can visit our Design Patterns section.