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.