"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Design Patterns in Java with examples

Design Patterns in Java with examples

Published on 2024-11-09
Browse:151

Design Patterns in Java with examples

What are Design Patterns in Java?

Design patterns are reusable solutions to common problems in software design. They represent best practices that can be applied to various situations in software development, particularly in object-oriented programming like Java.

Types of Design Patterns

  • Creational Patterns: Deal with object creation mechanisms.
  • Structural Patterns: Concerned with how classes and objects are composed.
  • Behavioral Patterns: Focus on communication between objects.

Uses of Design Patterns

  • Promote code reusability.
  • Improve code readability and maintainability.
  • Facilitate communication among developers.

Examples of Design Patterns

1. Singleton Pattern

    public class Singleton {
        private static Singleton instance;

        private Singleton() {}

        public static Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
Pros:
  • Controlled access to a single instance.
  • Saves memory by preventing multiple instances.
Cons:
  • Difficult to test due to global state.
  • Can lead to tight coupling.

2. Observer Pattern

    import java.util.ArrayList;
    import java.util.List;

    interface Observer {
        void update(String message);
    }

    class Subject {
        private List observers = new ArrayList();

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

        public void notifyObservers(String message) {
            for (Observer observer : observers) {
                observer.update(message);
            }
        }
    }
Pros:
  • Promotes loose coupling between subject and observers.
  • Easily extendable with new observers.
Cons:
  • Poor performance if there are many observers.
  • Difficult to debug due to indirect communication.

3. Builder Pattern

    class Product {
        private String part1;
        private String part2;

        public void setPart1(String part1) { this.part1 = part1; }
        public void setPart2(String part2) { this.part2 = part2; }
    }

    class Builder {
        private Product product = new Product();

        public Builder buildPart1(String part1) {
            product.setPart1(part1);
            return this;
        }

        public Builder buildPart2(String part2) {
            product.setPart2(part2);
            return this;
        }

        public Product build() {
            return product;
        }
    }
Pros:
  • Simplifies the creation of complex objects.
  • Makes the code more readable and maintainable.
Cons:
  • Adds complexity with multiple builder classes.
  • Can be overkill for simple objects.

4. Factory Pattern

    interface Shape {
       void draw();
    }

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

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

    class ShapeFactory {
       public Shape getShape(String shapeType) {
          if (shapeType == null) return null;
          if (shapeType.equalsIgnoreCase("CIRCLE")) return new Circle();
          if (shapeType.equalsIgnoreCase("RECTANGLE")) return new Rectangle();
          return null;
       }
    }
Pros:
  • Encapsulates object creation logic.
  • Easily extendable for new shapes without modifying existing code.
Cons:
  • Adds additional classes which can increase complexity.
  • If not used properly, can lead to a proliferation of factory classes.

The Gang of Four (GoF)

The "Gang of Four" refers to the authors of the influential book titled "Design Patterns: Elements of Reusable Object-Oriented Software." The authors—Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides—introduced 23 classic design patterns that have become a foundation for software engineering.

Conclusion

Design patterns are essential tools in Java programming that help developers create robust and maintainable systems. Understanding their uses, advantages, and disadvantages is crucial for effective software design. The Gang of Four's contributions provide a solid foundation for applying these patterns effectively.

Release Statement This article is reproduced at: https://dev.to/codegreen/design-patterns-in-java-with-examples-1klg?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3