Design Pattern 5 (Strategy pattern)

Source: Internet
Author: User

Preface

Rule mode: it definesAlgorithmFamily, respectively encapsulated, so that they can be replaced with each other. This pattern changes the algorithm and does not affect the customers who use the algorithm.

Rule mode structure

 

Strategy: strategy class, which defines public interfaces of all supported algorithms

Concretestrategy1, concretestrategy2, and concretestrategy3 are specific policy classes, which encapsulate specific algorithms or actions and inherit from strategy.

Context context, which is configured with a concretestrategy to maintain a reference to the strategy object.

CodeImplementation

After a brief introduction to the definition of the Policy Mode and Its schema structure, we can further understand it through code.

Strategy class, which defines public interfaces of all supported algorithms

PublicAbstract  ClassStrategy {Public Abstract VoidAlgorithminterface ();}

Concretestrategy encapsulates specific algorithms or actions, inherited from strategy

 
 
     Public   Class  Concretestrategy1: Strategy {  Public   Override   Void  Algorithminterface () {console. writeline ( "  Implementation of algorithm 1  "  );}}  Public   Class  Concretestrategy2: Strategy {  Public   Override   Void  Algorithminterface () {console. writeline (  "  Implementation of algorithm 2  "  );}} Public   Class  Concretestrategy3: Strategy {  Public   Override   Void  Algorithminterface () {console. writeline (  "  Implementation of algorithm 3  "  );}} 

Context, which is configured with a concretestrategy to maintain a reference to the strategy object.

Public ClassContext {strategy Strategy;PublicContext (strategy Strategy ){This. Strategy =Strategy ;}Public VoidContextinterface () {strategy. algorithminterface ();}}

Client call code

     Class  Program {  Static   Void Main ( String [] ARGs) {context; Context = New Context ( New  Concretestrategy1 (); context. contextinterface (); Context = New Context ( New  Concretestrategy2 (); context. contextinterface (); Context = New Context ( New  Concretestrategy3 (); context. contextinterface (); console. Readline ();}} 

Running Effect display

Summary

Policy mode is a method that defines a series of algorithms. From a conceptual point of view, all these algorithms perform the same job, but they only implement differently, it can call all algorithms in the same way, reducing the coupling between various algorithm classes and algorithm classes.

Advantages of the Policy mode:

The strategy class hierarchy of the Policy mode defines a series of reusable algorithms or actions for context. Inheritance helps you analyze the common functions of these algorithms.

This simplifies unit testing because each algorithm has its own class and can be tested independently through its own interface.

In general, the rule mode is used to encapsulate algorithms, but in practice, we find that it can be used to encapsulate almost any type of rules, as long as you hear that different business rules need to be applied at different times during the analysis process, you can consider the possibility of using the Policy mode to handle such changes.

 

 

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.