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; } }
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; } }
The application scenarios of the singleton pattern include but are not limited to the following situations:
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; } } }
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(); } }
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(); } }
Application scenarios of the factory pattern include but are not limited to the following situations:
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!