1) Singleton pattern
In its core structure, it only contains a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system and that the instance is easy to access from the outside world, thereby facilitating control of the number of instances and saving system resources.
Application scenario: If you hope that only one object of a certain class can exist in the system, the singleton mode is the best solution.
2) Factory pattern
The factory pattern mainly provides an interface for creating objects.
The application scenarios are as follows:
a. It is impossible to predict which class instance needs to be created when coding.
b. The system should not rely on the details of how product class instances are created, composed, and expressed.
3) Strategy pattern
Strategy pattern: defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. This mode allows changes to the algorithm to be independent of the customers using the algorithm.
The application scenarios are as follows.
a. There are many solutions to achieve one thing.
b. I can decide which implementation to use at any time.
c. More plans may be added in the future.
d. The strategy model prevents changes in the plan from affecting customers who use the plan.
Example business scenarios are as follows.
System operations must be logged. The logs are usually recorded in the database to facilitate subsequent management. However, when recording logs to the database, errors may occur, such as being temporarily unable to connect to the database. Then Record it in the file first. There are two algorithms for writing logs to the database and files, but the caller does not care and is only responsible for writing.
4) Observer Pattern
The Observer pattern, also known as the publish/subscribe pattern, defines one-to-many dependencies between objects. When an object changes state , all its dependencies will be notified and updated automatically.
The application scenarios are as follows:
a. Updating the status of an object requires synchronous updates of other objects, and the number of other objects is dynamically variable.
b. Objects only need to notify other objects of their own updates without knowing the details of other objects.
5) Iterator pattern
The Iterator pattern provides a way to sequentially access the individual elements of an aggregate object without exposing the internal representation of the object.
The application scenarios are as follows:
When you need to access a collection object and need to traverse no matter what these objects are, you should consider using the iterator pattern. In fact, the stl container is a good example of the iterator pattern.
6) Template method pattern
The template method pattern defines the skeleton of an algorithm in operation, deferring some steps to subclasses, and template methods enable subclasses to Certain steps of an algorithm can be redefined without changing the structure of the algorithm.
The application scenarios are as follows: For some functions, different effects are displayed on different objects, but the functional framework is the same.
The above is the detailed content of What are the design patterns in java development?. For more information, please follow other related articles on the PHP Chinese website!