Strategy Pattern)

Source: Internet
Author: User

Model motivation:

1. to complete a task, there are often multiple methods. Each method is called a policy. We can select different policies based on different environments or conditions to complete the task.

2. Similar situations are often encountered in software development. There are multiple ways to implement a function. At this time, a design mode can be used to allow the system to flexibly choose a solution, it can also easily add new solutions.

3. In software systems, there are many algorithms that can implement a certain function, such as searching and sorting. A common method is hard coding (hardcoding) in a class, to provide multiple search algorithms, you can write these algorithms into a class and provide multiple methods in the class. Each method corresponds to a specific search algorithm; of course, you can also encapsulate these search algorithms in a unified method, through if... Else... And other condition judgment statements. Both of these methods can be called hard encoding. To add a new search algorithm, you need to modify the source code of the encapsulated algorithm class. Replace the search algorithm, you also need to modify the client call code. A large number of search algorithms are encapsulated in this algorithm class. This type of code is complicated and difficult to maintain.

4. In addition to providing specialized search algorithm classes, you can also directly include the algorithm code in the client program. This approach is not desirable and will cause huge and difficult maintenance of the client program, if there are a large number of available algorithms, the problem will become more serious.

5. To solve these problems, you can define some independent classes to encapsulate different algorithms. Each class encapsulates a specific algorithm. Here, every class that encapsulates algorithms can be called strategy. To ensure the consistency of these policies, an abstract policy class is generally used for algorithm definition, each algorithm corresponds to a specific policy class.

 

Pattern intent:

Strategy pattern: definedA series of algorithms, SetEvery algorithm is encapsulated.And allow themMutual replacement.The rule mode changes the algorithm independently from the customers who use it., Also known as policy ). Policy mode is an object behavior mode.

 

UML diagram:

 

 

 

 

Role:

Context: The environment class can adopt multiple policies to solve a problem, and maintain a reference instance for the abstract strategy class in the Environment class.

Strategy: the abstract strategy class declares Abstract methods for supported algorithms and is the parent class of all policy classes.

Concretestrategy: the specific policy class implements the algorithms defined in the abstract policy class.

 

Mode Analysis:

1. The policy mode is a design mode that is easy to understand and use. The policy mode is an encapsulation of the algorithm. It separates the responsibility of the algorithm from the algorithm itself, delegate to different object management. Rule mode encapsulates a series of algorithms into a series of policy classes and serves as a subclass of an abstract policy class. In a word, it is "preparing a set of algorithms and encapsulating each algorithm to make them interchangeable"

 

Code:

Public abstract class abstractstrategy

{

Public abstract void algorithm ();

}

 

 

Public class concretestrategya extendsimplements actstrategy

{

Public void algorithm ()

{

// Algorithm

}

}

 

Public class context

{

Private abstractstrategy strategy;

Public void setstrategy (abstractstrategy strategy)

{

This. Strategy = strategy;

}

Public void algorithm ()

{

Strategy. algorithm ();

}

}

 

Client code:

Context context = new context ();

Abstractstrategy strategy;

Strategy = new concretestrategya ();

Context. setstrategy (Strategy );

Context. algorithm ();

 

 

 

Advantages and disadvantages:

Advantages

1. The policy mode providesPerfect support for the principle of opening/closingYou can select algorithms or actions without modifying the original system, or flexibly add new algorithms or actions.

2. The policy mode provides methods for managing related algorithm families.

3. The rule mode provides a way to replace the inheritance relationship.

4. Using policy mode can avoid using multiple conditional transfer statements.

 

Disadvantages:

1. The client must know all the policy classes and decide which one to use.

2. The policy mode will generate many policy classes. You can use the enjoy mode to reduce the number of objects to a certain extent.

 

Scope of use:

1. If there are many classes in a system, the difference between them is only their behavior, then the policy mode can dynamically allow an object to select a behavior among many behaviors.

2. A system must dynamically select one of several algorithms.

3. If an object has many behaviors and the appropriate mode is not used, these behaviors must be implemented using multiple conditional selection statements.

43001 do not want the client to know the complex and algorithm-related data structures, encapsulate algorithms and related data structures in specific policy classes, and improve the confidentiality and security of algorithms.

 

 

Mode extension:

Rule mode and status Mode

1. You can determine whether the policy mode or status mode is used by the number of environmental states.

2. Select a specific strategy type for the Environment type of the Policy mode. The specific strategy type does not need to care about the environment type. The environment type of the state mode needs to be put into a specific state due to external factors, in this way, the status can be switched. Therefore, there is a two-way association between the Environment class and the status class.

3. When using the Policy mode, the client needs to know which specific policy is selected. When using the status mode, the client does not need to care about the specific status, the environment status is automatically converted based on user operations.

4. If the objects of a class in the system have multiple states, the behavior varies under different States, and the status mode can be used for switching between these States; if a row of a class in the system has multiple implementation methods, and these implementation methods can be swapped, the policy mode is used.

 

 

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.