Summary of C ++ technical issues-14th common design patterns and application scenarios

Source: Internet
Author: User

Summary of C ++ technical issues-14th common design patterns and application scenarios

Design Patterns are specific to design principles. In the word of jianghu, it is the secret of martial arts. Some of the fixed routines summarized in the programming world are the sunflower collections.

Personal opinions, follow the design principles in the programming process, and learn from the design pattern. It is simply a routine, but sometimes it is complicated.

Common modes and their scenarios are as follows.

1) Singleton mode.

The Singleton mode is a common software design mode.

Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance in a class in the system and the instance is easy to access, so as to conveniently control the number of instances and save system resources.

Corresponding class diagram.

Application Scenario: If you want to have only one class object in the system, the singleton mode is the best solution.

2) factory model.

The factory mode provides interfaces for object creation.

Factory models are classified into three types based on the concepts in Java and patterns:

A. Simple Factory)

B. Factory Method) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Yy4gs + authorization + ICAgICAgINXiyP3W1sSjyr2008nPtb3Pwtbwsr2z6c/zo6yyosfSuPy + authorization + IDxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141213/20141213090340208.png" alt = "\">

The following two scenarios use the factory model:

A. You cannot predict the type of instance to be created during encoding.

B. The system should not rely on the details of how product instances are created, combined, and expressed.

3) Policy mode.

Policy mode: defines algorithm families, which are encapsulated separately so that they can be replaced with each other. This mode makes the algorithm changes independent of the customers who use the algorithm.

Corresponding class diagram.

The application scenarios are as follows.

A. There are many solutions to achieve one thing.

B. I can decide which implementation to adopt at any time.

C. More plans may be added in the future.

D. The policy mode does not affect customers who use the solution.

The following is an example of a business scenario.

System operations must have log records, which are usually recorded in the database for convenience of subsequent management. However, errors may occur when logs are recorded in the database, for example, if the database cannot be connected for the moment, it should be recorded in the file first. Two algorithms are used to write logs to databases and files, but the caller does not care about them. They are only responsible for writing logs.

4) Observer mode.

The observer mode is also called the publish/subscribe mode. It defines one-to-multiple dependencies between objects. When an object changes its status, all its dependent persons will receive notifications and update them automatically.

Corresponding class diagram.

The system must be able to track the current weather conditions (temperature, humidity, atmospheric pressure) in real time and display the current weather conditions, weather statistics, and weather forecasts on three different devices ).

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.