Programming

Introduction to Design Patterns in Java

Introduction to Design Patterns in Java

Design patterns are reusable solutions to commonly occurring problems in software design. They provide a structured approach to building software systems that are flexible, maintainable, and scalable. In this blog post, we will explore some of the most commonly used design patterns in Java and discuss how they can be implemented.

1. Singleton Pattern

The Singleton pattern ensures that only one instance of a class is created and provides a global point of access to it. This can be useful when you want to limit the number of instances of a particular class or when you want to provide a centralized access point to some resource.

public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // private constructor to prevent instantiation
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

2. Factory Pattern

The Factory pattern is a creational pattern that provides an interface for creating objects, but allows subclasses to decide which class to instantiate. It is useful when you want to encapsulate object creation and provide a level of abstraction between the client code and the actual implementation.

public interface Shape {
    void draw();
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

public class ShapeFactory {
    public Shape createShape(String type) {
        if (type.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        } else if (type.equalsIgnoreCase("circle")) {
            return new Circle();
        } else {
            throw new IllegalArgumentException("Invalid shape type");
        }
    }
}

3. Observer Pattern

The Observer pattern is a behavioral pattern that defines a one-to-many relationship between objects, where a change in one object triggers a notification to all the other objects. It is useful when you want to decouple the subject (the object being observed) from its observers (the objects that are interested in its state).

public interface Observer {
    void update(String message);
}

public interface Subject {
    void registerObserver(Observer observer);
    void removeObserver(Observer observer);
    void notifyObservers(String message);
}

public class BookStore implements Subject {
    private List<Observer> observers = new ArrayList<>();

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}

public class Customer implements Observer {
    private String name;

    public Customer(String name) {
        this.name = name;
    }

    @Override
    public void update(String message) {
        System.out.println(name + " received a notification: " + message);
    }
}

Conclusion

Design patterns are an essential part of software development and can greatly improve the quality and maintainability of your code. In this blog post, we covered three commonly used design patterns in Java: Singleton, Factory, and Observer. These patterns are just a starting point, and there are many more patterns that you can explore and apply in your projects.

By understanding and applying design patterns, you can write cleaner, more modular code that is easier to understand and maintain. So go ahead, experiment with these patterns, and let them guide you towards building better software systems.

comments powered by Disqus