Summary of C + + technical problems-the 14th common design pattern and its application scenario

Source: Internet
Author: User

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

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.