Home > Java > javaTutorial > Explore the practical application of Java design patterns: the applicable environments of singleton pattern and factory pattern

Explore the practical application of Java design patterns: the applicable environments of singleton pattern and factory pattern

PHPz
Release: 2023-12-23 14:22:15
Original
1112 people have browsed it

Explore the practical application of Java design patterns: the applicable environments of singleton pattern and factory pattern

In-depth understanding of Java design patterns: the application scenarios of singleton mode and factory mode require specific code examples

Design patterns are practiced and widely used in software development Methodology and experience summary for solving specific problems. In Java language application development, commonly used design patterns include singleton pattern and factory pattern. This article will deeply explore the application scenarios of these two design patterns and illustrate them with specific code examples.

1. Singleton pattern

The singleton pattern is a commonly used creational design pattern. It ensures that a class has only one instance and provides a global access point. Specific implementation methods include lazy man style and hungry man style.

The lazy singleton mode is suitable for situations where resources are relatively large and frequently used. The following is a sample code of the lazy singleton pattern:

public class LazySingleton {
    private static LazySingleton instance;
    
    private LazySingleton() {
        // 私有构造方法
    }
    
    public static LazySingleton getInstance() {
        if (instance == null) {
            synchronized (LazySingleton.class) {
                if (instance == null) {
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }
}
Copy after login

The hungry singleton pattern is suitable for situations where resources are relatively small and will always be used. The following is a sample code of the Hungry-style singleton pattern:

public class EagerSingleton {
    private static final EagerSingleton instance = new EagerSingleton();
    
    private EagerSingleton() {
        // 私有构造方法
    }
    
    public static EagerSingleton getInstance() {
        return instance;
    }
}
Copy after login

The application scenarios of the singleton pattern include but are not limited to the following situations:

  1. Objects that need to be created and destroyed frequently, To reduce resource usage;
  2. Objects that require global access to facilitate sharing data or calling methods between different modules;
  3. Need to control the number of instances of classes, such as thread pools and database connection pools wait.

2. Factory pattern

Factory pattern is a commonly used creational design pattern. It encapsulates the object creation process in a factory class and provides a unified interface to the outside world. . Factory patterns include ordinary factory pattern, factory method pattern and abstract factory pattern.

Ordinary factory mode is suitable for dynamically deciding which specific instance to create based on the incoming parameters. The following is a sample code of a common factory pattern:

public class ShapeFactory {
    public Shape createShape(String shapeType) {
        if ("circle".equals(shapeType)) {
            return new Circle();
        } else if ("rectangle".equals(shapeType)) {
            return new Rectangle();
        } else if ("triangle".equals(shapeType)) {
            return new Triangle();
        } else {
            return null;
        }
    }
}
Copy after login

The factory method pattern is suitable for situations where the product line needs to be expanded. Each specific factory is responsible for creating a product. The following is a sample code of the factory method pattern:

public interface ShapeFactory {
    Shape createShape();
}

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

public class RectangleFactory implements ShapeFactory {
    @Override
    public Shape createShape() {
        return new Rectangle();
    }
}

public class TriangleFactory implements ShapeFactory {
    @Override
    public Shape createShape() {
        return new Triangle();
    }
}
Copy after login

The abstract factory pattern is suitable for situations where you need to create a set of related or dependent product objects. The following is a sample code of the abstract factory pattern:

public interface AbstractFactory {
    Shape createShape();
    Color createColor();
}

public class CircleFactory implements AbstractFactory {
    @Override
    public Shape createShape() {
        return new Circle();
    }
    
    @Override
    public Color createColor() {
        return new Red();
    }
}

public class RectangleFactory implements AbstractFactory {
    @Override
    public Shape createShape() {
        return new Rectangle();
    }
    
    @Override
    public Color createColor() {
        return new Blue();
    }
}

public class TriangleFactory implements AbstractFactory {
    @Override
    public Shape createShape() {
        return new Triangle();
    }
    
    @Override
    public Color createColor() {
        return new Green();
    }
}
Copy after login

Application scenarios of the factory pattern include but are not limited to the following situations:

  1. Situations where multiple similar objects need to be created to simplify the code Logic;
  2. Need to hide the creation details of specific products to reduce coupling;
  3. Need to expand the product line to facilitate the creation of new products.

To sum up, the singleton pattern and factory pattern are commonly used design patterns and are widely used in Java application development. The singleton pattern is suitable for scenarios where it is necessary to ensure that a class has only one instance, while the factory pattern is suitable for scenarios where the creation process of an object needs to be encapsulated. In specific applications, developers should choose appropriate design patterns based on needs to improve code quality and maintainability.

The above is the detailed content of Explore the practical application of Java design patterns: the applicable environments of singleton pattern and factory pattern. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template