Software Design Pattern strategy pattern strategy Pattern

Source: Internet
Author: User

Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it. (Original article: The Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. strategy lets the algorithm vary independently from clients that use it .) context (Application Scenario): 1. Use the algorithm provided by concretestrategy.

2. Maintain a strategy instance internally.

3. dynamically sets the specific implementation algorithm of strategy during runtime.

4. Interaction and data transmission with strategy.

Strategy ):

1. A public interface is defined. Different algorithms implement this interface in different ways. The context uses this interface to call different algorithms, which are generally implemented using interfaces or abstract classes.

Concretestrategy (specific policy class ):

2. Implemented the interface defined by strategy and provided specific algorithm implementation.

 

Application scenarios:

1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running. (For example, flybehavior and quackbehavior)

2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future. (For example, the specific implementation of flybehavior and quackbehavior can be changed or expanded at Will)

3. Hiding implementation details of specific policies (algorithms) from the customer (duck) is completely independent of each other.

 

UML diagram:

Implementation of Rule Mode
-The purpose of the Policy mode is to encapsulate each algorithm into an independent class with a common interface for mutual replacement.
-The policy mode allows the algorithm to change without affecting the client. The policy mode can be used to separate the behavior from the environment.
-The Environment class is responsible for maintaining and querying behavior classes, and various algorithms are provided in specific policies. Because the algorithm and environment are independent, modifications to the algorithm do not affect the environment and the client.

 

Procedure
-1. Define a public interface for the policy object.
-2. Write a policy class that implements the above public interface
-3. Save a reference to the policy object in the class using the Policy object.
-4. In the class using policy objects, set and get methods (injection) of policy objects or assign values using constructor methods are implemented.

 

 

• Disadvantages of policy Mode
-1. The client must know all the policy classes and decide which policy class to use.
-2. There are many strategies.

 

• Solution
-Factory method adopted

 

Test: The addition, subtraction, multiplication, and Division operations of two numbers are demonstrated.

I:

Create a Policy Interface

Package collection2;

Public interface strategy
{
Public int calculate (int A, int B );
}

 

II:

Method class for implementing a set of policies

Package collection2;

Public class addstrategy implements strategy
{

Public int calculate (int A, int B)
{
Return A + B;
}

}

 

Package collection2;

Public class subtractstrategy implements strategy
{
Public int calculate (int A, int B)
{
Return A-B;
}
}

 

Package collection2;

Public class multiplystrategy implements strategy
{

Public int calculate (int A, int B)
{
Return a * B;
}

}

 

Package collection2;

Public class dividestrategy implements strategy
{

Public int calculate (int A, int B)
{
Return A/B;
}

}

 

III:

Defines a policy object class to assign values to the set and get methods (injection) or constructor of the Policy object.

Package collection2;

Public class environment
{
Private strategy Strategy;

 
Public Environment (strategy Strategy)
{
This. setstrategy (Strategy );
}

 
Public void setstrategy (strategy Strategy)
{
This. Strategy = strategy;
}

 
Public strategy getstrategy ()
{
Return strategy;
}

 
Public int calculate (int A, int B)
{
Return strategy. Calculate (A, B );
}

}

 

IV:

Build a customer class for a policy and test the policy mode defined above

Package collection2;

Public class client
{
Public static void main (string [] ARGs)
{
Addstrategy = new addstrategy ();
// Addition policy
Environment = new environment (addstrategy );
System. Out. println ("addition policy" + environment. Calculate (8, 2 ));

// Subtraction Policy
Subtractstrategy = new subtractstrategy ();
Environment. setstrategy (subtractstrategy );
System. Out. println ("subtraction policy:" + environment. Calculate (8, 2 ));

// Multiplication Policy
Multiplystrategy = new multiplystrategy ();
Environment. setstrategy (multiplystrategy );
System. Out. println ("Multiplication policy:" + environment. Calculate (8, 2 ));

// Division Policy
Dividestrategy = new dividestrategy ();
Environment. setstrategy (dividestrategy );
System. Out. println ("Division Policy:" + environment. Calculate (8, 2 ));

}
}

Test results:

Addition policy 10
Subtraction policy: 6
Multiplication policy: 16
Division Policy: 4

Related Article

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.