Home  >  Article  >  Java  >  Common design patterns in Java frameworks

Common design patterns in Java frameworks

WBOY
WBOYOriginal
2024-06-02 10:57:57325browse

Design patterns in Java framework are used to enhance the scalability, maintainability and reusability of code. Commonly used patterns include: Singleton Pattern: Ensures that only one instance of a class exists and accesses it throughout the application. Factory method pattern: Create an interface for objects, and the subclass decides which object to instantiate. Observer pattern: Define a one-to-many dependency relationship. When one object changes, other objects receive notifications and update their status. Strategy pattern: Define a series of algorithms and make them interchangeable, making the algorithm class independent of the client class.

Common design patterns in Java frameworks

Commonly used design patterns in Java frameworks

In the software development process, design patterns are a proven code organization Structures for solving common problems. In Java frameworks, design patterns are widely used to enhance the scalability, maintainability, and reusability of code. The following are some of the most commonly used design patterns in Java frameworks:

Singleton pattern

  • Purpose: Ensure a Only one instance of a class exists, and that instance is accessed throughout the application.

    public class Singleton {
      private static Singleton instance;
    
      private Singleton() { /* Private constructor to prevent instantiation */ }
    
      public static Singleton getInstance() {
          if (instance == null) {
              instance = new Singleton();
          }
          return instance;
      }
    }

Factory method pattern

  • ##Purpose: Define an interface for creating objects, let The subclass determines which class is instantiated.

    public interface ShapeFactory {
      Shape createShape();
    }
    
    public class CircleFactory implements ShapeFactory {
      @Override
      public Shape createShape() {
          return new Circle();
      }
    }
    
    public class SquareFactory implements ShapeFactory {
      @Override
      public Shape createShape() {
          return new Square();
      }
    }

Observer Pattern

  • ##Purpose:

    Define one-to-many between objects Dependencies, when one object changes, other objects are notified and update their state.

    public interface Observer {
      void update(Subject subject);
    }
    
    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() {
          for (Observer observer : observers) {
              observer.update(this);
          }
      }
    }

Strategy pattern

    ##Purpose:
  • Define a series of algorithms, encapsulate them and make them They are interchangeable. This pattern makes algorithm classes independent of the client classes that use them.

    public interface Strategy {
      int calculate(int num1, int num2);
    }
    
    public class AdditionStrategy implements Strategy {
      @Override
      public int calculate(int num1, int num2) {
          return num1 + num2;
      }
    }
    
    public class SubtractionStrategy implements Strategy {
      @Override
      public int calculate(int num1, int num2) {
          return num1 - num2;
      }
    }

  • Practical case: Observer pattern in Spring Framework

Spring Framework uses the observer pattern to notify bean events. When a bean is created, destroyed, or changed, Spring publishes events that applications can subscribe to and act accordingly.

@EventListener(ApplicationReadyEvent.class)
public void handleApplicationReadyEvent() {
    // 在应用程序启动时执行的操作
}

The above is the detailed content of Common design patterns in Java frameworks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn