Design Patterns in Java

Design patterns help us write better software. They are like ready-made solutions. You can use them to solve common problems in programming.


1. Singleton Pattern

Singleton means “only one.” It makes sure a class has only one object.

Example from life: You only have one President in a country. You don’t want more than one.

Example in software: You may need only one database connection object in your app.

Java Example:


public class President {
    private static President instance = null;

    private President() {
        // private constructor
    }

    public static President getInstance() {
        if (instance == null) {
            instance = new President();
        }
        return instance;
    }
}
  

2. Factory Pattern

Factory pattern creates objects for you. You don't use new directly.

Example from life: A pizza shop is a factory. You tell it which pizza you want. It gives it to you.

Example in software: You ask the factory for a shape object. It returns a circle or square.

Java Example:


interface Shape {
    void draw();
}

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

class Square implements Shape {
    public void draw() {
        System.out.println("Drawing a square");
    }
}

class ShapeFactory {
    public Shape getShape(String type) {
        if (type.equalsIgnoreCase("circle")) {
            return new Circle();
        } else if (type.equalsIgnoreCase("square")) {
            return new Square();
        }
        return null;
    }
}

// Usage
ShapeFactory factory = new ShapeFactory();
Shape shape = factory.getShape("circle");
shape.draw();
  

3. Observer Pattern

Observer pattern is about watching. Many objects can watch (observe) another object.

Example from life: You follow someone on social media. When they post, you get notified.

Example in software: A weather station sends updates to your app when temperature changes.

Java Example:


import java.util.*;

class WeatherStation {
    private List<Observer> observers = new ArrayList<>();
    private int temperature;

    public void addObserver(Observer o) {
        observers.add(o);
    }

    public void setTemperature(int t) {
        temperature = t;
        notifyObservers();
    }

    private void notifyObservers() {
        for (Observer o : observers) {
            o.update(temperature);
        }
    }
}

interface Observer {
    void update(int temperature);
}

class PhoneDisplay implements Observer {
    public void update(int temperature) {
        System.out.println("Phone: Temperature is " + temperature);
    }
}
  

4. MVC Pattern

MVC stands for Model-View-Controller. It is used in GUI and web apps.

Example from life: Think of a restaurant. The kitchen is the model (data), the waiter is the controller, and the customer sees the menu (view).

Model = data, View = display, Controller = logic.

Java Example (Simplified):


// Model
class Student {
    private String name;
    public Student(String name) { this.name = name; }
    public String getName() { return name; }
}

// View
class StudentView {
    public void printStudentDetails(String name) {
        System.out.println("Student: " + name);
    }
}

// Controller
class StudentController {
    private Student model;
    private StudentView view;

    public StudentController(Student model, StudentView view) {
        this.model = model;
        this.view = view;
    }

    public void updateView() {
        view.printStudentDetails(model.getName());
    }
}
  

Conclusion

Design patterns make your code clean and smart.

They help you avoid repeating mistakes.

Learn them, and use them when needed!