Policy Mode-strategy
In policy mode, a class (the policy consumer) can change its execution policy. For example, in the sort algorithm, a variety of sorting algorithms belong to the sorting algorithm, but the implementation of the details of the algorithm, the user can easily replace the policy, select one to perform the task.
This article is basically a copy of Ilkka Seppälä (GitHub Iluwatar) example, his example is very image.
The scene is this: there is a dragon Slayer, he will replace different equipment (strategy) to fight against different dragons.
Defining the Dragonslayingstrategy interface
Strategy mode has a lot of strategies, here to abstract the Dragon Slayer strategy, define the Dragon Slayer scene of the strategy Dragonslayingstrategy interface.
/** * Dragon Slayer Policy interface */@FunctionalInterfacepublic interface Dragonslayingstrategy { void execute ();}
Dragonslayer class
Here defines the Dragon Slayer warrior Dragonslayer.
Dragon Slayer there is a default policy that can also be used to replace the policy and to use the current policy for the attack.
/** * Dragon Slayer Warrior */public class Dragonslayer { /** * Dragon Slayer Strategy */ private dragonslayingstrategy strategy; /** * If it is a null parameter constructor, assign a default policy * /Public Dragonslayer () { strategy = new Dragonslayingstrategy () { @ Override public Void execute () { System.out.println ("Default policy: Punching");}} ; } /** * Pass in a policy and instantiate it according to this policy Dragon Warrior * /Public Dragonslayer (Dragonslayingstrategy strategy) { This.strategy = strategy; } /** * Strategy can be changed at any time, change a bit better /* Public Dragonslayer Changestrategy (Dragonslayingstrategy strategy) { this.strategy = strategy; return this; } /** * Use current policy to execute Dragon Slayer */public void Gotobattle () { this.strategy.execute () }}
Realize
The basic model has come out, but we haven't created a specific strategy class yet, let's create two.
Spellstrategy class
/** * Mantra Strategy */public class Spellstrategy implements dragonslayingstrategy{@Override public void execute () { System.out.println ("Enchantment Strategy: Spell the Dragon seal Off");} }
Firestrategy class
/** * Firearms Strategy */public class Firestrategy implements Dragonslayingstrategy { @Override public Void execute () { S YSTEM.OUT.PRINTLN ("Firearms Strategy: Burning");} }
Main
Here is the run example
public class Main {public static void main (string[] args) {//There is a dragon Slayer warrior Dragonslayer slayer = new DragonS Layer (); System.out.println ("The Young Dragon appears"); Slayer.gotobattle (); /*-************ World Quiet A moment **************-*/System.out.println ("\ n Ice Dragon appeared!"); The Dragon Slayer found that he could be restrained by fire, so he changed his firearms strategy and attacked Slayer.changestrategy (new Firestrategy ()). Gotobattle (); /*-************ World Quiet A moment **************-*/System.out.println ("\ n the ancient dragon appeared!"); The dragon is so powerful that it can only be equipped with the spell-reading skill and then attack Slayer.changestrategy (New Spellstrategy ()). Gotobattle (); /*-************ the world was quiet for a moment **************-*/System.out.println ("The Magic Dragon appeared"); The Dragon Slayer ran out of all the tactics, learned a new skill on the spot, and did not have time to name the skill, the Dragon Slayer, Fast and Fast Slayer.changestrategy (new Dragonslayingstrategy () { @Override public void Execute () {System.out.println ("mystical skill: an epiphany from the palm of the sky, the Tathagata!"); }}). Gotobattle (); /*-************ World Quiet A moment **************-*/System.ouT.println ("The Dragon appears"); Dragon Slayer uses functional programming to invent a new kind of divine moves: Brainwashing Slayer.changestrategy (()-SYSTEM.OUT.PRINTLN ("Brainwashing strategy: Brainwashing ~~~~~~"). Gotobattle (); }}
Policy Mode-strategy (Java implementation)