Java Design Pattern policy pattern
This article continues to introduce the strategy modes of the 23 design patterns series.
Background this situation is often encountered in software development. There are multiple algorithms or policies for implementing a function. We can select different algorithms or policies based on different environments or conditions to complete this function. Such as search and sorting. A common method is Hard Coding in a class. To provide multiple search algorithms, you can write these algorithms into a class, multiple methods are provided in this class. Each method corresponds to a specific search algorithm. Of course, these search algorithms can also be encapsulated in a unified method, through if... Else... Or case and other condition judgment statements for selection. 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. If we include these policies on the client, this approach is not desirable, it will lead to a large client program and difficult to maintain, if there are a large number of available algorithms, the problem will become more serious.
How can we split algorithms and objects so that algorithms can change independently of customers who use them?
The solution extracts frequently changed or potentially changed parts of a class as an interface and then contains the instance of this object in the class, this type of instance can call the class behavior that implements this interface at will during runtime. For example, you can define a series of algorithms, encapsulate each algorithm, and make them replace each other, so that the algorithm can change independently of the customers who use it. This is the rule mode.
Many classes are applicable only to different behaviors. "Policy" provides a method to configure a class with one of multiple actions. That is, a system must dynamically select one of several algorithms. It is used when an application needs to implement a specific service or function and has multiple implementation methods.
A class defines multiple behaviors, and these behaviors appear in the form of multiple conditional statements in the operations of this class. Move related condition branches into their respective Strategy classes to replace these condition statements.
Advantage 1: the behavior of objects can be dynamically changed. disadvantage 1. The client must know all the policy classes and decide which policy classes to use. 2. The policy mode will generate many policy classes.
Context: uses a ConcreteStrategy object for configuration. Maintain a reference to the Strategy object. You can define an interface to allow Strategy to access its data. Abstract Strategy: defines the public interfaces of all supported algorithms. Context uses this interface to call a ConcreteStrategy-defined algorithm. ConcreteStrategy: a specific algorithm is implemented using the Strategy interface.
The application scenario is as follows. Liu Bei is about to marry his wife in Jiangdong. Zhuge Liang gave Zhao cloud three tips, saying that the split by Tianji can solve the thorny problem. There are three elements in the scenario: Three Tips (specific strategies), one tip (Environment), and Zhao Yun (caller ).
Abstract Strategy)
public interface Strategy { public void operate();}
ConcreteStrategy: Coming to Wu
Public class BackDoor implements IStrategy {@ Override public void operate () {System. out. println (ask Qiao guolao for help and ask Wu Guotai to put pressure on Sun Quan so that Sun Quan cannot kill Liu Bei );}}Tip 2: ask Wu Guotai to open the green light
Public class GivenGreenLight implements IStrategy {@ Override public void operate () {System. out. println (ask Wu Guotai to open a green light and allow );}}Tip 3: after Mrs sun is disconnected, the defender will be blocked.
Public class BlackEnemy implements IStrategy {@ Override public void operate () {System. out. println (when Mrs sun is disconnected, block the pursuit of troops );}}
Context)
Public class Context {private Strategy strategy; // constructor, which one do you want to use public Context (Strategy strategy) {this. strategy = strategy;} public void setStrategy (Strategy strategy) {this. strategy = strategy;} public void operate () {this. strategy. operate ();}}
The following is the usage
Public class Zhaoyun {public static void main (String [] args) {Context context; System. out. println (---------- I just arrived at Wu Guo to use the first tip ---------------); context = new Context (new BackDoor (); context. operate (); System. out. println (); System. out. println (---------- Liu Bei is happy to use the second tip ---------------); context. setStrategy (new GivenGreenLight (); context. operate (); System. out. println (); System. out. println (---------- Sun Quan's pursuit came, using the third tip ---------------); context. setStrategy (new BlackEnemy (); context. operate (); System. out. println ();}}
After three moves, Zhou Lang was "Lost and defeated ".
The above is the policy mode. Multiple different solutions are dynamically switched to change object behavior.