Design Pattern learning Summary-strategy Pattern)

Source: Internet
Author: User

Problem:
When we define a seriesAlgorithmHow can an algorithm be freely and dynamically replaced to cope with different business needs without affecting the use of the client? In addition, the increase, decrease, or modification of the algorithm will not affect the environment and the client.

Definition:
Policy patterns define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.

Intent:
The Strategy mode is the behavior mode. Because it is a behavior mode, it is not used to solve class instantiation and has nothing to do with what kind of products to create, the problem solved by this mode is to package different algorithms (Actions) into a series of policy classes, so that they can replace each other and provide an access interface, the client determines under what circumstances the specific policy is used to complete a function. You can also add and modify algorithms as needed to easily develop plug-in systems. For clients, if they don't care about instantiating those objects and produce those products, they only need to provide the policy to complete a function.

Participants:
• Strategy role:
Defines the public interfaces of the supported algorithms. Different algorithms implement this interface in different ways. context calls the algorithms defined by concretestrategy through this interface. It is generally implemented using an interface or abstract class.
• Concretestrategy role:
Inherits the role of an abstract policy (Strategy) and encapsulates specific algorithms and actions.
• Context:
The external encapsulation class of the policy, or the container class of the policy. It maintains a reference to the strategy object. Sets the specific concretestrategy of strategy during runtime dynamically based on different policies, and implements interaction and data transmission.
UML diagram:

 
Instance description:

Nokia mobile phone Factory
For example, Nokia has two production plants in China and Finland. The Asia Pacific version is produced in China and the European and American versions are produced in Finland. The production details of the two factories may be different, but both of them can produce n8 and Vivo mobile phones, in addition, mobile phone factories can be dynamically added, such as n8 and n9.

The UML diagram is as follows:

 

Code:

///   <Summary>
/// Mobile phone factory abstract policy (Strategy) Role
///   </Summary>
Public Abstract Class Iphonefactory
{
Public Abstract Void Createn8 ();
Public Abstract Void Createn9 ();
}
///   <Summary>
/// Mobile phone Factory policy (concretestrategy) Role
///   </Summary>
Public Class Phonefactory_china: iphonefactory
{

Public Override VoidCreaten8 ()
{
System. Console. writeline ("I am n8 from China.");
}

Public Override Void Createn9 ()
{
System. Console. writeline (" I am a region produced in China. " );
}
}
///   <Summary>
/// Mobile phone Factory policy (concretestrategy) Role
///   </Summary>
Public Class Phonefactory_finland: iphonefactory
{
Public Override Void Createn8 ()
{
System. Console. writeline ( " I am n8 from Finland. " );
}

Public Override Void Createn9 ()
{
System. Console. writeline ( " I'm from Finland. " );
}
}
///   <Summary>
/// Mobile phone production Context
///   </Summary>
Public Class Phonefactorycontext
{
Iphonefactory phonefactory = Null ;
Public Phonefactorycontext (iphonefactory _ phonefactory)
{
This . Phonefactory = _ phonefactory;
}

Public VoidCreaten8 ()
{
Phonefactory. createn8 ();
}

Public void createn9 ()
{< br> phonefactory. createn9 ();
}< BR >}

/// <Summary>
///Client
/// </Summary>
VoidStrategytest ()
{
Phonefactorycontext context =NewPhonefactorycontext (NewPhonefactory_china ());
Context. createn8 ();
}

 

Advantages:
• The policy mode provides methods for managing related algorithm families. The hierarchical structure of the Strategy class defines an algorithm or behavior family, and can play a very good role as a constraint.
• AvoidProgramTo make the system more flexible and easy to expand.

Disadvantages:

• Each specific policy class generates a new class, so it increases the number of classes to be maintained by the system.
• The client must know all the policy classes and decide which one to use.

Application Scenario:
• Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running.
• Different policies (algorithms) must be used in different situations, or they may be implemented in other ways in the future.
• Hides implementation details of specific policies (algorithms) from customers and is completely independent of each other.

 

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.