Design patterns are the embodiment of design principles. In the world, the legend is martial arts cheats, summed up some of the fixed routines, programming the "Sunflower Treasure Book."
personal opinion, the design principle is followed in the programming process, and the design pattern is used for reference. Completely follow the routines, sometimes rather simple problems complicate.
The common patterns and their scenarios are as follows.
1) Single case mode.
Singleton mode is a kind of common software design pattern.
in its core structure, it contains only 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 the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources.
corresponds to a class diagram.
Scenario: Singleton mode is the best solution if you want to have only one object for a class in the system.
2) Factory mode.
Factory mode is primarily an interface for creating objects.
The factory model is divided into three categories according to the reference in Java and mode:
A. Simple Factory mode (Easy Factory)
B. Factory method Mode (Factory methods)
C. Abstract Factory mode (Factory)
These three modes are progressively abstracted from top to bottom and more general.
corresponds to a class diagram.
Here are two scenarios for using Factory mode:
A. You cannot foresee what kind of instance you need to create at the time of encoding.
B. The system should not rely on the details of how product class instances are created, combined, and expressed.
3) Policy mode.
Policy mode: Defines the family of algorithms, which are encapsulated separately so that they can be replaced by each other. This pattern allows the algorithm to change independently of the customer using the algorithm.
Corresponds to a class diagram.
The application scenario is as follows.
A. One thing, there are many options to achieve.
B. I can decide at any time which implementation to adopt.
C. More options may be added in the future.
D. The strategy model allows changes in the scenario to not affect the customers who use the scenario.
Examples of business scenarios are as follows.
The operation of the system must have a log record, usually log in the database, easy to follow the management, but in the log to the database, there may be errors, such as temporarily not connected to the database, then the first recorded in the file. The log writes to the database and the file is two algorithms, but the caller does not care, only responsible for writing is.
4) Viewer mode.
The Observer pattern, also known as the Publish / Subscribe pattern, defines a one-to-many dependency between objects, and when an object changes state, all its dependents are notified and updated automatically.
corresponds to a class diagram.
The application scenario is as follows.
A. Updates to an object state require other objects to be updated synchronously, and the number of other objects is dynamically variable.
B. Objects only need to notify their own updates to other objects without needing to know the details of other objects.
Business Scenario Example: Weather station story, weather monitoring system must be able to track the current weather conditions ( temperature, humidity, atmospheric pressure ), and can be displayed on three different devices ( Current weather conditions, weather statistics, weather forecasts ) .
Summary of C + + technical problems-the 14th common design pattern and its application scenario