Rule mode (1)

Source: Internet
Author: User

An important purpose of using design patterns is to reduceCodeThe coupling between them. In other words, this reduces the number of codes.
Instead of starting the entire body! At the same time, make the code easy to expand.
Of course, even if we don't need a design pattern, we can implement the basic functions we need, but design them out.ProgramNot good
Structure. If the requirement changes, it will cause a lot of trouble.
Specific to the Strategy mode, it setsAlgorithmEncapsulate them into separate classes one by one, and make them replace each other to make
Algorithms are independent of customers who use them. In this way, you only need to add new algorithms or change existing algorithms.
Local changes do not affect the main structure of the program.

 

Policy is actually a way to design a class, which breaks down a behavior in orthogonal form to form different strategies. For example, if you want to generate an object, you can create a new object or clone an object. This is a policy. In the policy mode, you can design a simple and easy-to-maintain class to facilitate reuse.

 

The rule mode emphasizes different methods for doing the same thing. These methods cannot be repeated, that is, orthogonal
Polymorphism is just a language wit. Some languages that do not support polymorphism also need to implement strategies.
The strategy is at the design level, and the polymorphism is at the language level.

 

The policy mode can be used to deal with unstable and changing requirements. For content that needs to change frequently, You can restrict this part in the policy to avoid modifying the system everywhere ..
Ace uses a large number of policy modes.

 

Differences between simple factory mode and Policy Mode

The previous problem solved is the creation of objects.
The latter encapsulates a series of algorithms and allows them to replace them. The first is the creation mode, and the second is the behavior mode.

Policy mode: defines the algorithm family, which is encapsulated separately so that they can replace each other. This mode allows algorithm changes to be independent of algorithm customers.
Simple Factory: completely independent object creation, so that object creation is irrelevant to specific users.

 

In reality, the core part of strategy is the use of abstract classes. The strategy mode can be used with very few modifications and is fast when users need to change.

Strategy and factory are similar. Strategy is relatively simple and easy to understand:

Strategy is applicable to the following scenarios:

1. save files in different formats;

2. Use different algorithms to compress files;

3. capture images using different algorithms;

4. output images of the same data in different formats, such as curves or block bar.
A simple factory is not strictly a design pattern, but a good programming habit.

 

The State mode indicates that an object presents different behaviors in different environments. The policy mode uses multiple classes to implement the same function.

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.