Summary of strategy patterns in Java design patterns

Source: Internet
Author: User

Definition of the policy mode: (definition truncated from http://www.cnblogs.com/whgk/p/6087064.html)

1, the policy model defines the algorithm family, respectively encapsulated, so that they can replace each other, this mode allows the algorithm to change independently of the client using the algorithm

In other words, the methods used are encapsulated separately into separate classes, which are then managed uniformly by the same type of interface, so that users who need to use these methods can call them at any time. The combat behavior in the example above is equivalent to the term algorithm in the definition. Just a change of opinion.

Disadvantages:

1. The client must know all of the policy classes and decide which policy class to use at its own discretion. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows the algorithm or behavior.

2, because the policy model to each specific policy implementation is packaged into a class, if there are many alternative strategies, then the number of these classes is very large.

To recognize a policy pattern from a code perspective:

(Zhao Yun with three tricks to solve the problem, there are three elements in this scene: Three ingenious tricks, one capsule, one Zhao Yun)

1. First create a policy interface (contains an algorithm that can be executed by each of the tips)

2. Then write three implementation classes, because there are three clever tricks

3. So the stratagem has, need a bag to store three ingenious tricks

4. The protagonist Zhao Yun appearance, once opened the ingenious plan

(for self-study, do not spray)

Summary of strategy patterns in Java design patterns

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.