Strategy Mode of design pattern

Source: Internet
Author: User

Policy mode (strategy):

The policy model embodies two very basic principles of object-oriented design:

L Concept of package change;

L use interfaces in programming, not implementations of interfaces;

Interface-Oriented Programming

Definition of the policy mode:

L Define a set of algorithms that encapsulate each algorithm and enable them to be interchangeable;

L Policy mode enables these algorithms to change independently of each other when they are called by the client;

The meaning of the strategy mode:

L Policy mode enables developers to develop software that consists of a number of replaceable parts, and the relationships between the parts are weakly connected;

L Weak connection characteristics make the software more extensible, easy to maintain, and more importantly, it greatly improves the reusability of software;

The composition of the policy mode:

Abstract policy roles: policy classes, usually implemented by an interface or abstract class;

L Specific strategy role: packaging the relevant algorithms and behaviors;

Environment role: holds a reference to a policy class that is eventually called to the client.

Implementation of the policy pattern:

The purpose of the strategy model is to target a set of algorithms, encapsulating each algorithm in a separate class with a common interface, so that they can be replaced by each other;

L policy mode allows the algorithm to change without affecting the client. The use of strategy mode can separate behavior and environment;

The Environment class is responsible for maintaining and querying the behavior classes, and various algorithms are provided in the specific strategy. Because the algorithm and the environment are independent, the modification of the algorithm will not affect the environment and the client.

Steps to write the policy mode:

1. Define a public interface for the policy object;

2. Write the policy class, which implements the public interface above;

3. Save a reference to the policy object in the class that uses the policy object;

4. In the class that uses the policy object, implement the Set and Get methods (injections) of the policy object or use the construction method to complete the assignment;

Disadvantages of the policy model:

1. The client must specify all the policy classes and decide which policy class to use at its own discretion;

2. Resulting in a lot of strategy classes; (using factory method to solve this problem)

This article turns from: programmer encouragement Teacher

Strategy Mode of design pattern

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.